Files
Aurora-Core/app/api/v1/series.py
T
NixReaperandClaude Sonnet 5 0ab31a314c Add movie/series ratings and watchlist
Builds on the accounts foundation: PUT/DELETE a rating (0.5-10.0) or a
watchlist entry on any movie or series, all scoped to the bearer token's
own user, plus GET /account/ratings|watchlist/{movies,series} to list
them back. Both are polymorphic entity_type/entity_id tables, same shape
as watch_provider_offerings and trending_entries, so movie and series
share one repository/router each rather than duplicating per type.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-06 10:47:41 -05:00

189 lines
6.6 KiB
Python

"""Series endpoints (section 17)."""
from __future__ import annotations
from typing import Literal
from fastapi import APIRouter, Depends, HTTPException, Query, status
from redis.asyncio import Redis
from sqlalchemy.ext.asyncio import AsyncSession
from app.api.deps import cached_response, get_current_user, not_found
from app.core.database import get_db
from app.core.redis import get_redis
from app.core.security import enforce_rate_limit
from app.models.user import User
from app.schemas.account import RatingIn, RatingOut
from app.schemas.tv import (
SeriesCreditsOut,
SeriesDetailOut,
SeriesImagesOut,
SeriesListOut,
SeriesSeasonsOut,
SeriesVideosOut,
SeriesWatchProvidersOut,
)
from app.services import tv_service
from app.services.cache import build_key
router = APIRouter(prefix="/series", tags=["series"], dependencies=[Depends(enforce_rate_limit)])
@router.get("", response_model=SeriesListOut)
async def list_series(
genre: str | None = Query(None, description="Aurora genre id, e.g. aur_genre_..."),
keyword: str | None = Query(None, description="Aurora keyword id, e.g. aur_keyword_..."),
year: int | None = Query(None, description="First air date year"),
status_: str | None = Query(None, alias="status", description="Series status, e.g. ended"),
sort_by: Literal["popularity", "first_air_date", "vote_average", "title"] = Query("popularity"),
sort_dir: Literal["asc", "desc"] = Query("desc"),
page: int = Query(1, ge=1),
page_size: int = Query(20, ge=1, le=50),
session: AsyncSession = Depends(get_db),
) -> SeriesListOut:
try:
return await tv_service.list_series(
session,
genre_aurora_id=genre,
keyword_aurora_id=keyword,
year=year,
status=status_,
sort_by=sort_by,
sort_dir=sort_dir,
page=page,
page_size=page_size,
)
except tv_service.InvalidSeriesFilterError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, str(exc)) from exc
@router.get("/trending", response_model=SeriesListOut)
async def trending_series(
time_window: Literal["day", "week"] = Query("day", alias="window"),
page: int = Query(1, ge=1),
page_size: int = Query(20, ge=1, le=50),
session: AsyncSession = Depends(get_db),
) -> SeriesListOut:
try:
return await tv_service.list_trending(
session, time_window=time_window, page=page, page_size=page_size
)
except tv_service.InvalidSeriesFilterError as exc:
raise HTTPException(status.HTTP_422_UNPROCESSABLE_CONTENT, str(exc)) from exc
@router.get("/{aurora_id}", response_model=SeriesDetailOut)
async def get_series(
aurora_id: str,
session: AsyncSession = Depends(get_db),
redis: Redis = Depends(get_redis),
) -> SeriesDetailOut:
try:
return await cached_response(
redis,
build_key("series", aurora_id),
SeriesDetailOut,
lambda: tv_service.get_series_detail(session, aurora_id),
)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.get("/{aurora_id}/seasons", response_model=SeriesSeasonsOut)
async def get_series_seasons(
aurora_id: str, session: AsyncSession = Depends(get_db)
) -> SeriesSeasonsOut:
try:
return await tv_service.get_series_seasons(session, aurora_id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.get("/{aurora_id}/credits", response_model=SeriesCreditsOut)
async def get_series_credits(
aurora_id: str, session: AsyncSession = Depends(get_db)
) -> SeriesCreditsOut:
try:
return await tv_service.get_series_credits(session, aurora_id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.get("/{aurora_id}/images", response_model=SeriesImagesOut)
async def get_series_images(
aurora_id: str, session: AsyncSession = Depends(get_db)
) -> SeriesImagesOut:
try:
return await tv_service.get_series_images(session, aurora_id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.get("/{aurora_id}/videos", response_model=SeriesVideosOut)
async def get_series_videos(
aurora_id: str, session: AsyncSession = Depends(get_db)
) -> SeriesVideosOut:
try:
return await tv_service.get_series_videos(session, aurora_id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.get("/{aurora_id}/watch-providers", response_model=SeriesWatchProvidersOut)
async def get_series_watch_providers(
aurora_id: str, session: AsyncSession = Depends(get_db)
) -> SeriesWatchProvidersOut:
try:
return await tv_service.get_series_watch_providers(session, aurora_id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.put("/{aurora_id}/rating", response_model=RatingOut)
async def rate_series(
aurora_id: str,
body: RatingIn,
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db),
) -> RatingOut:
try:
return await tv_service.rate_series(session, aurora_id, user_id=user.id, value=body.value)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.delete("/{aurora_id}/rating", status_code=status.HTTP_204_NO_CONTENT)
async def unrate_series(
aurora_id: str,
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db),
) -> None:
try:
await tv_service.unrate_series(session, aurora_id, user_id=user.id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.post("/{aurora_id}/watchlist", status_code=status.HTTP_204_NO_CONTENT)
async def add_series_to_watchlist(
aurora_id: str,
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db),
) -> None:
try:
await tv_service.add_series_to_watchlist(session, aurora_id, user_id=user.id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc
@router.delete("/{aurora_id}/watchlist", status_code=status.HTTP_204_NO_CONTENT)
async def remove_series_from_watchlist(
aurora_id: str,
user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db),
) -> None:
try:
await tv_service.remove_series_from_watchlist(session, aurora_id, user_id=user.id)
except tv_service.SeriesNotFoundError as exc:
raise not_found("series", aurora_id) from exc