auto-commit for 0175172f-4f7e-4804-ad7f-68312dcdad0d

This commit is contained in:
emergent-agent-e1
2026-07-23 22:23:41 +00:00
parent 22d8479b39
commit bd8480bd14
11 changed files with 89 additions and 31 deletions
+47
View File
@@ -706,6 +706,12 @@ async def list_subs(movie_id: str, user: dict = Depends(get_current_user)):
return docs
@api.get("/episodes/{episode_id}/subtitles", response_model=List[Subtitle])
async def list_episode_subs(episode_id: str, user: dict = Depends(get_current_user)):
docs = await db.subtitles.find({"episode_id": episode_id}, {"_id": 0}).sort("created_at", 1).to_list(20)
return docs
@api.post("/movies/{movie_id}/subtitles", response_model=Subtitle)
async def upload_sub(
movie_id: str,
@@ -979,6 +985,8 @@ async def radarr_import(payload: dict, user: dict = Depends(require_admin)):
await db.movies.insert_one(movie)
if auto in ("quick", "abr"):
await _enqueue_transcode(movie["id"], auto, triggered_by="auto")
# Auto-fetch English subtitle (fire and forget)
asyncio.create_task(_auto_fetch_subtitle("movie", movie["id"], movie.get("tmdb_id")))
created += 1
return {"ok": True, "imported": created}
@@ -1208,6 +1216,13 @@ async def sonarr_import(payload: dict, user: dict = Depends(require_admin)):
sonarr_episode_id=ep["sonarr_episode_id"],
).model_dump()
await db.episodes.insert_one(edoc); created_eps += 1
# Auto-fetch English subtitle for this episode (needs show's tmdb_id + season/episode numbers)
show_doc = await db.shows.find_one({"id": show_id}, {"_id": 0, "tmdb_id": 1})
if show_doc and show_doc.get("tmdb_id"):
asyncio.create_task(_auto_fetch_subtitle(
"episode", edoc["id"], show_doc["tmdb_id"],
season=ep["season_number"], episode=ep["episode_number"],
))
# Update counts
seasons = await db.episodes.distinct("season_number", {"show_id": show_id})
ecount = await db.episodes.count_documents({"show_id": show_id})
@@ -1379,6 +1394,38 @@ async def tmdb_enrich_all(user: dict = Depends(require_admin)):
return {"ok": True, "enriched": enriched, "skipped": skipped, "failed": failed}
async def _auto_fetch_subtitle(content_type: str, content_id: str, tmdb_id: Optional[int],
season: Optional[int] = None, episode: Optional[int] = None,
language: str = "en", label: str = "English"):
"""Best-effort background: pull first OpenSubs result and attach to movie/episode."""
if not tmdb_id: return
s = await get_settings()
key = s.get("opensubs_api_key", "")
if not key: return
try:
results = await asyncio.to_thread(opensubs_client.search, key, tmdb_id, None, language, episode, season)
if not results: return
r = results[0]
link = await asyncio.to_thread(opensubs_client.get_download_link, key, r["file_id"])
srt = await asyncio.to_thread(opensubs_client.download_content, link)
vtt = srt_to_vtt(srt) if "WEBVTT" not in srt[:20] else srt
except Exception as e:
logger.warning(f"Auto-subtitle failed for {content_type} {content_id}: {e}")
return
sid = str(uuid.uuid4()); fname = f"{sid}.vtt"
(SUBS_DIR / fname).write_text(vtt, encoding="utf-8")
doc = {
"id": sid,
"movie_id": content_id if content_type == "movie" else "",
"episode_id": content_id if content_type == "episode" else "",
"language": language, "label": label, "storage_path": fname,
"is_default": True,
"created_at": datetime.now(timezone.utc).isoformat(),
}
await db.subtitles.insert_one(doc)
logger.info(f"Auto-attached subtitle for {content_type} {content_id[:8]}")
# ============ HEALTH ============
@api.get("/")
async def root():