From 2d298f982408f222ad344b2aa9c18bbe7dc70f12 Mon Sep 17 00:00:00 2001 From: Tyler Hoang Date: Wed, 6 May 2026 17:20:35 -0700 Subject: Add TMDB poster picker to film detail page - New movie_images() async function in services/tmdb.py fetches poster URLs from TMDB /movie/{id}/images endpoint, filtering for English and no-text posters only - New GET /tmdb/posters endpoint returns list of available posters for a TMDB ID - New POST /films/{film_id}/poster endpoint to save selected poster (mirrors the stars endpoint pattern) - Add "Change Poster" button on detail page (only shown if film has a TMDB ID) that opens a 3-column grid of posters - Selected poster gets accent border, main image updates instantly, no page reload needed - Posters are cached per load to avoid refetching on re-open Co-Authored-By: Claude Haiku 4.5 --- routers/films.py | 17 +++++++++ routers/tmdb.py | 15 +++++++- services/tmdb.py | 20 +++++++++++ templates/detail.html | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 1 deletion(-) diff --git a/routers/films.py b/routers/films.py index ec56c0a..4ac023c 100644 --- a/routers/films.py +++ b/routers/films.py @@ -467,6 +467,23 @@ async def update_film_stars(film_id: int, request: Request, db: Session = Depend return {"film_id": film.id, "stars": film.stars} +@router.post("/films/{film_id}/poster") +async def update_film_poster(film_id: int, request: Request, db: Session = Depends(get_db)): + film = _get_film_or_404(db, film_id) + try: + payload = await request.json() + except Exception as exc: # noqa: BLE001 + raise HTTPException(status_code=400, detail="Request body must be JSON.") from exc + + poster_url = payload.get("poster_url", "").strip() + if not poster_url: + raise HTTPException(status_code=400, detail="poster_url is required.") + + film.poster_url = poster_url + db.commit() + return {"film_id": film.id, "poster_url": film.poster_url} + + @router.get("/director/{director_name}") async def director_detail(director_name: str, request: Request, db: Session = Depends(get_db)): films = _director_films(db, director_name) diff --git a/routers/tmdb.py b/routers/tmdb.py index 522c1d0..5f7943f 100644 --- a/routers/tmdb.py +++ b/routers/tmdb.py @@ -3,7 +3,7 @@ from dotenv import load_dotenv from fastapi import APIRouter, Query from fastapi.responses import JSONResponse -from services.tmdb import TMDBNotConfiguredError, search_movies +from services.tmdb import TMDBNotConfiguredError, search_movies, movie_images load_dotenv() @@ -30,3 +30,16 @@ async def search_tmdb(q: str = Query(..., min_length=2)): "results": [], }, ) + + +@router.get("/posters") +async def tmdb_posters(tmdb_id: int = Query(...)): + try: + async with httpx.AsyncClient(timeout=10.0) as client: + urls = await movie_images(client, tmdb_id) + return {"posters": urls} + except TMDBNotConfiguredError: + return JSONResponse( + status_code=503, + content={"error": "TMDB_API_KEY is not configured.", "posters": []}, + ) diff --git a/services/tmdb.py b/services/tmdb.py index 3618168..3574f57 100644 --- a/services/tmdb.py +++ b/services/tmdb.py @@ -84,6 +84,26 @@ async def movie_detail(client: httpx.AsyncClient, movie_id: int, key: str | None return response.json() +async def movie_images(client: httpx.AsyncClient, movie_id: int) -> list[str]: + """Return list of poster URLs for a TMDB movie ID.""" + try: + url = f"https://api.themoviedb.org/3/movie/{movie_id}/images" + response = await client.get( + url, + params={"api_key": api_key(), "include_image_language": "en,null"}, + ) + response.raise_for_status() + data = response.json() + except httpx.HTTPError: + return [] + + return [ + poster_url(p["file_path"]) + for p in data.get("posters", []) + if p.get("file_path") + ] + + def movie_payload(movie: dict, fallback: dict | None = None) -> dict: fallback = fallback or {} release_date = movie.get("release_date") or fallback.get("release_date") diff --git a/templates/detail.html b/templates/detail.html index 59779d8..20897b0 100644 --- a/templates/detail.html +++ b/templates/detail.html @@ -12,6 +12,23 @@ {{ film.title[:1] }} {% endif %} + + {% if film.tmdb_id %} + + + + {% endif %} +
Shelf @@ -122,4 +139,83 @@
+ +{% if film.tmdb_id %} + +{% endif %} {% endblock %} -- cgit v1.3-2-g0d8e