spaceapi/app.py

60 lines
1.7 KiB
Python
Raw Normal View History

2024-01-27 12:55:09 +01:00
from functools import lru_cache
2024-01-18 22:22:02 +01:00
import json
2024-01-27 12:55:09 +01:00
import time
2024-01-18 22:22:02 +01:00
import urllib.request
from flask import Flask, jsonify
app = Flask(__name__)
2024-01-27 12:55:09 +01:00
@lru_cache(maxsize=1)
def get_jitsi_status(ttl_hash : int = 0) -> bool | None:
# ttl_hash is used to cache the result for a certain amount of time.
# This is done to rate-limit the outgoing requests to the jitsi server.
# Inspired by https://stackoverflow.com/a/55900800
# The jitsi server uses mod_census to provide a JSON representation of all
# currently active rooms. We can use this to check if the lounge is currently
# occupied.
2024-01-18 22:22:02 +01:00
try:
content = urllib.request.urlopen("https://talk.chaoswest.tv/room-census").read()
census = json.loads(content)
2024-01-19 15:49:15 +01:00
return any(room["room_name"] == "cws-lounge@muc.meet.jitsi" for room in census.get("room_census", []))
2024-01-18 22:22:02 +01:00
except Exception as e:
print("shit got fucked up:")
print(e)
return None
2024-01-27 12:55:09 +01:00
def get_ttl_hash(seconds : int = 1) -> int:
# Generates a monotonically increasing "hash" as time progresses
return round(time.time() / seconds)
2024-01-18 22:22:02 +01:00
@app.route("/spaceapi")
def spaceapi():
result = {
'api': '0.13',
'api_compatibility': ['14'],
'space': 'chaoswest.tv',
'logo': 'https://chaoswest.tv/logo.png',
'url': 'https://chaoswest.tv',
'location': {
'lat': 50.47718,
'lon': 12.33427,
'address': 'FSN1-DC14',
},
'contact': {
'mastodon': '@chaoswesttv@chaos.social',
}
}
2024-01-27 12:55:09 +01:00
status = get_jitsi_status(get_ttl_hash())
2024-01-18 22:22:02 +01:00
2024-01-19 15:49:15 +01:00
if status is not None:
2024-01-18 22:22:02 +01:00
result["state"] = {
'open': status
}
return jsonify(result)