"""Shared helpers, serializers and constants for the plex route package.""" import logging from fastapi import HTTPException from sqlalchemy import select from sqlalchemy.orm import Session from app.models import ( PlexItem, PlexLibrary, ) from app.plex.lookups import by_rating_key log = logging.getLogger("siftlode.plex") def item_or_404(db: Session, rating_key: str) -> PlexItem: it = by_rating_key(db, PlexItem, rating_key) if it is None: raise HTTPException(status_code=404, detail="Unknown Plex item") return it def _norm_lang(lang: str | None) -> str: """The overlay has an EN base + an optional HU twin; anything other than `hu` serves EN.""" return "hu" if lang == "hu" else "en" def _content_libs(db: Session) -> tuple[list[int], list[int]]: """The BROWSABLE content libraries, partitioned into (movie_lib_ids, show_lib_ids) — the single source of the browse scope. Excludes HU overlay libraries (`overlay_of` set): those are never browsed on their own, they only lend their Hungarian metadata to the EN twin's rows (E5 S5).""" libs = ( db.execute( select(PlexLibrary).where( PlexLibrary.enabled.is_(True), PlexLibrary.overlay_of.is_(None) ) ) .scalars() .all() ) movie_lib_ids = [lb.id for lb in libs if lb.kind == "movie"] show_lib_ids = [lb.id for lb in libs if lb.kind == "show"] return movie_lib_ids, show_lib_ids