feat(tmdb): Season 00 按每集播出年份命名
- 特别篇原来取 season_years['00'],那是 Season 00 整体的 air_date
(夏目是 2009,第一集是 3D 猫咪老师剧场),导致 2013/2014/2017 的
6 个 OVA 全被标成 2009
- 改为:遇到 S00 时拉一次 /tv/{id}/season/0,把每集年份存进实体的
special_years(如 {'06': '2013'}),命名时优先用本集年份
- 分季请求失败/404 时回落到季年份,不会变成空年份
- 实测:夏目 6 个 OVA 分别得到 2013 / 2014 / 2017 / 2017 / 2017 / 2017
This commit is contained in:
@@ -154,6 +154,24 @@ def season_years_from(payload: dict) -> dict[str, str]:
|
|||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def special_years_from(payload: dict) -> dict[str, str]:
|
||||||
|
"""从 /tv/{id}/season/{n} 的响应生成按集号的年份 {"06": "2013"}。
|
||||||
|
|
||||||
|
Season 00 整体只有一个 air_date(往往是特别篇第一集的年份),
|
||||||
|
对逐个特别篇没有意义,所以按集取。
|
||||||
|
"""
|
||||||
|
out: dict[str, str] = {}
|
||||||
|
for episode in payload.get("episodes") or []:
|
||||||
|
try:
|
||||||
|
num = str(int(episode.get("episode_number", 0))).zfill(2)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
continue
|
||||||
|
air = (episode.get("air_date") or "").strip()
|
||||||
|
if len(air) >= 4 and air[:4].isdigit():
|
||||||
|
out[num] = air[:4]
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def build_entity(mtype: str, tmdb_id, payload: dict) -> dict:
|
def build_entity(mtype: str, tmdb_id, payload: dict) -> dict:
|
||||||
"""把 TMDB 详情响应映射成本库的记录结构。"""
|
"""把 TMDB 详情响应映射成本库的记录结构。"""
|
||||||
localized = _first(payload, "title", "name") or ""
|
localized = _first(payload, "title", "name") or ""
|
||||||
|
|||||||
@@ -185,6 +185,11 @@ class ParserService:
|
|||||||
if season_years:
|
if season_years:
|
||||||
episode_year = season_years.get(season_num, fit_year)
|
episode_year = season_years.get(season_num, fit_year)
|
||||||
|
|
||||||
|
# 特别篇按「本集」的播出年份:Season 00 的整体 air_date 往往是第一篇的年份
|
||||||
|
special_years = (tmdb or {}).get("special_years") or {}
|
||||||
|
if special_years.get(ep):
|
||||||
|
episode_year = special_years[ep]
|
||||||
|
|
||||||
prefix = f"{media_name}.S{season_num}E{ep}"
|
prefix = f"{media_name}.S{season_num}E{ep}"
|
||||||
if episode_year:
|
if episode_year:
|
||||||
prefix = f"{prefix}.{episode_year}"
|
prefix = f"{prefix}.{episode_year}"
|
||||||
|
|||||||
@@ -297,6 +297,13 @@ class TMDBService:
|
|||||||
if entity.get("type") != "tv" or not season_num:
|
if entity.get("type") != "tv" or not season_num:
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
# Season 00(特别篇)不走季年份:整体 air_date 往往是第一篇的年份,
|
||||||
|
# 逐个特别篇要用各自的播出年份,所以单独拉 season/0 的分集数据。
|
||||||
|
if str(season_num).zfill(2) == "00":
|
||||||
|
if not (entity.get("special_years") or {}):
|
||||||
|
cls._load_special_years(entity)
|
||||||
|
return entity
|
||||||
|
|
||||||
season_years = entity.setdefault("season_years", {})
|
season_years = entity.setdefault("season_years", {})
|
||||||
if season_num in season_years:
|
if season_num in season_years:
|
||||||
return entity
|
return entity
|
||||||
@@ -318,3 +325,41 @@ class TMDBService:
|
|||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
log.warning(f"⚠️ 补季写入失败: {exc}")
|
log.warning(f"⚠️ 补季写入失败: {exc}")
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _load_special_years(cls, entity):
|
||||||
|
"""拉一次 /tv/{id}/season/0,把每个特别篇的播出年份存进实体。"""
|
||||||
|
log.info("🔄 缓存缺少 Season 00 的分集年份,按需补拉特别篇详情...")
|
||||||
|
session = cls._get_session()
|
||||||
|
payload = cls._fetch_season(session, "tv", entity["tmdb_id"], 0)
|
||||||
|
entity["special_years"] = store.special_years_from(payload) if payload else {}
|
||||||
|
if not entity["special_years"]:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
with store.transaction(config.TMDB_CACHE_PATH) as db:
|
||||||
|
store.put_entity(db, entity)
|
||||||
|
except OSError as exc:
|
||||||
|
log.warning(f"⚠️ 特别篇年份写入失败: {exc}")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _fetch_season(cls, session, mtype, tmdb_id, season_number) -> dict | None:
|
||||||
|
try:
|
||||||
|
key = (mtype, int(tmdb_id), int(season_number))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return None
|
||||||
|
if key in cls._dead_ids:
|
||||||
|
return None
|
||||||
|
url = f"https://api.themoviedb.org/3/{mtype}/{tmdb_id}/season/{season_number}"
|
||||||
|
try:
|
||||||
|
resp = session.get(url, params=cls._params(), timeout=config.TMDB_TIMEOUT)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
log.warning(f"⚠️ TMDB 分季请求异常 [{mtype}/{tmdb_id} S{season_number}]: {exc}")
|
||||||
|
return None
|
||||||
|
if resp.status_code == 404:
|
||||||
|
cls._dead_ids.add(key)
|
||||||
|
log.warning(f"⚠️ TMDB 分季不存在(404): {mtype}/{tmdb_id} S{season_number}")
|
||||||
|
return None
|
||||||
|
if resp.status_code != 200:
|
||||||
|
log.warning(f"⚠️ TMDB 分季获取失败 [{mtype}/{tmdb_id} S{season_number}]: HTTP {resp.status_code}")
|
||||||
|
return None
|
||||||
|
return resp.json()
|
||||||
|
|||||||
Reference in New Issue
Block a user