import httpx from dotenv import load_dotenv from fastapi import APIRouter, Query from fastapi.responses import JSONResponse from services.tmdb import TMDBNotConfiguredError, search_movies, movie_images load_dotenv() router = APIRouter(prefix="/tmdb", tags=["tmdb"]) @router.get("/search") async def search_tmdb(q: str = Query(..., min_length=2)): try: return {"results": await search_movies(q, limit=8, include_details=True)} except TMDBNotConfiguredError: return JSONResponse( status_code=503, content={ "error": "TMDB_API_KEY is not configured.", "results": [], }, ) except httpx.HTTPError: return JSONResponse( status_code=502, content={ "error": "TMDB search failed. Check your API key and try again.", "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": []}, )