54 lines
2.1 KiB
Python
54 lines
2.1 KiB
Python
"""Refreshes Aurora's mirror of TMDb's own trending lists (section 17,
|
|||
|
|
additive) -- see app/models/trending.py for why this mirrors TMDb's
|
||
|
|
ranking rather than Aurora computing its own from popularity deltas.
|
||
|
|
|
||
|
|
Unlike recommendations' never-fabricate-a-stub rule, this actually
|
||
|
|
ingests/refreshes each trending item's own metadata as a side effect --
|
||
|
|
there are only ~20 items per (media_type, time_window) window (TMDb's
|
||
|
|
first page), cheap enough to fully ingest rather than silently drop
|
||
|
|
anything Aurora hasn't seen yet.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from app.core.database import session_scope
|
||
|
|
from app.core.logging import get_logger
|
||
|
|
from app.providers.base import MetadataProvider
|
||
|
|
from app.repositories.trending_repository import replace_trending
|
||
|
|
from app.services.ingestion.movie_ingestion import ingest_movie
|
||
|
|
from app.services.ingestion.series_ingestion import ingest_series
|
||
|
|
|
||
|
|
logger = get_logger(__name__)
|
||
|
|
|
||
|
|
# TMDb's own media_type values -> Aurora's entity_type values.
|
||
|
|
_MEDIA_TYPE_TO_ENTITY_TYPE = {"movie": "movie", "tv": "series"}
|
||
|
|
|
||
|
|
|
||
|
|
async def refresh_trending(provider: MetadataProvider, media_type: str, time_window: str) -> int:
|
||
|
|
"""Fetch TMDb's current trending list for (media_type, time_window),
|
||
|
|
ingest/refresh each item, and replace Aurora's own ranked snapshot for
|
||
|
|
that window. Returns how many entries were written."""
|
||
|
|
|
||
|
|
entity_type = _MEDIA_TYPE_TO_ENTITY_TYPE[media_type]
|
||
|
|
raw_results = await provider.get_trending(media_type, time_window)
|
||
|
|
|
||
|
|
entity_ids = []
|
||
|
|
for item in raw_results:
|
||
|
|
external_id = str(item["id"])
|
||
|
|
async with session_scope() as session:
|
||
|
|
if media_type == "movie":
|
||
|
|
entity_id = await ingest_movie(session, provider, external_id)
|
||
|
|
else:
|
||
|
|
entity_id = await ingest_series(session, provider, external_id)
|
||
|
|
entity_ids.append(entity_id)
|
||
|
|
|
||
|
|
async with session_scope() as session:
|
||
|
|
await replace_trending(
|
||
|
|
session, entity_type=entity_type, time_window=time_window, entity_ids=entity_ids
|
||
|
|
)
|
||
|
|
|
||
|
|
logger.info(
|
||
|
|
"trending_refreshed", media_type=media_type, time_window=time_window, count=len(entity_ids)
|
||
|
|
)
|
||
|
|
return len(entity_ids)
|