summaryrefslogtreecommitdiff
path: root/routers/tmdb.py
blob: 522c1d05f03151608db9c02382f3942788e8ed01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import httpx
from dotenv import load_dotenv
from fastapi import APIRouter, Query
from fastapi.responses import JSONResponse

from services.tmdb import TMDBNotConfiguredError, search_movies

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": [],
            },
        )