From bb2c594cbf92e6fbfb5940b9b7c32e84170dfe41 Mon Sep 17 00:00:00 2001 From: architect Date: Fri, 27 Nov 2026 10:10:15 +0300 Subject: [PATCH] [ARCH] Seerr session cache: requests stable 200 --- ops/media-alpha/media_alpha.py | 37 ++++++++++++++++++----- ops/media-alpha/tests/test_media_paths.py | 2 +- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/ops/media-alpha/media_alpha.py b/ops/media-alpha/media_alpha.py index 8783ded..02a586b 100644 --- a/ops/media-alpha/media_alpha.py +++ b/ops/media-alpha/media_alpha.py @@ -3,6 +3,7 @@ import html import http.cookiejar import json +import time import urllib.error import urllib.parse import urllib.request @@ -13,6 +14,8 @@ JF = "http://192.168.1.103:8096" KEY = Path("/root/.jellyfin-key").read_text().strip() HOST, PORT = "127.0.0.1", 9180 ALLOWED = {"maloyakapg@gmail.com"} +_SEERR_OP = None +_SEERR_TS = 0.0 def jf(path): @@ -108,7 +111,15 @@ def home_status(): def seerr(path): """Read-only Seerr requests via the local breakglass Jellyfin login.""" + global _SEERR_OP, _SEERR_TS base = "http://192.168.1.121:5055" + if _SEERR_OP is not None and time.time() - _SEERR_TS < 1800: + try: + req = urllib.request.Request(base + path, headers={"Accept": "application/json"}) + with _SEERR_OP.open(req, timeout=8) as r: + return 200, json.load(r) + except Exception: + _SEERR_OP = None cred = Path("/root/.jellyfin-breakglass") try: lines = cred.read_text().splitlines() @@ -116,13 +127,25 @@ def seerr(path): pw = next(x.split("=", 1)[1] for x in lines if x.startswith("password=")) cj = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) - login = urllib.request.Request( - base + "/api/v1/auth/jellyfin", - data=json.dumps({"username": user, "password": pw}).encode(), - headers={"Content-Type": "application/json"}, method="POST") - with op.open(login, timeout=8) as r: - if r.status != 200: - return 503, {"ok": False, "error": "seerr_login_%s" % r.status} + ok = False + for attempt in range(3): + login = urllib.request.Request( + base + "/api/v1/auth/jellyfin", + data=json.dumps({"username": user, "password": pw}).encode(), + headers={"Content-Type": "application/json"}, method="POST") + try: + with op.open(login, timeout=8) as r: + if r.status == 200: + ok = True + break + except urllib.error.HTTPError as e: + if e.code not in (401, 403): + raise + time.sleep(0.5) + if not ok: + return 503, {"ok": False, "error": "seerr_login_forbidden"} + _SEERR_OP = op + _SEERR_TS = time.time() req = urllib.request.Request(base + path, headers={"Accept": "application/json"}) with op.open(req, timeout=8) as r: return 200, json.load(r) diff --git a/ops/media-alpha/tests/test_media_paths.py b/ops/media-alpha/tests/test_media_paths.py index 3521de0..dfc3ad8 100644 --- a/ops/media-alpha/tests/test_media_paths.py +++ b/ops/media-alpha/tests/test_media_paths.py @@ -28,7 +28,7 @@ class MediaPaths(unittest.TestCase): "/v1/media/works/" + iid: 200, "/v1/media/works/" + iid + "/availability": 200, "/v1/media/works/" + iid + "/editions": 200, - "/v1/media/requests": 503, + "/v1/media/requests": 200, "/v1/media/continue": 501, "/v1/media/releases": 501, "/v1/media/recommendations": 501,