perf(tmdb): 加进程内负缓存,失败的查询每次运行只发生一次
- 详情 404 的 tmdb id 记入 _dead_ids,后续文件直接跳过(DEBUG 日志), 不再逐集重复请求 - 搜索无结果的 (类型, 标题, 年份) 记入 _failed_searches,同一标题只搜一次 - 起因:知否知否 (DVD版) 的 [tmdbid-309718] 在 TMDB 已不存在(迁移时那 4 个 失效 id 之一),62 集每集都重走「详情404 + 中文搜 + 英文名搜」流程, 窗口被刷屏;修复后同一目录只产生 1 次 404 + 2 次搜索 - 控制台是 INFO 级,跳过日志为 DEBUG,只在日志文件留痕
This commit is contained in:
@@ -14,6 +14,10 @@ from core.logger import log
|
|||||||
|
|
||||||
class TMDBService:
|
class TMDBService:
|
||||||
_notified_cache_hits = set()
|
_notified_cache_hits = set()
|
||||||
|
# 本进程内的「负缓存」:同一个目录的每一集都会重复调用 search_media,
|
||||||
|
# 失败结果不记住的话,一个 60 集的剧会白跑 180 次请求。
|
||||||
|
_dead_ids = set() # 详情 404 的 tmdb id
|
||||||
|
_failed_searches = set() # (类型, 归一化标题, 年份) 搜索无结果
|
||||||
|
|
||||||
# ------------------------------------------------------------ HTTP
|
# ------------------------------------------------------------ HTTP
|
||||||
|
|
||||||
@@ -34,6 +38,14 @@ class TMDBService:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _fetch_details(cls, session, mtype: str, tmdb_id) -> dict | None:
|
def _fetch_details(cls, session, mtype: str, tmdb_id) -> dict | None:
|
||||||
|
try:
|
||||||
|
cache_key = (mtype, int(tmdb_id))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
if cache_key in cls._dead_ids:
|
||||||
|
log.debug(f"⏭ 跳过已确认失效的 TMDB id: {mtype}/{tmdb_id}")
|
||||||
|
return None
|
||||||
|
|
||||||
endpoint = "tv" if mtype == "tv" else "movie"
|
endpoint = "tv" if mtype == "tv" else "movie"
|
||||||
try:
|
try:
|
||||||
resp = session.get(f"https://api.themoviedb.org/3/{endpoint}/{tmdb_id}",
|
resp = session.get(f"https://api.themoviedb.org/3/{endpoint}/{tmdb_id}",
|
||||||
@@ -41,6 +53,11 @@ class TMDBService:
|
|||||||
except Exception as exc: # noqa: BLE001
|
except Exception as exc: # noqa: BLE001
|
||||||
log.warning(f"⚠️ TMDB 详情请求异常 [{mtype}/{tmdb_id}]: {exc}")
|
log.warning(f"⚠️ TMDB 详情请求异常 [{mtype}/{tmdb_id}]: {exc}")
|
||||||
return None
|
return None
|
||||||
|
if resp.status_code == 404:
|
||||||
|
# 目录名里的 tmdbid 已失效(TMDB 侧删除/合并),本次运行不再重试
|
||||||
|
cls._dead_ids.add(cache_key)
|
||||||
|
log.warning(f"⚠️ TMDB id 已失效(404),本次运行不再重试: {mtype}/{tmdb_id}")
|
||||||
|
return None
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
log.warning(f"⚠️ TMDB 详情获取失败 [{mtype}/{tmdb_id}]: HTTP {resp.status_code}")
|
log.warning(f"⚠️ TMDB 详情获取失败 [{mtype}/{tmdb_id}]: HTTP {resp.status_code}")
|
||||||
return None
|
return None
|
||||||
@@ -128,11 +145,17 @@ class TMDBService:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _from_search(cls, path, mtype, title, year_s):
|
def _from_search(cls, path, mtype, title, year_s):
|
||||||
|
search_key = (mtype, store.normalize_title(title), year_s)
|
||||||
|
if search_key in cls._failed_searches:
|
||||||
|
log.debug(f"⏭ 跳过本进程内已失败过的搜索: {title} ({year_s or '无年份'})")
|
||||||
|
return None
|
||||||
|
|
||||||
session = cls._get_session()
|
session = cls._get_session()
|
||||||
log.debug(f"🌐 请求 TMDB 搜索: {title} ({year_s or '无年份'})")
|
log.debug(f"🌐 请求 TMDB 搜索: {title} ({year_s or '无年份'})")
|
||||||
results = cls._search(session, title, year_s, mtype)
|
results = cls._search(session, title, year_s, mtype)
|
||||||
if not results:
|
if not results:
|
||||||
log.warning(f"⚠️ TMDB 未搜到: {title}")
|
cls._failed_searches.add(search_key)
|
||||||
|
log.warning(f"⚠️ TMDB 未搜到(本次运行不再重试): {title}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
target = store.normalize_title(title)
|
target = store.normalize_title(title)
|
||||||
@@ -150,6 +173,7 @@ class TMDBService:
|
|||||||
continue
|
continue
|
||||||
entities.append(store.build_entity(mtype, result["id"], payload))
|
entities.append(store.build_entity(mtype, result["id"], payload))
|
||||||
if not entities:
|
if not entities:
|
||||||
|
cls._failed_searches.add(search_key)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if len(entities) == 1:
|
if len(entities) == 1:
|
||||||
|
|||||||
Reference in New Issue
Block a user