fix(tmdb): 网络异常不再当作未搜到,异常日志打码
This commit is contained in:
+36
-8
@@ -1,6 +1,7 @@
|
|||||||
# src/services/tmdb.py
|
# src/services/tmdb.py
|
||||||
"""TMDB 查询:优先用目录名里的 tmdbid,其次查本地缓存库,最后才发请求。"""
|
"""TMDB 查询:优先用目录名里的 tmdbid,其次查本地缓存库,最后才发请求。"""
|
||||||
|
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
@@ -13,13 +14,26 @@ from core import tmdb_store as store
|
|||||||
from core.logger import log
|
from core.logger import log
|
||||||
|
|
||||||
|
|
||||||
|
# requests 的异常串里带完整 URL,含 api_key;写日志前统一打码
|
||||||
|
_API_KEY_RE = re.compile(r"api_key=[^&\s'\")]+", re.I)
|
||||||
|
|
||||||
|
|
||||||
|
def _redact(text) -> str:
|
||||||
|
return _API_KEY_RE.sub("api_key=***", str(text))
|
||||||
|
|
||||||
|
|
||||||
|
class TMDBUnavailable(RuntimeError):
|
||||||
|
"""TMDB 网络/HTTP 不可用:无法判断标题是否存在,不能当成「没搜到」缓存。"""
|
||||||
|
|
||||||
|
|
||||||
class TMDBService:
|
class TMDBService:
|
||||||
_notified_cache_hits = set()
|
_notified_cache_hits = set()
|
||||||
# 本进程内的「负缓存」:同一个目录的每一集都会重复调用 search_media,
|
# 本进程内的「负缓存」:同一个目录的每一集都会重复调用 search_media,
|
||||||
# 失败结果不记住的话,一个 60 集的剧会白跑 180 次请求。
|
# 失败结果不记住的话,一个 60 集的剧会白跑 180 次请求。
|
||||||
_dead_ids = set() # 详情 404 的 tmdb id
|
_dead_ids = set() # 详情 404 的 tmdb id
|
||||||
_special_years_checked = set() # 本进程内查过 S00 的剧:无特别篇也记住,避免每集重拉
|
_special_years_checked = set() # 本进程内查过 S00 的剧:无特别篇也记住,避免每集重拉
|
||||||
_failed_searches = set() # (类型, 归一化标题, 年份) 搜索无结果
|
_failed_searches = set() # (类型, 归一化标题, 年份) 搜索无结果(不含网络异常)
|
||||||
|
_network_errors = 0 # 调用链里的网络失败次数,用来区分「没搜到」和「连不上」
|
||||||
_id_hits_notified = set() # 目录名 tmdbid 命中:每个 id 只记一次日志
|
_id_hits_notified = set() # 目录名 tmdbid 命中:每个 id 只记一次日志
|
||||||
_id_entity_cache = {} # 进程内实体缓存,避免每集重读缓存文件
|
_id_entity_cache = {} # 进程内实体缓存,避免每集重读缓存文件
|
||||||
|
|
||||||
@@ -55,7 +69,8 @@ class TMDBService:
|
|||||||
resp = session.get(f"{config.TMDB_BASE_URL}/{endpoint}/{tmdb_id}",
|
resp = session.get(f"{config.TMDB_BASE_URL}/{endpoint}/{tmdb_id}",
|
||||||
params=cls._params(), timeout=config.TMDB_TIMEOUT)
|
params=cls._params(), timeout=config.TMDB_TIMEOUT)
|
||||||
except Exception as exc: # noqa: BLE001
|
except Exception as exc: # noqa: BLE001
|
||||||
log.warning(f"⚠️ TMDB 详情请求异常 [{mtype}/{tmdb_id}]: {exc}")
|
cls._network_errors += 1
|
||||||
|
log.warning(f"⚠️ TMDB 详情请求异常 [{mtype}/{tmdb_id}]: {_redact(exc)}")
|
||||||
return None
|
return None
|
||||||
if resp.status_code == 404:
|
if resp.status_code == 404:
|
||||||
# 目录名里的 tmdbid 已失效(TMDB 侧删除/合并),本次运行不再重试
|
# 目录名里的 tmdbid 已失效(TMDB 侧删除/合并),本次运行不再重试
|
||||||
@@ -63,6 +78,7 @@ class TMDBService:
|
|||||||
log.warning(f"⚠️ TMDB id 已失效(404),本次运行不再重试: {mtype}/{tmdb_id}")
|
log.warning(f"⚠️ TMDB id 已失效(404),本次运行不再重试: {mtype}/{tmdb_id}")
|
||||||
return None
|
return None
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
|
cls._network_errors += 1
|
||||||
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
|
||||||
return resp.json()
|
return resp.json()
|
||||||
@@ -77,11 +93,11 @@ class TMDBService:
|
|||||||
resp = session.get(f"{config.TMDB_BASE_URL}/search/{endpoint}",
|
resp = session.get(f"{config.TMDB_BASE_URL}/search/{endpoint}",
|
||||||
params=cls._params(extra), timeout=config.TMDB_TIMEOUT)
|
params=cls._params(extra), timeout=config.TMDB_TIMEOUT)
|
||||||
except Exception as exc: # noqa: BLE001
|
except Exception as exc: # noqa: BLE001
|
||||||
log.warning(f"⚠️ TMDB 搜索异常 [{title}]: {exc}")
|
cls._network_errors += 1
|
||||||
return []
|
raise TMDBUnavailable(_redact(f"{type(exc).__name__}: {exc}")) from exc
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
log.warning(f"⚠️ TMDB 搜索失败 [{title}]: HTTP {resp.status_code}")
|
cls._network_errors += 1
|
||||||
return []
|
raise TMDBUnavailable(f"HTTP {resp.status_code}")
|
||||||
return resp.json().get("results") or []
|
return resp.json().get("results") or []
|
||||||
|
|
||||||
# ------------------------------------------------------------ 对外入口
|
# ------------------------------------------------------------ 对外入口
|
||||||
@@ -176,7 +192,13 @@ class TMDBService:
|
|||||||
|
|
||||||
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)
|
errors_before = cls._network_errors
|
||||||
|
try:
|
||||||
|
results = cls._search(session, title, year_s, mtype)
|
||||||
|
except TMDBUnavailable as exc:
|
||||||
|
log.warning(f"⚠️ TMDB 连不上(网络异常,本次跳过;下次会重试): "
|
||||||
|
f"{title} -> {exc}")
|
||||||
|
return None
|
||||||
if not results:
|
if not results:
|
||||||
cls._failed_searches.add(search_key)
|
cls._failed_searches.add(search_key)
|
||||||
log.warning(f"⚠️ TMDB 未搜到(本次运行不再重试): {title}")
|
log.warning(f"⚠️ TMDB 未搜到(本次运行不再重试): {title}")
|
||||||
@@ -197,7 +219,11 @@ 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:
|
||||||
|
if cls._network_errors > errors_before:
|
||||||
|
log.warning(f"⚠️ TMDB 连不上(网络异常,本次跳过;下次会重试): {title}")
|
||||||
|
return None
|
||||||
cls._failed_searches.add(search_key)
|
cls._failed_searches.add(search_key)
|
||||||
|
log.warning(f"⚠️ TMDB 未搜到(本次运行不再重试): {title}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if len(entities) == 1:
|
if len(entities) == 1:
|
||||||
@@ -362,13 +388,15 @@ class TMDBService:
|
|||||||
try:
|
try:
|
||||||
resp = session.get(url, params=cls._params(), timeout=config.TMDB_TIMEOUT)
|
resp = session.get(url, params=cls._params(), timeout=config.TMDB_TIMEOUT)
|
||||||
except Exception as exc: # noqa: BLE001
|
except Exception as exc: # noqa: BLE001
|
||||||
log.warning(f"⚠️ TMDB 分季请求异常 [{mtype}/{tmdb_id} S{season_number}]: {exc}")
|
cls._network_errors += 1
|
||||||
|
log.warning(f"⚠️ TMDB 分季请求异常 [{mtype}/{tmdb_id} S{season_number}]: {_redact(exc)}")
|
||||||
return None
|
return None
|
||||||
if resp.status_code == 404:
|
if resp.status_code == 404:
|
||||||
cls._dead_ids.add(key)
|
cls._dead_ids.add(key)
|
||||||
log.warning(f"⚠️ TMDB 分季不存在(404): {mtype}/{tmdb_id} S{season_number}")
|
log.warning(f"⚠️ TMDB 分季不存在(404): {mtype}/{tmdb_id} S{season_number}")
|
||||||
return None
|
return None
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
|
cls._network_errors += 1
|
||||||
log.warning(f"⚠️ TMDB 分季获取失败 [{mtype}/{tmdb_id} S{season_number}]: HTTP {resp.status_code}")
|
log.warning(f"⚠️ TMDB 分季获取失败 [{mtype}/{tmdb_id} S{season_number}]: HTTP {resp.status_code}")
|
||||||
return None
|
return None
|
||||||
return resp.json()
|
return resp.json()
|
||||||
|
|||||||
Reference in New Issue
Block a user