The media tool gets a status op (the title's stage, Radarr's progress from Seerr's downloadStatus, the public Jellyfin link) and unwatch. A status poll with watch renews a lease on the caller's records of the title: while it lives the Seerr webhook holds its push, the poll that sees the end claims it, and an unclaimed hold goes out when the lease lapses. /api/movies/status|search feed the conversation's movie cards — the owner or a guest with Movies on, cached, nothing in the ledger. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
3.5 KiB
Python
100 lines
3.5 KiB
Python
"""``/api/movies/*`` — what the conversation's movie cards read
|
|
(``frontend/src/business/movies/``), on the app's own session: the owner, or a
|
|
guest with the Movies group switched on.
|
|
|
|
* ``GET /api/movies/status?type=&tmdb=`` — one title's live state
|
|
(``media.op_status``: stage, Radarr's download progress, the Jellyfin link),
|
|
polled by a card while its title is on its way;
|
|
* ``GET /api/movies/search?q=&year=&type=`` — a search card's posters.
|
|
|
|
Read-only and free: no Seerr user is made, nothing is requested, nothing goes
|
|
in the tool ledger. Short-lived caches keep a page of open cards off Seerr
|
|
(``STATUS_TTL_S``, ``SEARCH_TTL_S``).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import time
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
|
|
import schemas
|
|
from core.http import _r
|
|
from core.state import State
|
|
from users.caller import CallerDep
|
|
|
|
from . import access, media
|
|
|
|
router = APIRouter()
|
|
|
|
STATUS_TTL_S = 15
|
|
SEARCH_TTL_S = 600
|
|
_cache: dict[tuple, tuple[float, dict]] = {}
|
|
_cache_lock = threading.Lock()
|
|
|
|
|
|
def _cached(key: tuple, ttl: float) -> dict | None:
|
|
with _cache_lock:
|
|
hit = _cache.get(key)
|
|
return hit[1] if hit and time.time() - hit[0] < ttl else None
|
|
|
|
|
|
def _keep(key: tuple, value: dict) -> dict:
|
|
with _cache_lock:
|
|
if len(_cache) > 2000:
|
|
_cache.clear()
|
|
_cache[key] = (time.time(), value)
|
|
return value
|
|
|
|
|
|
def _ctx(deps, caller) -> media.Ctx:
|
|
"""The caller as the media ops see them; a guest needs Movies switched on."""
|
|
if caller.is_owner:
|
|
return media.ctx_for(deps, None, None)
|
|
if not deps.users.active(caller.id) or deps.security_state.guests_locked():
|
|
raise HTTPException(403, "not available")
|
|
if "media" not in access.config(deps, access.Who(caller.id))["groups"]:
|
|
raise HTTPException(403, "Movies isn't switched on for you")
|
|
return media.ctx_for(deps, caller.id, None)
|
|
|
|
|
|
async def _run(fn, deps, ctx: media.Ctx, body: dict) -> dict:
|
|
from .routes import _http
|
|
if not media.api_key():
|
|
raise HTTPException(503, "the hub has no Seerr key")
|
|
try:
|
|
return await fn(media.Seerr(_http()), ctx, body)
|
|
except media.MediaError as e:
|
|
raise HTTPException(e.status, e.message) from None
|
|
|
|
|
|
@router.get("/api/movies/status", responses=_r(schemas.MovieStatus))
|
|
async def movie_status(deps: State, caller: CallerDep, tmdb: int, type: str = "movie"):
|
|
"""One title's stage, download progress and link, for its card."""
|
|
ctx = _ctx(deps, caller)
|
|
kind = "tv" if type == "tv" else "movie"
|
|
key = ("status", ctx.uid, ctx.lang, kind, tmdb)
|
|
hit = _cached(key, STATUS_TTL_S)
|
|
if hit is not None:
|
|
# The lease is the hub's own and cheap: always fresh.
|
|
return {**hit, "watching": media.STORE.watching(kind, tmdb)}
|
|
return _keep(key, await _run(media.op_status, deps, ctx, {"type": kind, "tmdb": tmdb}))
|
|
|
|
|
|
@router.get("/api/movies/search", responses=_r(schemas.MovieSearch))
|
|
async def movie_search(deps: State, caller: CallerDep, q: str, year: int | None = None,
|
|
type: str | None = None):
|
|
"""A search, as the skill ran it, with posters."""
|
|
ctx = _ctx(deps, caller)
|
|
body: dict = {"query": q}
|
|
if year:
|
|
body["year"] = year
|
|
if type in ("movie", "tv"):
|
|
body["type"] = type
|
|
key = ("search", ctx.uid, ctx.lang, " ".join(q.lower().split()), year, body.get("type"))
|
|
hit = _cached(key, SEARCH_TTL_S)
|
|
if hit is not None:
|
|
return hit
|
|
return _keep(key, await _run(media.op_search, deps, ctx, body))
|