diff --git a/ops/media-alpha/media_alpha.py b/ops/media-alpha/media_alpha.py index e0b9901..66f7176 100644 --- a/ops/media-alpha/media_alpha.py +++ b/ops/media-alpha/media_alpha.py @@ -44,6 +44,33 @@ def recent(): return out +def breakglass_auth(): + lines = Path("/root/.jellyfin-breakglass").read_text().splitlines() + user = next(x.split("=", 1)[1] for x in lines if x.startswith("username=")) + pw = next(x.split("=", 1)[1] for x in lines if x.startswith("password=")) + req = urllib.request.Request( + JF + "/Users/AuthenticateByName", + data=json.dumps({"Username": user, "Pw": pw}).encode(), + headers={"Content-Type": "application/json", + "Authorization": 'MediaBrowser Client="Alpha", Device="Host", DeviceId="alpha", Version="1.0"'}, + method="POST") + with urllib.request.urlopen(req, timeout=10) as r: + d = json.load(r) + return d.get("AccessToken"), (d.get("User") or {}).get("Id") + + +def continue_items(): + tok, uid = breakglass_auth() + if not tok or not uid: + return [] + h = {"Authorization": 'MediaBrowser Token="%s"' % tok, "Accept": "application/json"} + req = urllib.request.Request(JF + "/Users/%s/Items/Resume?Limit=10&MediaTypes=Video" % uid, headers=h) + with urllib.request.urlopen(req, timeout=10) as r: + data = json.load(r) + return [{"id": x.get("Id"), "name": x.get("Name"), "type": x.get("Type")} + for x in (data.get("Items") or [])] + + def prism_stats(): with urllib.request.urlopen("http://127.0.0.1:8095/api/prism/llm", timeout=6) as r: return json.load(r) @@ -291,13 +318,25 @@ class H(BaseHTTPRequestHandler): return if u.path == "/v1/media/requests": code, body = seerr("/api/v1/request?take=20") + if code == 503: + time.sleep(1) + code, body = seerr("/api/v1/request?take=20") self.out(code, body) return if len(parts) == 4 and parts[:3] == ["v1", "media", "requests"]: code, body = seerr("/api/v1/request/%s" % urllib.parse.quote(parts[3])) + if code == 503: + time.sleep(1) + code, body = seerr("/api/v1/request/%s" % urllib.parse.quote(parts[3])) self.out(code, body) return - if u.path in ("/v1/media/continue", "/v1/media/releases", "/v1/media/recommendations"): + if u.path == "/v1/media/continue": + try: + self.out(200, {"ok": True, "items": continue_items()}) + except Exception as e: + self.out(502, {"ok": False, "error": str(e)[:200]}) + return + if u.path in ("/v1/media/releases", "/v1/media/recommendations"): self.out(501, {"ok": False, "error": "not_configured", "endpoint": u.path}) return diff --git a/ops/media-alpha/tests/test_media_paths.py b/ops/media-alpha/tests/test_media_paths.py index dfc3ad8..166d08d 100644 --- a/ops/media-alpha/tests/test_media_paths.py +++ b/ops/media-alpha/tests/test_media_paths.py @@ -29,7 +29,7 @@ class MediaPaths(unittest.TestCase): "/v1/media/works/" + iid + "/availability": 200, "/v1/media/works/" + iid + "/editions": 200, "/v1/media/requests": 200, - "/v1/media/continue": 501, + "/v1/media/continue": 200, "/v1/media/releases": 501, "/v1/media/recommendations": 501, }