CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
Mirrors TMDb's own /trending/{movie|tv}/{day|week} ranking rather than
Aurora computing trending itself from popularity deltas -- that would
require re-syncing the entire catalog on a schedule, infeasible at any
real scale, whereas TMDb already computes this well from signals (real
view/search velocity) Aurora has no access to.
New: TrendingEntry model + migration 0003, MetadataProvider.get_trending
(concrete/optional, like aclose(), so existing test fakes don't all need
updating), refresh_trending() which actually ingests each trending item
(only ~20 per window, cheap enough to fully ingest rather than apply the
never-fabricate-a-stub rule recommendations uses), a twice-daily arq cron
job (WorkerSettings.cron_jobs, new to this codebase), a manual admin
trigger for right after a deploy, and GET /movies/trending + GET
/series/trending.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
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)
|