From e086737ca547fdd960bd6e4f9b10d7caa20b8e7a Mon Sep 17 00:00:00 2001 From: Sleeps Date: Fri, 9 Oct 2026 11:57:30 +0100 Subject: [PATCH] Expose authenticated Overview inventory and background batch controls --- scene_search/api.py | 98 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 6 deletions(-) diff --git a/scene_search/api.py b/scene_search/api.py index bdcdff2..498f4e5 100644 --- a/scene_search/api.py +++ b/scene_search/api.py @@ -13,9 +13,12 @@ from .config import settings from .database import connect, init_db from .frame_source import FrameSource from .index_jobs import IndexJobManager +from .overview_jobs import OverviewJobManager +from .overviews import overview_inventory from .search_service import SearchService WEB_INDEX_PATH = Path(__file__).parent / "web" / "index.html" +WEB_OVERVIEWS_PATH = Path(__file__).parent / "web" / "overviews.html" app = FastAPI( title="Jellyfin Scene Search API", @@ -25,6 +28,7 @@ app = FastAPI( _service: SearchService | None = None _index_jobs: IndexJobManager | None = None _caption_jobs: CaptionJobManager | None = None +_overview_jobs: OverviewJobManager | None = None _frame_source: FrameSource | None = None _job_start_lock = threading.Lock() @@ -40,6 +44,19 @@ class CaptionRequest(BaseModel): folder_path: str | None = None +class OverviewRequest(BaseModel): + action: str + media_id: str | None = None + library_id: str | None = None + limit: int = Field(default=20, ge=0, le=100000) + min_captions: int = Field(default=1, ge=1) + max_captions: int = Field(default=64, ge=1, le=64) + style: str = "auto" + model: str | None = None + refresh_drafts: bool = False + confirm_publish: bool = False + + def get_search_service() -> SearchService: global _service if _service is None: @@ -69,6 +86,13 @@ def get_caption_jobs() -> CaptionJobManager: return _caption_jobs +def get_overview_jobs() -> OverviewJobManager: + global _overview_jobs + if _overview_jobs is None: + _overview_jobs = OverviewJobManager(settings.db_path) + return _overview_jobs + + def get_frame_source() -> FrameSource: global _frame_source if _frame_source is None: @@ -97,6 +121,11 @@ def web_dashboard() -> FileResponse: return FileResponse(WEB_INDEX_PATH, media_type="text/html") +@app.get("/overviews", include_in_schema=False) +def web_overviews() -> FileResponse: + return FileResponse(WEB_OVERVIEWS_PATH, media_type="text/html") + + @app.get("/api/health") def health() -> dict: return get_search_service().health() @@ -145,9 +174,9 @@ def jellyfin_libraries() -> dict: def jellyfin_library_selection(request: IndexRequest) -> dict: try: with _job_start_lock: - if get_caption_jobs().running: + if get_caption_jobs().running or get_overview_jobs().running: raise RuntimeError( - "Captioning is currently running. Wait for it to finish before changing indexed libraries." + "Captioning or Overview synchronisation is running. Stop it before changing indexed libraries." ) return get_index_jobs().apply_library_selection(request.library_ids) except RuntimeError as exc: @@ -166,9 +195,9 @@ def jellyfin_library_selection(request: IndexRequest) -> dict: def jellyfin_index(request: IndexRequest) -> dict: try: with _job_start_lock: - if get_caption_jobs().running: + if get_caption_jobs().running or get_overview_jobs().running: raise RuntimeError( - "Captioning is currently running. Wait for it to finish before indexing." + "Captioning or Overview synchronisation is running. Stop it before indexing." ) return get_index_jobs().start( request.library_ids, @@ -239,9 +268,9 @@ def caption_folders(library_id: str = Query(min_length=1)) -> dict: def caption_start(request: CaptionRequest) -> dict: try: with _job_start_lock: - if get_index_jobs().running: + if get_index_jobs().running or get_overview_jobs().running: raise RuntimeError( - "Jellyfin indexing is currently running. Wait for it to finish before captioning." + "Jellyfin indexing or Overview synchronisation is running. Stop it before captioning." ) return get_caption_jobs().start( request.library_ids, @@ -297,6 +326,63 @@ def caption_stop() -> dict: raise HTTPException(status_code=409, detail=str(exc)) from exc +@app.get("/api/overviews/items", dependencies=[Depends(require_api_key)]) +def overview_items( + library_id: str | None = None, + offset: int = Query(default=0, ge=0), + limit: int = Query(default=50, ge=1, le=100), +) -> dict: + try: + return overview_inventory( + db_path=settings.db_path, library_id=library_id, + offset=offset, limit=limit, + ) + except ValueError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from exc + + +@app.get("/api/overviews/status", dependencies=[Depends(require_api_key)]) +def overview_status() -> dict: + return get_overview_jobs().status() + + +@app.post("/api/overviews/start", dependencies=[Depends(require_api_key)], status_code=202) +def overview_start(request: OverviewRequest) -> dict: + if request.action in {"apply", "restore"} and not request.confirm_publish: + raise HTTPException( + status_code=400, detail="Explicit confirmation is required for Jellyfin metadata changes" + ) + try: + with _job_start_lock: + if get_index_jobs().running or get_caption_jobs().running: + raise HTTPException( + status_code=409, + detail="Stop Jellyfin indexing/captioning before starting an Overview job", + ) + if get_overview_jobs().running: + raise HTTPException(status_code=409, detail="An Overview job is already running") + return get_overview_jobs().start( + action=request.action, media_id=request.media_id, + library_id=request.library_id, limit=request.limit, + min_captions=request.min_captions, + max_captions=request.max_captions, + style=request.style, model=request.model, + refresh_drafts=request.refresh_drafts, + ) + except ValueError as exc: + raise HTTPException(status_code=400, detail=str(exc)) from exc + except RuntimeError as exc: + raise HTTPException(status_code=409, detail=str(exc)) from exc + + +@app.post("/api/overviews/stop", dependencies=[Depends(require_api_key)]) +def overview_stop() -> dict: + try: + return get_overview_jobs().stop() + except RuntimeError as exc: + raise HTTPException(status_code=409, detail=str(exc)) from exc + + @app.get( "/api/frames/{frame_id}/thumbnail", dependencies=[Depends(require_api_key)],