From 02790018ef25b93fca36643c0249e08454f98465 Mon Sep 17 00:00:00 2001 From: ali Date: Thu, 19 Nov 2026 20:23:49 +0800 Subject: [PATCH] =?UTF-8?q?fix(tmdb):=20=E5=90=8C=E5=90=8D=E4=BD=9C?= =?UTF-8?q?=E5=93=81=E6=8C=89=E5=BA=93=E5=86=85=E5=88=86=E7=B1=BB=E6=B6=88?= =?UTF-8?q?=E6=AD=A7=EF=BC=88=E5=8A=A8=E6=BC=AB=E7=89=88/=E7=9C=9F?= =?UTF-8?q?=E4=BA=BA=E7=89=88=E8=8B=8D=E5=85=B0=E8=AF=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 苍兰诀在 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 --- media/src/services/parser.py | 3 +- media/src/services/tmdb.py | 59 +++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/media/src/services/parser.py b/media/src/services/parser.py index bd042fc..17dd483 100644 --- a/media/src/services/parser.py +++ b/media/src/services/parser.py @@ -139,6 +139,7 @@ class ParserService: tmdb = TMDBService.search_media( title, year, m_type, season_num, 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() if alt_title and alt_title.lower() != plain_title.lower(): 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 "" diff --git a/media/src/services/tmdb.py b/media/src/services/tmdb.py index 95138ff..6d48588 100644 --- a/media/src/services/tmdb.py +++ b/media/src/services/tmdb.py @@ -2,6 +2,7 @@ """TMDB 查询:优先用目录名里的 tmdbid,其次查本地缓存库,最后才发请求。""" import sys +from pathlib import Path import requests from requests.adapters import HTTPAdapter @@ -83,10 +84,12 @@ class TMDBService: # ------------------------------------------------------------ 对外入口 @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。 优先级:目录名里的 tmdbid → 缓存 lookup → 在线搜索。 + media_path 是待整理的媒体文件路径,仅用于「动漫/真人」分类消歧。 返回的实体字段见 core.tmdb_store.build_entity。 """ if not config.TMDB_API_KEY: @@ -99,9 +102,9 @@ class TMDBService: if known_tmdb_id: entity = cls._from_id(path, m_type, known_tmdb_id, title, year_s) 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: - 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: return None return cls._ensure_season(entity, season_num, year_s) @@ -126,13 +129,18 @@ class TMDBService: return entity @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) keys = store.lookup(db, mtype, title, year_s) entities = [db["entities"][k] for k in keys if k in db["entities"]] if not entities: return None + # 同名作品可能有多条(苍兰诀动漫版/真人版),缓存命中也要按所在分类过滤 + narrowed = cls._prefer_by_category(media_path, entities) + if narrowed: + entities = narrowed + if len(entities) == 1: mark = (mtype, store.normalize_title(title), year_s) if mark not in cls._notified_cache_hits: @@ -144,7 +152,7 @@ class TMDBService: return cls._choose(entities, title, year_s) @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) if search_key in cls._failed_searches: log.debug(f"⏭ 跳过本进程内已失败过的搜索: {title} ({year_s or '无年份'})") @@ -179,9 +187,17 @@ class TMDBService: if len(entities) == 1: chosen = entities[0] elif exact: - # 精确标题匹配后仍有多条(如「苍兰诀」真人版 + 动漫版)→ 才问 - log.info(f"🔀 「{title}」精确匹配到 {len(entities)} 个候选,需要确认") - chosen = cls._choose(entities, title, year_s) + # 精确标题匹配后仍有多条(如「苍兰诀」真人版 tv/130368 + 动漫版 tv/206534) + # 先用库里的分类目录消歧,剩下的才需要问 + 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: log.warning(f"⚠️ 「{title}」没有精确匹配,{len(entities)} 个候选取热度最高者" 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 def _choose(cls, entities, title, year_s): """候选并列时才走到这里:交互模式让用户选,否则取第一个(搜索热度顺序)。"""