[ARCH] Media API: work, availability, playback resolve; tests 5/5
This commit is contained in:
1 parent
a66a7a0e75
commit
a6a4e1ac89
2 files changed
+74
No files matched your search
@@ -40,6 +40,23 @@ def recent():
|
||||
return out
|
||||
|
||||
|
||||
def item(i):
|
||||
qs = urllib.parse.urlencode({"Ids": i, "Fields": "Overview,ProductionYear,MediaSources"})
|
||||
data = jf("/Items?" + qs)
|
||||
items = data.get("Items") or []
|
||||
if not items:
|
||||
raise RuntimeError("item not found")
|
||||
data = items[0]
|
||||
sources = data.get("MediaSources") or []
|
||||
return {
|
||||
"id": data.get("Id"), "name": data.get("Name"),
|
||||
"year": data.get("ProductionYear"), "type": data.get("Type"),
|
||||
"overview": data.get("Overview") or "",
|
||||
"available": bool(sources),
|
||||
"source_count": len(sources),
|
||||
}
|
||||
|
||||
|
||||
def search(q):
|
||||
if not q.strip():
|
||||
return []
|
||||
@@ -80,6 +97,28 @@ class H(BaseHTTPRequestHandler):
|
||||
return True
|
||||
return user in ALLOWED
|
||||
|
||||
def do_POST(self):
|
||||
if not self.allowed():
|
||||
self.out(403, {"ok": False, "error": "forbidden"})
|
||||
return
|
||||
u = urllib.parse.urlparse(self.path)
|
||||
if u.path != "/v1/media/playback/resolve":
|
||||
self.out(404, {"ok": False, "error": "not found"})
|
||||
return
|
||||
try:
|
||||
n = int(self.headers.get("Content-Length") or 0)
|
||||
body = json.loads(self.rfile.read(n).decode() or "{}")
|
||||
iid = body.get("id")
|
||||
if not iid:
|
||||
self.out(400, {"ok": False, "error": "id required"})
|
||||
return
|
||||
it = item(iid)
|
||||
self.out(200, {"ok": True, "provider": "jellyfin",
|
||||
"url": "https://media.buryans.ru/web/index.html#!/details?id=" + urllib.parse.quote(iid),
|
||||
"name": it["name"]})
|
||||
except Exception as e:
|
||||
self.out(502, {"ok": False, "error": str(e)[:300]})
|
||||
|
||||
def do_GET(self):
|
||||
if not self.allowed():
|
||||
self.out(403, {"ok": False, "error": "forbidden"})
|
||||
@@ -92,6 +131,24 @@ class H(BaseHTTPRequestHandler):
|
||||
except Exception as e:
|
||||
self.out(503, {"ok": False, "error": str(e)[:200]})
|
||||
return
|
||||
parts = [x for x in u.path.split("/") if x]
|
||||
if len(parts) == 4 and parts[:3] == ["v1", "media", "works"]:
|
||||
try:
|
||||
it = item(parts[3])
|
||||
if parts[3] and u.path.endswith("/availability"):
|
||||
self.out(200, {"ok": True, "available": it["available"], "source_count": it["source_count"]})
|
||||
else:
|
||||
self.out(200, {"ok": True, "item": it})
|
||||
except Exception as e:
|
||||
self.out(502, {"ok": False, "error": str(e)[:300]})
|
||||
return
|
||||
if len(parts) == 5 and parts[:3] == ["v1", "media", "works"] and parts[4] == "availability":
|
||||
try:
|
||||
it = item(parts[3])
|
||||
self.out(200, {"ok": True, "available": it["available"], "source_count": it["source_count"]})
|
||||
except Exception as e:
|
||||
self.out(502, {"ok": False, "error": str(e)[:300]})
|
||||
return
|
||||
if u.path == "/v1/media/recent":
|
||||
try:
|
||||
self.out(200, {"ok": True, "items": recent()})
|
||||
|
||||
@@ -27,6 +27,23 @@ class Alpha(unittest.TestCase):
|
||||
self.assertTrue(data.get("ok"))
|
||||
self.assertGreater(len(data.get("items") or []), 0)
|
||||
|
||||
def test_work_availability_playback(self):
|
||||
_, body = get("/v1/media/search?q=the")
|
||||
items = json.loads(body).get("items") or []
|
||||
self.assertTrue(items)
|
||||
iid = items[0]["id"]
|
||||
code, body = get("/v1/media/works/" + iid)
|
||||
self.assertEqual(code, 200)
|
||||
self.assertTrue(json.loads(body).get("item"))
|
||||
code, body = get("/v1/media/works/" + iid + "/availability")
|
||||
self.assertEqual(code, 200)
|
||||
self.assertTrue(json.loads(body).get("ok"))
|
||||
req = urllib.request.Request(BASE + "/v1/media/playback/resolve",
|
||||
data=json.dumps({"id": iid}).encode(),
|
||||
headers={"Content-Type": "application/json"}, method="POST")
|
||||
with urllib.request.urlopen(req, timeout=10) as r:
|
||||
self.assertEqual(r.status, 200)
|
||||
|
||||
def test_other_email_forbidden(self):
|
||||
with self.assertRaises(urllib.error.HTTPError) as ctx:
|
||||
get("/", {"X-Audit-Gateway": "1", "X-Audit-User": "other@example.com"})
|
||||
|
||||
Reference in new issue
Block a user