mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-30 07:56:21 +08:00
Merge pull request #7468 from jxxghp/fix/yearless-tv-alias-evidence
fix(search): verify year evidence for TV alias conflicts
This commit is contained in:
@@ -971,15 +971,12 @@ class TorrentHelper:
|
||||
torrent_meta: MetaBase,
|
||||
) -> bool:
|
||||
"""
|
||||
判断无年份目标是否只通过别名命中,需进一步核验媒体身份。
|
||||
判断是否需要对仅靠别名命中的剧集再识别一次。
|
||||
|
||||
目标和资源只有一侧缺年份时才消歧。两侧都有年份时沿用普通年份校验,
|
||||
两侧都缺年份时按别名兜底,避免用无日期约束的同名查询排除资源。
|
||||
"""
|
||||
if (
|
||||
mediainfo.type != MediaType.TV
|
||||
or (
|
||||
getattr(mediainfo, "year", None)
|
||||
and getattr(torrent_meta, "year", None)
|
||||
)
|
||||
):
|
||||
if mediainfo.type != MediaType.TV or bool(mediainfo.year) == bool(torrent_meta.year):
|
||||
return False
|
||||
media_titles = {
|
||||
text_tools.normalize_upper(value)
|
||||
@@ -1006,6 +1003,23 @@ class TorrentHelper:
|
||||
meta_names.intersection(media_titles)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _matches_torrent_year(mediainfo: MediaInfo, torrent_meta: MetaBase) -> bool:
|
||||
"""
|
||||
判断媒体已知日期是否能印证种子标注的年份。
|
||||
|
||||
优先使用资源覆盖的季的首播年份。查不到对应季的日期时才使用整剧首播年,
|
||||
不拿其他季的年份证明这是同一条资源。
|
||||
"""
|
||||
torrent_year = str(torrent_meta.year or "")[:4]
|
||||
if not torrent_year:
|
||||
return False
|
||||
season_years = mediainfo.season_years or {}
|
||||
known_years = {
|
||||
str(year)[:4] for season in torrent_meta.season_list if (year := season_years.get(season))
|
||||
}
|
||||
return torrent_year in (known_years or {str(mediainfo.year or "")[:4]})
|
||||
|
||||
@staticmethod
|
||||
def match_same_work_evidence(
|
||||
target_mediainfo: MediaInfo,
|
||||
@@ -1013,13 +1027,15 @@ class TorrentHelper:
|
||||
torrent_meta: MetaBase,
|
||||
) -> Tuple[bool, str]:
|
||||
"""
|
||||
判断无年份别名资源能否按订阅目标处理,只有明确属于另一部作品时才拒绝。
|
||||
判断缺少年份的别名命中能否按目标作品处理。
|
||||
|
||||
同名撞车偶尔下错的代价远低于资源一直下不下来:原名或语言不同、候选条目没有首播日期
|
||||
(多为未播出或残缺的重复条目)都不足以否定目标,按目标处理。若目标没有年份,但候选与目标
|
||||
来自同一媒体源且媒体编号明确不同,则可以确认是另一部作品,应拒绝;对没有可比身份的候选
|
||||
仍保留原有的宽松兜底。TMDB 的电影与电视剧数字 ID 不可跨类型比较。
|
||||
当目标缺少整剧首播年时,即使按名字查到不同编号,也需要候选的日期符合资源年份,
|
||||
才能把它当作另一部作品。目标有年份时,保留无年份资源原有的比较规则。
|
||||
TMDB 的电影与电视剧数字 ID 不可跨类型比较。
|
||||
|
||||
:param target_mediainfo: 搜索或订阅指定的目标
|
||||
:param candidate_mediainfo: 按资源名称及日期尝试识别出的候选
|
||||
:param torrent_meta: 资源自身解析出的名称、年份和季集
|
||||
:return: 是否按目标处理,以及给用户看的依据或拒绝原因
|
||||
"""
|
||||
target_identity = resolve_media_identity(media=target_mediainfo)
|
||||
@@ -1031,17 +1047,17 @@ class TorrentHelper:
|
||||
return True, "识别结果就是订阅的作品"
|
||||
|
||||
candidate_year = str(candidate_mediainfo.year or "")[:4]
|
||||
if not candidate_year:
|
||||
return True, f"按名字查到的《{candidate_mediainfo.title}》没有首播日期,不能说明是另一部作品"
|
||||
|
||||
target_years = {
|
||||
str(year)[:4]
|
||||
for year in [target_mediainfo.year, *(target_mediainfo.season_years or {}).values()]
|
||||
if year
|
||||
}
|
||||
torrent_year = str(torrent_meta.year or "")[:4]
|
||||
if candidate_year in target_years or (torrent_year and torrent_year in target_years):
|
||||
return True, "年份与订阅的作品一致"
|
||||
if target_mediainfo.year:
|
||||
if not candidate_year:
|
||||
return True, f"按名字查到的《{candidate_mediainfo.title}》没有首播日期,不能说明是另一部作品"
|
||||
target_years = {
|
||||
str(year)[:4]
|
||||
for year in [target_mediainfo.year, *(target_mediainfo.season_years or {}).values()]
|
||||
if year
|
||||
}
|
||||
if candidate_year in target_years or (torrent_year and torrent_year in target_years):
|
||||
return True, "年份与订阅的作品一致"
|
||||
|
||||
for field in ("tmdb_id", "imdb_id", "tvdb_id", "douban_id", "bangumi_id"):
|
||||
if field == "tmdb_id" and not same_type:
|
||||
@@ -1051,19 +1067,22 @@ class TorrentHelper:
|
||||
if target_id and candidate_id and str(target_id) == str(candidate_id):
|
||||
return True, "与订阅的作品有相同的媒体编号"
|
||||
|
||||
if not target_years:
|
||||
if not target_mediainfo.year:
|
||||
if TorrentHelper._matches_torrent_year(target_mediainfo, torrent_meta):
|
||||
return True, "资源标注的年份与订阅的作品一致"
|
||||
if (
|
||||
same_type
|
||||
and all(target_identity)
|
||||
and all(candidate_identity)
|
||||
and target_identity[0] == candidate_identity[0]
|
||||
and target_identity != candidate_identity
|
||||
and TorrentHelper._matches_torrent_year(candidate_mediainfo, torrent_meta)
|
||||
):
|
||||
return False, (
|
||||
f"按名字查到的是另一部作品《{candidate_mediainfo.title}》({candidate_year}),"
|
||||
f"按名字查到的《{candidate_mediainfo.title}》与资源年份({torrent_year})一致,"
|
||||
f"与订阅的《{target_mediainfo.title}》媒体编号不同"
|
||||
)
|
||||
return True, "订阅的作品没有年份可以比较"
|
||||
return True, "没有找到资源年份吻合且媒体编号不同的作品"
|
||||
return False, (
|
||||
f"按名字查到的是另一部作品《{candidate_mediainfo.title}》({candidate_year}),"
|
||||
f"和订阅的《{target_mediainfo.title}》({target_mediainfo.year})首播年份不同"
|
||||
|
||||
@@ -25,7 +25,8 @@ from app.schemas.media import resolve_media_identity
|
||||
from app.schemas.types import MediaSource, MediaType, ProgressKey, SystemConfigKey
|
||||
|
||||
SiteKey = Tuple[Optional[int], Optional[str]]
|
||||
DisambiguationCache = Dict[Tuple[str, str, str], Optional[MediaInfo]]
|
||||
DisambiguationKey = Tuple[str, str, str, Optional[int], Optional[int]]
|
||||
DisambiguationCache = Dict[DisambiguationKey, Optional[MediaInfo]]
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
@@ -159,11 +160,11 @@ def _torrent_meta(torrent: TorrentInfo, custom_words: List[str], mediainfo: Medi
|
||||
return meta
|
||||
|
||||
|
||||
def _disambiguation_key(meta: MetaBase) -> Tuple[str, str, str]:
|
||||
def _disambiguation_key(meta: MetaBase) -> DisambiguationKey:
|
||||
"""
|
||||
构造同名候选识别缓存键。
|
||||
按名称、年份及季范围缓存识别,避免同名同年的不同季互相复用结果。
|
||||
"""
|
||||
return meta.cn_name or "", meta.en_name or "", str(meta.year or "")
|
||||
return meta.cn_name or "", meta.en_name or "", str(meta.year or ""), meta.begin_season, meta.end_season
|
||||
|
||||
|
||||
def _same_work_matched(
|
||||
@@ -173,16 +174,14 @@ def _same_work_matched(
|
||||
cache: DisambiguationCache,
|
||||
) -> bool:
|
||||
"""
|
||||
无年份资源只靠别名对上订阅时,按名字再识别一次;只有明确属于另一部作品才跳过。
|
||||
|
||||
识别不到不算反证:宁可偶尔下错同名作品,也不能让资源一直下不下来。
|
||||
目标或资源缺少年份时核对别名命中,只有明确属于另一部作品才跳过。
|
||||
"""
|
||||
key = _disambiguation_key(torrent_meta)
|
||||
if key not in cache:
|
||||
cache[key] = MediaChain().recognize_by_meta(torrent_meta, obtain_images=False)
|
||||
candidate = cache[key]
|
||||
if not candidate:
|
||||
logger.debug(f"{torrent.site_name} - {torrent.title} 资源名没有年份,按名字也识别不到作品,按订阅的作品处理")
|
||||
logger.debug(f"{torrent.site_name} - {torrent.title} 按名称未识别到作品,保留别名匹配结果")
|
||||
return True
|
||||
matched, evidence = TorrentHelper.match_same_work_evidence(
|
||||
target_mediainfo=mediainfo,
|
||||
@@ -190,9 +189,9 @@ def _same_work_matched(
|
||||
torrent_meta=torrent_meta,
|
||||
)
|
||||
if matched:
|
||||
logger.debug(f"{torrent.site_name} - {torrent.title} 资源名没有年份,{evidence},按订阅的作品处理")
|
||||
logger.debug(f"{torrent.site_name} - {torrent.title},{evidence},保留别名匹配结果")
|
||||
else:
|
||||
logger.info(f"跳过 {torrent.title}({torrent.site_name}):资源名没有年份,{evidence}")
|
||||
logger.info(f"跳过 {torrent.title}({torrent.site_name}):{evidence}")
|
||||
return matched
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from app.chain.search import SearchChain
|
||||
from app.chain.search import result as search_module
|
||||
from app.domain.context import MediaInfo, TorrentInfo
|
||||
@@ -113,9 +115,12 @@ def test_exact_search_reuses_disambiguation_for_same_parsed_title(monkeypatch):
|
||||
media_chain.recognize_by_meta.assert_called_once()
|
||||
|
||||
|
||||
def test_exact_search_rejects_yearful_wrong_work_for_yearless_alias(monkeypatch):
|
||||
"""目标缺少年份时,带年份的错误英文别名资源也必须经过消歧。"""
|
||||
target = MediaInfo(
|
||||
@pytest.fixture
|
||||
def yearless_evidence():
|
||||
"""
|
||||
构造有明确媒体编号、尚缺首播日期的《铁证》订阅目标。
|
||||
"""
|
||||
return MediaInfo(
|
||||
media_source=MediaSource.TMDB,
|
||||
media_id="304086",
|
||||
tmdb_id=304086,
|
||||
@@ -125,6 +130,25 @@ def test_exact_search_rejects_yearful_wrong_work_for_yearless_alias(monkeypatch)
|
||||
type=MediaType.TV,
|
||||
year=None,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("season", "resource_year", "candidate_year", "season_years", "kept"),
|
||||
[
|
||||
pytest.param(1, "2023", "2023", {}, False, id="other-work-same-year"),
|
||||
pytest.param(1, "2026", "2023", {}, True, id="fallback-other-year"),
|
||||
pytest.param(2, "2026", "2023", {2: "2026"}, False, id="other-work-same-season-year"),
|
||||
pytest.param(2, "2026", None, {2: "2026"}, False, id="season-year-only"),
|
||||
pytest.param(2, "2026", "2023", {3: "2026"}, True, id="unrelated-season-year"),
|
||||
pytest.param(2, "2026", "2026", {2: "2027"}, True, id="known-season-year-takes-precedence"),
|
||||
],
|
||||
)
|
||||
def test_exact_search_checks_candidate_year_for_yearless_alias(
|
||||
monkeypatch, yearless_evidence, season, resource_year, candidate_year, season_years, kept,
|
||||
):
|
||||
"""
|
||||
只有二次识别候选也符合资源年份时,不同媒体编号才足以拒绝别名资源。
|
||||
"""
|
||||
candidate = MediaInfo(
|
||||
media_source=MediaSource.TMDB,
|
||||
media_id="330828",
|
||||
@@ -132,61 +156,90 @@ def test_exact_search_rejects_yearful_wrong_work_for_yearless_alias(monkeypatch)
|
||||
title="证词",
|
||||
original_title="证词",
|
||||
type=MediaType.TV,
|
||||
year="2023",
|
||||
year=candidate_year,
|
||||
season_years=season_years,
|
||||
original_language="zh",
|
||||
)
|
||||
torrent = TorrentInfo(
|
||||
title="Evidence S01 2023 1080p WEB-DL H264 AAC",
|
||||
title=f"Evidence S{season:02d} {resource_year} 1080p WEB-DL H264 AAC",
|
||||
site_name="测试站点",
|
||||
category=MediaType.TV.value,
|
||||
)
|
||||
media_chain = Mock()
|
||||
media_chain.recognize_by_meta.return_value = candidate
|
||||
monkeypatch.setattr(search_module, "MediaChain", Mock(return_value=media_chain))
|
||||
monkeypatch.setattr(
|
||||
search_module.TorrentHelper,
|
||||
"sort_torrents",
|
||||
staticmethod(lambda contexts: contexts),
|
||||
)
|
||||
chain = object.__new__(SearchChain)
|
||||
contexts, recognize = _parse_evidence_resources(monkeypatch, yearless_evidence, [torrent], candidate)
|
||||
|
||||
contexts = chain._parse_result(
|
||||
torrents=[torrent],
|
||||
mediainfo=target,
|
||||
rule_groups=[],
|
||||
)
|
||||
|
||||
assert contexts == []
|
||||
media_chain.recognize_by_meta.assert_called_once()
|
||||
assert bool(contexts) is kept
|
||||
recognize.assert_called_once()
|
||||
|
||||
|
||||
def test_exact_search_accepts_yearless_alias_when_identity_is_confirmed(monkeypatch):
|
||||
"""目标缺少年份时,二次识别确认同一媒体仍可保留资源。"""
|
||||
target = MediaInfo(
|
||||
media_source=MediaSource.TMDB,
|
||||
media_id="304086",
|
||||
tmdb_id=304086,
|
||||
title="铁证",
|
||||
original_title="铁证",
|
||||
names=["Evidence"],
|
||||
type=MediaType.TV,
|
||||
year=None,
|
||||
)
|
||||
candidate = MediaInfo(
|
||||
media_source=MediaSource.TMDB,
|
||||
media_id="304086",
|
||||
tmdb_id=304086,
|
||||
title="铁证",
|
||||
original_title="铁证",
|
||||
type=MediaType.TV,
|
||||
)
|
||||
def test_exact_search_accepts_yearless_alias_when_identity_is_confirmed(monkeypatch, yearless_evidence):
|
||||
"""
|
||||
目标缺少年份时,二次识别确认同一媒体仍可保留资源。
|
||||
"""
|
||||
torrent = TorrentInfo(
|
||||
title="Evidence S01 2026 1080p WEB-DL H264 AAC",
|
||||
site_name="测试站点",
|
||||
category=MediaType.TV.value,
|
||||
)
|
||||
contexts, recognize = _parse_evidence_resources(
|
||||
monkeypatch, yearless_evidence, [torrent], yearless_evidence,
|
||||
)
|
||||
|
||||
assert len(contexts) == 1
|
||||
recognize.assert_called_once()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("seasons", [(1, 2), (2, 1)])
|
||||
def test_exact_search_keeps_alias_recognition_separate_by_season(monkeypatch, yearless_evidence, seasons):
|
||||
"""
|
||||
同名同年的两个季各自识别,交换资源顺序也只保留正确身份的季。
|
||||
"""
|
||||
torrents = [TorrentInfo(
|
||||
title=f"Evidence S{season:02d} 2026 1080p WEB-DL H264 AAC",
|
||||
site_name="测试站点",
|
||||
category=MediaType.TV.value,
|
||||
) for season in seasons]
|
||||
other = MediaInfo(
|
||||
media_source=MediaSource.TMDB, media_id="330828", tmdb_id=330828,
|
||||
title="证词", type=MediaType.TV, year="2026",
|
||||
)
|
||||
|
||||
def recognize_season(meta, *, obtain_images):
|
||||
"""
|
||||
模拟媒体源根据请求季号返回不同身份。
|
||||
"""
|
||||
assert not obtain_images
|
||||
return yearless_evidence if meta.begin_season == 1 else other
|
||||
|
||||
contexts, recognize = _parse_evidence_resources(
|
||||
monkeypatch, yearless_evidence, torrents, recognize_season=recognize_season,
|
||||
)
|
||||
|
||||
assert [context.meta_info.begin_season for context in contexts] == [1]
|
||||
assert [call.args[0].begin_season for call in recognize.call_args_list] == list(seasons)
|
||||
|
||||
|
||||
def test_exact_search_skips_disambiguation_without_year_evidence(monkeypatch, yearless_evidence):
|
||||
"""
|
||||
订阅和资源均无年份时继续按标题匹配,不因同名查询引入反证。
|
||||
"""
|
||||
torrent = TorrentInfo(
|
||||
title="Evidence S01 1080p WEB-DL H264 AAC",
|
||||
site_name="测试站点",
|
||||
category=MediaType.TV.value,
|
||||
)
|
||||
|
||||
contexts, recognize = _parse_evidence_resources(monkeypatch, yearless_evidence, [torrent])
|
||||
|
||||
assert len(contexts) == 1
|
||||
recognize.assert_not_called()
|
||||
|
||||
|
||||
def _parse_evidence_resources(monkeypatch, target, torrents, candidate=None, *, recognize_season=None):
|
||||
"""
|
||||
走精确搜索解析资源,用离线识别结果代替媒体源查询。
|
||||
"""
|
||||
media_chain = Mock()
|
||||
media_chain.recognize_by_meta.return_value = candidate
|
||||
media_chain.recognize_by_meta = Mock(return_value=candidate, side_effect=recognize_season)
|
||||
monkeypatch.setattr(search_module, "MediaChain", Mock(return_value=media_chain))
|
||||
monkeypatch.setattr(
|
||||
search_module.TorrentHelper,
|
||||
@@ -196,13 +249,12 @@ def test_exact_search_accepts_yearless_alias_when_identity_is_confirmed(monkeypa
|
||||
chain = object.__new__(SearchChain)
|
||||
|
||||
contexts = chain._parse_result(
|
||||
torrents=[torrent],
|
||||
torrents=torrents,
|
||||
mediainfo=target,
|
||||
rule_groups=[],
|
||||
)
|
||||
|
||||
assert len(contexts) == 1
|
||||
media_chain.recognize_by_meta.assert_called_once()
|
||||
return contexts, media_chain.recognize_by_meta
|
||||
|
||||
|
||||
def test_parallel_filter_propagates_request_context(monkeypatch):
|
||||
@@ -228,6 +280,9 @@ def test_parallel_filter_propagates_request_context(monkeypatch):
|
||||
observed = []
|
||||
|
||||
def filter_torrent(_self, _torrent, _filter_params):
|
||||
"""
|
||||
保留资源并记录过滤时的请求关联 ID。
|
||||
"""
|
||||
observed.append(get_correlation_id())
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user