fix(tmdb): 同名作品按库内分类消歧(动漫版/真人版苍兰诀)

- 苍兰诀在 TMDB 有两条同名条目:tv/130368 真人剧(1 季 36 集,剧情)、
  tv/206534 动画(2 季 50 集,类型含 Animation),两者中文名与年份都相同,
  精确匹配后并列,非交互模式直接取了热度更高的真人版,导致动漫目录被命名为
  [tmdbid-130368]
- 新增分类偏好:路径含 动漫节目/动漫电影 时优先 genre 16(动画),
  含 电视节目/电影/综艺 时优先非动画;判断不出则维持原逻辑
- search_media 增加 media_path 参数(原 path 是缓存文件路径,拿不到分类),
  缓存命中与在线搜索两条路都套用该偏好
- 实测:动漫目录 → tmdbid-206534(S1 2022 / S2 2025),真人目录仍为 tmdbid-130368
This commit is contained in:
ali
2026-11-19 20:23:49 +08:00
parent 43ba5302e0
commit 02790018ef
2 changed files with 53 additions and 9 deletions
+2 -1
View File
@@ -139,6 +139,7 @@ class ParserService:
tmdb = TMDBService.search_media( tmdb = TMDBService.search_media(
title, year, m_type, season_num, title, year, m_type, season_num,
known_tmdb_id=ParserService._known_tmdb_id(f_path, m_type), known_tmdb_id=ParserService._known_tmdb_id(f_path, m_type),
media_path=f_path,
) )
# 中文目录名搜不到时,用文件名里的英文原名再试一次 # 中文目录名搜不到时,用文件名里的英文原名再试一次
@@ -147,7 +148,7 @@ class ParserService:
plain_title = title.replace(".", " ").strip() plain_title = title.replace(".", " ").strip()
if alt_title and alt_title.lower() != plain_title.lower(): if alt_title and alt_title.lower() != plain_title.lower():
log.info(f"🔁 中文标题未命中,改用文件名标题重试: {alt_title}") log.info(f"🔁 中文标题未命中,改用文件名标题重试: {alt_title}")
tmdb = TMDBService.search_media(alt_title, year, m_type, season_num) tmdb = TMDBService.search_media(alt_title, year, m_type, season_num, media_path=f_path)
# 文件名主名:优先用媒体库里的安全标题(白名单清洗 + 截断) # 文件名主名:优先用媒体库里的安全标题(白名单清洗 + 截断)
media_name = (tmdb or {}).get("media_title") or "" media_name = (tmdb or {}).get("media_title") or ""
+51 -8
View File
@@ -2,6 +2,7 @@
"""TMDB 查询:优先用目录名里的 tmdbid,其次查本地缓存库,最后才发请求。""" """TMDB 查询:优先用目录名里的 tmdbid,其次查本地缓存库,最后才发请求。"""
import sys import sys
from pathlib import Path
import requests import requests
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
@@ -83,10 +84,12 @@ class TMDBService:
# ------------------------------------------------------------ 对外入口 # ------------------------------------------------------------ 对外入口
@classmethod @classmethod
def search_media(cls, title, year=None, m_type="movie", season_num=None, known_tmdb_id=None): def search_media(cls, title, year=None, m_type="movie", season_num=None, known_tmdb_id=None,
media_path=None):
"""取 TMDB 实体,失败返回 None。 """取 TMDB 实体,失败返回 None。
优先级:目录名里的 tmdbid → 缓存 lookup → 在线搜索。 优先级:目录名里的 tmdbid → 缓存 lookup → 在线搜索。
media_path 是待整理的媒体文件路径,仅用于「动漫/真人」分类消歧。
返回的实体字段见 core.tmdb_store.build_entity。 返回的实体字段见 core.tmdb_store.build_entity。
""" """
if not config.TMDB_API_KEY: if not config.TMDB_API_KEY:
@@ -99,9 +102,9 @@ class TMDBService:
if known_tmdb_id: if known_tmdb_id:
entity = cls._from_id(path, m_type, known_tmdb_id, title, year_s) entity = cls._from_id(path, m_type, known_tmdb_id, title, year_s)
if entity is None: if entity is None:
entity = cls._from_lookup(path, m_type, title, year_s) entity = cls._from_lookup(path, m_type, title, year_s, media_path)
if entity is None: if entity is None:
entity = cls._from_search(path, m_type, title, year_s) entity = cls._from_search(path, m_type, title, year_s, media_path)
if entity is None: if entity is None:
return None return None
return cls._ensure_season(entity, season_num, year_s) return cls._ensure_season(entity, season_num, year_s)
@@ -126,13 +129,18 @@ class TMDBService:
return entity return entity
@classmethod @classmethod
def _from_lookup(cls, path, mtype, title, year_s): def _from_lookup(cls, path, mtype, title, year_s, media_path=None):
db = store.snapshot(path) db = store.snapshot(path)
keys = store.lookup(db, mtype, title, year_s) keys = store.lookup(db, mtype, title, year_s)
entities = [db["entities"][k] for k in keys if k in db["entities"]] entities = [db["entities"][k] for k in keys if k in db["entities"]]
if not entities: if not entities:
return None return None
# 同名作品可能有多条(苍兰诀动漫版/真人版),缓存命中也要按所在分类过滤
narrowed = cls._prefer_by_category(media_path, entities)
if narrowed:
entities = narrowed
if len(entities) == 1: if len(entities) == 1:
mark = (mtype, store.normalize_title(title), year_s) mark = (mtype, store.normalize_title(title), year_s)
if mark not in cls._notified_cache_hits: if mark not in cls._notified_cache_hits:
@@ -144,7 +152,7 @@ class TMDBService:
return cls._choose(entities, title, year_s) return cls._choose(entities, title, year_s)
@classmethod @classmethod
def _from_search(cls, path, mtype, title, year_s): def _from_search(cls, path, mtype, title, year_s, media_path=None):
search_key = (mtype, store.normalize_title(title), year_s) search_key = (mtype, store.normalize_title(title), year_s)
if search_key in cls._failed_searches: if search_key in cls._failed_searches:
log.debug(f"⏭ 跳过本进程内已失败过的搜索: {title} ({year_s or '无年份'})") log.debug(f"⏭ 跳过本进程内已失败过的搜索: {title} ({year_s or '无年份'})")
@@ -179,9 +187,17 @@ class TMDBService:
if len(entities) == 1: if len(entities) == 1:
chosen = entities[0] chosen = entities[0]
elif exact: elif exact:
# 精确标题匹配后仍有多条(如「苍兰诀」真人版 + 动漫版)→ 才问 # 精确标题匹配后仍有多条(如「苍兰诀」真人版 tv/130368 + 动漫版 tv/206534)
log.info(f"🔀 「{title}」精确匹配到 {len(entities)} 个候选,需要确认") # 先用库里的分类目录消歧,剩下的才需要问
chosen = cls._choose(entities, title, year_s) narrowed = cls._prefer_by_category(media_path, entities)
if narrowed and len(narrowed) == 1:
log.info(f"🔀 「{title}」有 {len(entities)} 个同名候选,按所在分类锁定 "
f"tmdbid-{narrowed[0]['tmdb_id']}")
chosen = narrowed[0]
else:
pool_entities = narrowed or entities
log.info(f"🔀 「{title}」精确匹配到 {len(pool_entities)} 个候选,需要确认")
chosen = cls._choose(pool_entities, title, year_s)
else: else:
log.warning(f"⚠️ 「{title}」没有精确匹配,{len(entities)} 个候选取热度最高者" log.warning(f"⚠️ 「{title}」没有精确匹配,{len(entities)} 个候选取热度最高者"
f"(tmdbid-{entities[0]['tmdb_id']}),如有误请核对") f"(tmdbid-{entities[0]['tmdb_id']}),如有误请核对")
@@ -192,6 +208,33 @@ class TMDBService:
# ------------------------------------------------------------ 选择与落库 # ------------------------------------------------------------ 选择与落库
# 库里按「动漫 / 真人」分目录,同名候选可用所在分类消歧:
# 苍兰诀就是典型——动漫版 tv/206534(2 季 50 集)与真人版 tv/130368(1 季 36 集)同名。
ANIME_DIRS = {"动漫节目", "动漫电影"}
LIVE_DIRS = {"电视节目", "电影", "综艺"}
ANIME_GENRE_ID = 16
@classmethod
def _prefer_by_category(cls, path, entities):
"""按文件所在分类目录偏好动画/真人;判断不出返回 None。"""
try:
parts = set(Path(path).parts)
except (TypeError, ValueError):
return None
if parts & cls.ANIME_DIRS:
want_anime = True
elif parts & cls.LIVE_DIRS:
want_anime = False
else:
return None
def is_anime(entity):
genres = entity.get("details", {}).get("genres") or []
return cls.ANIME_GENRE_ID in {g.get("id") for g in genres}
picked = [e for e in entities if is_anime(e) == want_anime]
return picked or None
@classmethod @classmethod
def _choose(cls, entities, title, year_s): def _choose(cls, entities, title, year_s):
"""候选并列时才走到这里:交互模式让用户选,否则取第一个(搜索热度顺序)。""" """候选并列时才走到这里:交互模式让用户选,否则取第一个(搜索热度顺序)。"""