107 lines
4.7 KiB
Python
107 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only Buryans Media Alpha: Jellyfin search + minimal UI."""
|
|
import html
|
|
import json
|
|
import urllib.error
|
|
import urllib.parse
|
|
import urllib.request
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
from pathlib import Path
|
|
|
|
JF = "http://192.168.1.103:8096"
|
|
KEY = Path("/root/.jellyfin-key").read_text().strip()
|
|
HOST, PORT = "127.0.0.1", 9180
|
|
|
|
|
|
def jf(path):
|
|
req = urllib.request.Request(JF + path, headers={
|
|
"Authorization": 'MediaBrowser Token="%s"' % KEY,
|
|
"Accept": "application/json",
|
|
})
|
|
with urllib.request.urlopen(req, timeout=10) as r:
|
|
return json.load(r)
|
|
|
|
|
|
def search(q):
|
|
if not q.strip():
|
|
return []
|
|
params = urllib.parse.urlencode({
|
|
"searchTerm": q, "IncludeItemTypes": "Movie,Series",
|
|
"Recursive": "true", "Limit": "24",
|
|
"Fields": "ProductionYear,Overview,ImageTags,RunTimeTicks",
|
|
})
|
|
data = jf("/Items?" + params)
|
|
out = []
|
|
for item in data.get("Items") or []:
|
|
out.append({
|
|
"id": item.get("Id"),
|
|
"name": item.get("Name"),
|
|
"year": item.get("ProductionYear"),
|
|
"type": item.get("Type"),
|
|
"overview": (item.get("Overview") or "")[:280],
|
|
"has_image": bool((item.get("ImageTags") or {}).get("Primary")),
|
|
})
|
|
return out
|
|
|
|
|
|
class H(BaseHTTPRequestHandler):
|
|
def out(self, code, body, ctype="application/json"):
|
|
if isinstance(body, (dict, list)):
|
|
body = json.dumps(body, ensure_ascii=False)
|
|
data = body.encode() if isinstance(body, str) else body
|
|
self.send_response(code)
|
|
self.send_header("Content-Type", ctype + "; charset=utf-8")
|
|
self.send_header("Content-Length", str(len(data)))
|
|
self.end_headers()
|
|
self.wfile.write(data)
|
|
|
|
def do_GET(self):
|
|
u = urllib.parse.urlparse(self.path)
|
|
if u.path == "/health":
|
|
try:
|
|
jf("/System/Info/Public")
|
|
self.out(200, {"ok": True, "jellyfin": True})
|
|
except Exception as e:
|
|
self.out(503, {"ok": False, "error": str(e)[:200]})
|
|
return
|
|
if u.path == "/v1/media/search":
|
|
q = urllib.parse.parse_qs(u.query).get("q", [""])[0]
|
|
try:
|
|
self.out(200, {"ok": True, "query": q, "items": search(q)})
|
|
except Exception as e:
|
|
self.out(502, {"ok": False, "error": str(e)[:300]})
|
|
return
|
|
if u.path == "/":
|
|
self.out(200, PAGE, "text/html")
|
|
return
|
|
self.out(404, {"ok": False, "error": "not found"})
|
|
|
|
def log_message(self, fmt, *args):
|
|
pass
|
|
|
|
|
|
PAGE = """<!doctype html><html lang=ru><head><meta charset=utf-8>
|
|
<meta name=viewport content="width=device-width,initial-scale=1">
|
|
<title>Buryans Media Alpha</title>
|
|
<style>
|
|
body{background:#0d1117;color:#e6edf3;font:15px/1.5 system-ui,Arial;margin:0;padding:28px}
|
|
h1{font-size:24px;margin:0 0 4px}.mut{color:#8b949e}
|
|
input{width:min(600px,80%);padding:12px;border-radius:8px;border:1px solid #30363d;background:#161b22;color:#e6edf3;font-size:16px}
|
|
button{padding:12px 16px;border-radius:8px;border:0;background:#2f81f7;color:#fff;font-size:16px;cursor:pointer}
|
|
.grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(230px,1fr));gap:12px;margin-top:18px}
|
|
.card{background:#161b22;border:1px solid #30363d;border-radius:10px;padding:14px}
|
|
.card h3{margin:0 0 4px;font-size:16px}.tag{color:#8b949e;font-size:12px}
|
|
a{color:#58a6ff}
|
|
</style></head><body>
|
|
<h1>Buryans Media Alpha</h1><div class=mut>Read-only поиск по Jellyfin. Запросы и просмотр — в native Jellyfin.</div>
|
|
<div style="margin:18px 0"><input id=q placeholder="Фильм или сериал..." autofocus> <button onclick="go()">Найти</button></div>
|
|
<div id=msg class=mut></div><div id=out class=grid></div>
|
|
<script>
|
|
const e=s=>String(s??'').replace(/[&<>"]/g,c=>({'&':'&','<':'<','>':'>','"':'"'}[c]));
|
|
async function go(){const q=document.getElementById('q').value.trim();if(!q)return;document.getElementById('msg').textContent='поиск...';const r=await fetch('/v1/media/search?q='+encodeURIComponent(q));const d=await r.json();if(!d.ok){document.getElementById('msg').textContent=d.error||'ошибка';return}document.getElementById('msg').textContent=d.items.length?'':'ничего не найдено';document.getElementById('out').innerHTML=d.items.map(x=>`<div class=card><h3>${e(x.name)}</h3><div class=tag>${e(x.type)} ${e(x.year||'')}</div><div class=mut style="margin-top:6px">${e(x.overview||'')}</div></div>`).join('')}
|
|
document.getElementById('q').addEventListener('keydown',e=>{if(e.key==='Enter')go()});
|
|
</script></body></html>"""
|
|
|
|
if __name__ == "__main__":
|
|
ThreadingHTTPServer((HOST, PORT), H).serve_forever()
|