Files
MoviePilot/app/chain/subscribe/match.py

601 lines
30 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""订阅候选准备、身份复核与资源匹配"""
import copy
from datetime import datetime
from typing import Callable, Dict, List, Optional
from app.application.configuration import get_configured_system_config
from app.application.subscription.contract import build_subscribe_meta, subscribe_media_key
from app.application.torrent.download import TorrentHelper
from app.chain.media import MediaChain
from app.chain.subscribe.contract import _SubscribeOwnerBase
from app.chain.subscribe.identity import subscribe_recognize_kwargs
from app.domain.context import (
Context,
MediaInfo,
TorrentInfo,
)
from app.domain.meta.metabase import MetaBase
from app.domain.meta.words import WordsMatcher
from app.domain.metainfo import MetaInfo
from app.runtime.log import logger
from app.runtime.stop import runtime_stop_state
from app.schemas.media import resolve_media_identity
from app.schemas.types import (
MediaType,
SystemConfigKey,
)
class SubscribeMatchOwner(_SubscribeOwnerBase):
"""订阅候选准备、身份复核与资源匹配,作为 SubscribeChain 的单一职责实现 owner。"""
def _prepare_match_torrents(
self,
torrents: Dict[str, List[Context]],
) -> Dict[str, List[Context]]:
"""预识别待匹配资源,并保留原上下文供后续订阅复用。"""
processed_torrents: Dict[str, List[Context]] = {}
for domain, contexts in torrents.items():
if runtime_stop_state.is_system_stopped:
break
processed_torrents[domain] = []
for context in contexts:
if runtime_stop_state.is_system_stopped:
break
if context.torrent_info and getattr(context.torrent_info, "category", None) in (
MediaType.MUSIC,
MediaType.MUSIC.value,
):
# 音乐 RSS 使用订阅目标做实体匹配,不应进入影视识别并累计失败次数。
processed_torrents[domain].append(context)
continue
if (
not context.media_info or not resolve_media_identity(media=context.media_info)[1]
) and context.media_recognize_fail_count < 3:
logger.debug(
f"尝试重新识别种子:{context.torrent_info.title},当前失败次数:"
f"{context.media_recognize_fail_count}/3"
)
re_mediainfo = MediaChain().recognize_by_meta(
context.meta_info,
obtain_images=False,
)
if re_mediainfo:
re_mediainfo.clear()
context.media_info = re_mediainfo
context.match_source = self._SubscribeChain__get_media_id_match_source(re_mediainfo)
context.candidate_recognized = bool(resolve_media_identity(media=re_mediainfo)[1])
context.media_info_is_target = False
context.media_recognize_fail_count = 0
logger.debug(f"种子 {context.torrent_info.title} 重新识别成功")
else:
context.media_recognize_fail_count += 1
logger.debug(
f"种子 {context.torrent_info.title} 媒体识别失败,失败次数:"
f"{context.media_recognize_fail_count}/3"
)
elif context.media_recognize_fail_count >= 3:
logger.debug(f"种子 {context.torrent_info.title} 已达到最大识别失败次数(3次),跳过识别")
processed_torrents[domain].append(context)
return processed_torrents
def match(
self,
torrents: Dict[str, List[Context]],
progress_callback: Optional[Callable[..., None]] = None,
) -> None:
"""
从缓存中匹配订阅,并自动下载。
该入口保持订阅刷新、定时任务和插件调用的稳定签名,具体匹配流程由内部阶段执行。
"""
if not torrents:
logger.warn("没有缓存资源,无法匹配订阅")
if progress_callback:
progress_callback(value=100, text="没有缓存资源,跳过订阅匹配")
return
if progress_callback:
progress_callback(value=0, text="正在预处理订阅资源 ...")
return self._execute_match(
torrents=torrents,
progress_callback=progress_callback,
)
def _execute_match(
self,
torrents: Dict[str, List[Context]],
progress_callback: Optional[Callable[..., None]] = None,
) -> None:
"""
从缓存中匹配订阅,并自动下载
:param torrents: 按站点分组的资源上下文
:param progress_callback: 订阅匹配进度更新回调
"""
if not torrents:
logger.warn("没有缓存资源,无法匹配订阅")
if progress_callback:
progress_callback(value=100, text="没有缓存资源,跳过订阅匹配")
return
if progress_callback:
progress_callback(value=0, text="正在预处理订阅资源 ...")
lock_acquired = False
try:
lock_acquired = self._acquire_run_lock("match", progress_callback)
if not lock_acquired:
return
processed_torrents = self._prepare_match_torrents(torrents)
# 所有订阅
subscribes = self.subscription_repository.list(self.get_states_for_search("R"))
total_num = len(subscribes)
if progress_callback:
progress_callback(
value=20,
text=f"资源预处理完成,开始匹配 {total_num} 个订阅 ...",
data={"total": total_num, "finished": 0},
)
try:
for index, subscribe in enumerate(subscribes, start=1):
if runtime_stop_state.is_system_stopped:
break
if progress_callback:
progress_callback(
value=20 + ((index - 1) / total_num * 80 if total_num else 80),
text=(f"正在匹配订阅({index}/{total_num}){subscribe.name} ..."),
data={
"total": total_num,
"finished": index - 1,
"current": subscribe.id,
},
)
logger.info(f"开始匹配订阅,标题:{subscribe.name} ...")
if subscribe.type == MediaType.MUSIC.value:
music_contexts = [context for contexts in processed_torrents.values() for context in contexts]
self._match_music_subscribe(subscribe, music_contexts)
continue
mediakey = subscribe_media_key(subscribe)
try:
meta = build_subscribe_meta(subscribe)
except ValueError:
logger.error(f"订阅 {subscribe.name} 类型错误:{subscribe.type}")
continue
# 订阅的站点域名列表
domains = []
if subscribe.sites:
domains = self.site_repository.get_domains_by_ids(subscribe.sites)
# 识别媒体信息
mediainfo: MediaInfo = MediaChain().recognize_media(
meta=meta,
mtype=meta.type,
**subscribe_recognize_kwargs(subscribe),
episode_group=subscribe.episode_group,
cache=False,
)
if not mediainfo:
logger.warn(
f"未识别到媒体信息,标题:{subscribe.name},"
f"媒体来源:{subscribe.media_source},媒体 ID:{subscribe.media_id}"
)
continue
# 如果媒体已存在或已下载完毕,跳过当前订阅处理
exist_flag, no_exists = self.check_and_handle_existing_media(
subscribe=subscribe, meta=meta, mediainfo=mediainfo, mediakey=mediakey
)
if exist_flag:
continue
# 匹配前聚合多来源别名;清理大字段时保留匹配所需的标题合集。
mediainfo = MediaChain().supplement_media_info(mediainfo) or mediainfo
auxiliary_names = list(getattr(mediainfo, "names", None) or [])
mediainfo.clear()
mediainfo.names = auxiliary_names
# 订阅识别词
if subscribe.custom_words:
custom_words_list = subscribe.custom_words.split("\n")
else:
custom_words_list = None
# 遍历预识别后的种子
_match_context = []
torrenthelper = TorrentHelper()
systemconfig = get_configured_system_config()
wordsmatcher = WordsMatcher()
for domain, contexts in processed_torrents.items():
if runtime_stop_state.is_system_stopped:
break
if domains and domain not in domains:
continue
logger.debug(f"开始匹配站点:{domain},共缓存了 {len(contexts)} 个种子...")
for context in contexts:
if runtime_stop_state.is_system_stopped:
break
# 提取信息
_context = copy.copy(context)
torrent_meta = _context.meta_info
torrent_mediainfo = _context.media_info
torrent_info = _context.torrent_info
# 不在订阅站点范围的不处理
sub_sites = self.get_sub_sites(subscribe)
if sub_sites and torrent_info.site not in sub_sites:
logger.debug(f"{torrent_info.site_name} - {torrent_info.title} 不符合订阅站点要求")
continue
# 有自定义识别词时,需要判断是否需要重新识别
if custom_words_list:
# 使用org_string,应用一次后理论上不能再次应用
_, apply_words = wordsmatcher.prepare(
torrent_meta.org_string, custom_words=custom_words_list
)
if apply_words:
logger.info(
f"{torrent_info.site_name} - {torrent_info.title} 因订阅存在自定义识别词,重新识别元数据..."
)
# 重新识别元数据
torrent_meta = MetaInfo(
title=torrent_info.title,
subtitle=torrent_info.description,
custom_words=custom_words_list,
)
# 更新元数据缓存
_context.meta_info = torrent_meta
# 重新识别媒体信息
torrent_mediainfo = MediaChain().recognize_by_meta(
torrent_meta,
episode_group=subscribe.episode_group,
obtain_images=False,
)
if torrent_mediainfo:
# 清理多余信息
torrent_mediainfo.clear()
# 更新种子缓存
_context.media_info = torrent_mediainfo
_context.match_source = self._SubscribeChain__get_media_id_match_source(
torrent_mediainfo
)
_context.candidate_recognized = bool(
resolve_media_identity(media=torrent_mediainfo)[1]
)
_context.media_info_is_target = False
# 如果仍然没有识别到媒体信息,尝试标题匹配
if not torrent_mediainfo or not resolve_media_identity(media=torrent_mediainfo)[1]:
logger.debug(
f"{torrent_info.site_name} - {torrent_info.title} 重新识别失败,尝试通过标题匹配..."
)
if not self._SubscribeChain__is_title_match_allowed(
mediainfo=mediainfo,
torrent_meta=torrent_meta,
torrent_info=torrent_info,
):
continue
# 匹配成功
logger.info(
f"{mediainfo.title_year} 通过标题匹配到可选资源:{torrent_info.site_name} - {torrent_info.title}"
)
torrent_mediainfo = mediainfo
# 更新种子缓存
_context.media_info = mediainfo
_context.match_source = "title"
_context.candidate_recognized = False
_context.media_info_is_target = True
# 直接比对媒体信息
if torrent_mediainfo and resolve_media_identity(media=torrent_mediainfo)[1]:
if torrent_mediainfo.type != mediainfo.type:
continue
torrent_mediainfo = self._SubscribeChain__reconcile_candidate_media(
target_mediainfo=mediainfo,
candidate_mediainfo=torrent_mediainfo,
torrent_meta=torrent_meta,
torrent_info=torrent_info,
context=_context,
)
if not torrent_mediainfo:
continue
match_source = _context.match_source
if match_source == "title":
# 标题兜底使用的是订阅目标 media_info,不能标记为候选自身识别结果。
_context.candidate_recognized = False
_context.media_info_is_target = True
match_label = "标题复核"
elif match_source == "unknown":
_context.match_source = self._SubscribeChain__get_media_id_match_source(
torrent_mediainfo
)
_context.candidate_recognized = True
_context.media_info_is_target = False
match_label = "媒体ID"
else:
_context.candidate_recognized = True
_context.media_info_is_target = False
match_label = "媒体ID"
logger.info(
f"{mediainfo.title_year} 通过{match_label}匹配到可选资源:"
f"{torrent_info.site_name} - {torrent_info.title}"
)
else:
continue
# 如果是电视剧
if torrent_mediainfo.type == MediaType.TV:
# 有多季的不要
if len(torrent_meta.season_list) > 1:
logger.debug(f"{torrent_info.title} 有多季,不处理")
continue
# 比对季
if torrent_meta.begin_season is not None:
if meta.begin_season != torrent_meta.begin_season:
logger.debug(f"{torrent_info.title} 季不匹配")
continue
elif meta.begin_season != 1:
logger.debug(f"{torrent_info.title} 季不匹配")
continue
# 非洗版
if not subscribe.best_version:
# 不是缺失的剧集不要
if no_exists and no_exists.get(mediakey):
# 缺失集
no_exists_info = no_exists.get(mediakey).get(subscribe.season)
if no_exists_info:
# 是否有交集
if (
no_exists_info.episodes
and torrent_meta.episode_list
and not set(no_exists_info.episodes).intersection(
set(torrent_meta.episode_list)
)
):
logger.debug(
f"{torrent_info.title} 对应剧集 {torrent_meta.episode_list} 未包含缺失的剧集"
)
continue
else:
if not self._SubscribeChain__is_full_season_best_version_resource(
meta=torrent_meta,
subscribe=subscribe,
):
logger.debug(
f"{subscribe.name} 正在全集洗版,{torrent_info.title} 不是全集资源"
)
continue
# 洗版时,不符合订阅集数的不要
if meta.type == MediaType.TV and not self._is_episode_range_covered(
meta=torrent_meta,
subscribe=subscribe,
):
logger.debug(
f"{subscribe.name} 正在洗版,{torrent_info.title} 不符合订阅集数范围"
)
continue
# 匹配订阅附加参数
if not torrenthelper.filter_torrent(
torrent_info=torrent_info, filter_params=self.get_params(subscribe)
):
continue
# 优先级过滤规则
if subscribe.best_version:
rule_groups = subscribe.filter_groups or systemconfig.get(
SystemConfigKey.BestVersionFilterRuleGroups
)
else:
rule_groups = subscribe.filter_groups or systemconfig.get(
SystemConfigKey.SubscribeFilterRuleGroups
)
result: List[TorrentInfo] = self.filter_torrents(
rule_groups=rule_groups, torrent_list=[torrent_info], mediainfo=torrent_mediainfo
)
if result is not None and not result:
# 不符合过滤规则
logger.debug(f"{torrent_info.title} 不匹配过滤规则")
continue
# 洗版时,优先级小于已下载优先级的不要
if subscribe.best_version:
if meta.type == MediaType.TV:
if not self._SubscribeChain__prepare_best_version_tv_candidate(
subscribe=subscribe,
context=_context,
priority=torrent_info.pri_order,
):
logger.info(
f"{subscribe.name} 正在洗版,{torrent_info.title} "
f"优先级未达到当前模式的升级条件"
)
continue
if (
meta.type != MediaType.TV
and subscribe.current_priority
and torrent_info.pri_order <= subscribe.current_priority
):
logger.info(
f"{subscribe.name} 正在洗版,{torrent_info.title} 优先级低于或等于已下载优先级"
)
continue
# 匹配成功
logger.info(f"{mediainfo.title_year} 匹配成功:{torrent_info.title}")
# 自定义属性
if subscribe.media_category:
torrent_mediainfo.category = subscribe.media_category
if subscribe.episode_group:
torrent_mediainfo.episode_group = subscribe.episode_group
_match_context.append(_context)
if not _match_context:
# 未匹配到资源
logger.info(f"{mediainfo.title_year} 未匹配到符合条件的资源")
self.finish_subscribe_or_not(
subscribe=subscribe, meta=meta, mediainfo=mediainfo, lefts=no_exists
)
continue
# 开始批量择优下载
logger.info(f"{mediainfo.title_year} 匹配完成,共匹配到{len(_match_context)}个资源")
downloads, lefts = self._SubscribeChain__download_best_version_with_full_pack_first(
contexts=_match_context,
no_exists=no_exists,
subscribe=subscribe,
mediakey=mediakey,
username=subscribe.username,
save_path=subscribe.save_path,
downloader=subscribe.downloader,
source=self.get_subscribe_source_keyword(subscribe),
)
# 同步外部修改,更新订阅信息
updated_subscribe = self.subscription_repository.get(subscribe.id)
# 判断是否要完成订阅
if updated_subscribe:
self.finish_subscribe_or_not(
subscribe=updated_subscribe,
meta=meta,
mediainfo=mediainfo,
downloads=downloads,
lefts=lefts,
)
finally:
processed_torrents.clear()
del processed_torrents
subscribes.clear()
del subscribes
if progress_callback:
progress_callback(
value=100,
text="订阅资源匹配完成",
data={"total": total_num, "finished": total_num},
)
finally:
if lock_acquired:
self._rlock.release()
logger.debug(f"match Lock released at {datetime.now()}")
@staticmethod
def _SubscribeChain__get_media_id_match_source(mediainfo: Optional[MediaInfo]) -> str:
"""
返回候选自身识别命中的明确媒体 ID 类型。
"""
media_source, media_id = resolve_media_identity(media=mediainfo)
if media_source and media_id:
return str(media_source)
return "unknown"
@staticmethod
def _SubscribeChain__is_title_match_allowed(
mediainfo: MediaInfo,
torrent_meta: MetaBase,
torrent_info: TorrentInfo,
) -> bool:
"""判断资源是否可以通过标题兜底匹配订阅目标。"""
if not TorrentHelper.match_torrent(
mediainfo=mediainfo,
torrent_meta=torrent_meta,
torrent=torrent_info,
):
return False
if not TorrentHelper.requires_identity_disambiguation(
mediainfo=mediainfo,
torrent_meta=torrent_meta,
):
return True
logger.info(
f"{torrent_info.site_name} - {torrent_info.title} 仅通过无年份别名命中且候选媒体身份无法确认,已跳过"
)
return False
@staticmethod
def _SubscribeChain__reconcile_candidate_media(
target_mediainfo: MediaInfo,
candidate_mediainfo: MediaInfo,
torrent_meta: MetaBase,
torrent_info: TorrentInfo,
context: Context,
) -> Optional[MediaInfo]:
"""
在推断媒体 ID 冲突时,以订阅目标对候选标题做严格复核。
标题携带明确媒体身份或目标缺少同来源 ID 时保持严格拒绝;只有普通
标题推断产生冲突且标题、别名、类型和年份复核通过时才回填订阅目标。
:param target_mediainfo: 订阅目标媒体信息
:param candidate_mediainfo: RSS 或首页候选的识别结果
:param torrent_meta: 候选标题解析信息
:param torrent_info: 候选种子信息
:param context: 当前订阅使用的候选上下文副本
:return: 可继续匹配的媒体信息,拒绝时返回 None
"""
candidate_source, candidate_id = resolve_media_identity(media=candidate_mediainfo)
target_source, target_id = resolve_media_identity(media=target_mediainfo)
conflicts = []
if candidate_source and candidate_id and (candidate_source != target_source or candidate_id != target_id):
conflicts.append(
(
str(candidate_source),
candidate_id,
f"{target_source}:{target_id}" if target_source and target_id else None,
)
)
if not conflicts:
return candidate_mediainfo
conflict_text = "、".join(
f"{source} {candidate_id} != {target_id if target_id is not None else '缺失'}"
for source, candidate_id, target_id in conflicts
)
if any(target_id is None for _, _, target_id in conflicts):
logger.debug(
f"{torrent_info.site_name} - {torrent_info.title} 候选媒体ID与订阅目标不可比较:{conflict_text}"
)
return None
explicit_source, explicit_id = resolve_media_identity(media=torrent_meta)
if explicit_source and explicit_id:
logger.debug(
f"{torrent_info.site_name} - {torrent_info.title} 标题含明确媒体ID "
f"{explicit_source}:{explicit_id},保持严格校验:{conflict_text}"
)
return None
if not TorrentHelper.match_torrent(
mediainfo=target_mediainfo,
torrent_meta=torrent_meta,
torrent=torrent_info,
):
logger.debug(
f"{torrent_info.site_name} - {torrent_info.title} 候选媒体ID冲突且标题复核失败:{conflict_text}"
)
return None
evidence_matched, evidence = TorrentHelper.match_same_work_evidence(
target_mediainfo=target_mediainfo,
candidate_mediainfo=candidate_mediainfo,
torrent_meta=torrent_meta,
)
if not evidence_matched:
logger.debug(
f"{torrent_info.site_name} - {torrent_info.title} 候选媒体ID冲突且缺少同作品证据:"
f"{evidence};{conflict_text}"
)
return None
context.media_info = target_mediainfo
context.match_source = "title"
context.candidate_recognized = False
context.media_info_is_target = True
logger.debug(
f"{target_mediainfo.title_year} 候选媒体ID冲突({conflict_text}),"
f"经标题或别名及{evidence}复核匹配到订阅目标:"
f"{torrent_info.site_name} - {torrent_info.title}"
)
return target_mediainfo