mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-21 19:34:19 +08:00
214 lines
8.6 KiB
Python
214 lines
8.6 KiB
Python
"""下载候选规范化、排序和媒体选择 owner。"""
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Callable, Dict, List, Optional, Set, Tuple, Union, cast
|
|
|
|
from app.application.configuration import get_chain_runtime_config_snapshot
|
|
from app.application.download.admission import SubscriptionDownloadGovernance
|
|
from app.application.download.failures import (
|
|
DownloadFailureSnapshot,
|
|
)
|
|
from app.application.torrent.download import TorrentHelper
|
|
from app.chain.download.contract import _DownloadOwnerBase
|
|
from app.domain.context import (
|
|
Context,
|
|
MediaInfo,
|
|
)
|
|
from app.domain.meta.metamusic import MetaMusic
|
|
from app.foundation import text as text_tools
|
|
from app.runtime.log import logger
|
|
from app.runtime.stop import runtime_stop_state
|
|
from app.schemas.event import ResourceSelectionEventData
|
|
from app.schemas.media import build_media_key, resolve_media_identity
|
|
from app.schemas.types import (
|
|
MUSIC_ENTITY_ALBUM,
|
|
ChainEventType,
|
|
MediaType,
|
|
NotificationChannel,
|
|
)
|
|
|
|
|
|
def _new_torrent_helper() -> TorrentHelper:
|
|
"""构造保留动态初始化行为但具有静态返回类型的种子助手。"""
|
|
factory = cast(Callable[[], TorrentHelper], TorrentHelper)
|
|
return factory()
|
|
|
|
|
|
def _confirmed_batch_contexts(contexts: List[Context]) -> List[Context]:
|
|
"""排除待人工确认的资源,覆盖原始候选及插件替换候选两个入口。"""
|
|
confirmed = [context for context in contexts if getattr(context, "match_status", None) in (None, "exact")]
|
|
if len(confirmed) != len(contexts):
|
|
logger.warning(f"跳过 {len(contexts) - len(confirmed)} 个待人工确认的资源,不参与自动批量下载")
|
|
return confirmed
|
|
|
|
|
|
class DownloadSelectionOwner(_DownloadOwnerBase):
|
|
"""下载候选规范化、排序和媒体选择 owner。"""
|
|
|
|
@classmethod
|
|
def _record_music_album_track_keys(
|
|
cls,
|
|
context: Context,
|
|
file_list: Optional[List[str]],
|
|
) -> None:
|
|
"""记录种子中可识别的音轨键,允许专辑订阅跨多次资源下载累计完成。"""
|
|
media = context.media_info
|
|
if (
|
|
not media
|
|
or media.type != MediaType.MUSIC
|
|
or getattr(media, "music_type", None) != MUSIC_ENTITY_ALBUM
|
|
):
|
|
return
|
|
|
|
audio_extensions = get_chain_runtime_config_snapshot().audio_extensions
|
|
track_keys = {
|
|
track_key
|
|
for file in file_list or []
|
|
if Path(str(file)).suffix.lower() in audio_extensions
|
|
and (track_key := cls._music_resource_track_key(file))
|
|
}
|
|
context.music_track_keys = sorted(track_keys) or None
|
|
|
|
@staticmethod
|
|
def _music_resource_track_key(file: str) -> Optional[str]:
|
|
"""把音轨身份编码成跨下载资源稳定保存的 JSON 键。"""
|
|
identity = DownloadSelectionOwner._music_resource_track_identity(file)
|
|
if identity is None:
|
|
return None
|
|
return json.dumps(identity, ensure_ascii=False, separators=(",", ":"))
|
|
|
|
@staticmethod
|
|
def _music_resource_track_identity(file: str) -> Optional[Tuple[int, Union[int, str]]]:
|
|
"""从资源文件路径提取盘号和曲序,缺少曲序时按归一化曲名去重。"""
|
|
file_path = Path(str(file))
|
|
file_meta = MetaMusic(
|
|
org_string=file_path.name,
|
|
title=file_path.stem,
|
|
).apply_path_context(file_path)
|
|
track_identity: Union[int, str, None] = file_meta.track_number
|
|
if track_identity is None:
|
|
track_identity = text_tools.normalize_upper(file_meta.title or file_path.stem)
|
|
if track_identity in (None, ""):
|
|
return None
|
|
return file_meta.disc_number or 1, track_identity
|
|
|
|
@staticmethod
|
|
def _media_identity_keys(media: Optional[MediaInfo]) -> Set[str]:
|
|
"""返回媒体的统一身份键,用于临时缺失集映射匹配。"""
|
|
if not media:
|
|
return set()
|
|
source, media_id = resolve_media_identity(media=media)
|
|
media_key = build_media_key(source, media_id)
|
|
return {media_key} if media_key else set()
|
|
|
|
@classmethod
|
|
def _matches_media_identity(cls, media: Optional[MediaInfo], media_key: object) -> bool:
|
|
"""判断媒体是否命中来源与原生 ID 组成的统一身份键。"""
|
|
return media_key is not None and str(media_key) in cls._media_identity_keys(media)
|
|
|
|
def _prepare_batch_download_contexts(
|
|
self,
|
|
contexts: List[Context],
|
|
downloader: Optional[str],
|
|
source: Optional[str],
|
|
) -> Tuple[List[Context], Dict[str, Optional[DownloadFailureSnapshot]]]:
|
|
"""
|
|
执行批量下载前的资源选择、排序和失败冷却准备。
|
|
|
|
:return: 排序后的上下文和本轮失败冷却记录;资源选择事件仍可替换上下文列表
|
|
"""
|
|
contexts = _confirmed_batch_contexts(contexts)
|
|
if not contexts:
|
|
return [], {}
|
|
logger.debug(f"Initial contexts: {len(contexts)} items, Downloader: {downloader}")
|
|
event_data = ResourceSelectionEventData(
|
|
contexts=contexts,
|
|
downloader=downloader,
|
|
origin=source,
|
|
)
|
|
event = self.eventmanager.send_event(ChainEventType.ResourceSelection, event_data)
|
|
if event and event.event_data:
|
|
event_data = event.event_data
|
|
if event_data.updated and event_data.updated_contexts is not None:
|
|
logger.debug(
|
|
f"Contexts updated by event: {len(event_data.updated_contexts)} "
|
|
f"items (source: {event_data.source})"
|
|
)
|
|
contexts = event_data.updated_contexts
|
|
contexts = _new_torrent_helper().sort_torrents(_confirmed_batch_contexts(contexts))
|
|
active_failures: Dict[str, Optional[DownloadFailureSnapshot]] = {
|
|
fingerprint: failure
|
|
for fingerprint, failure in self._active_download_failure_fingerprints(
|
|
contexts=contexts,
|
|
source=source,
|
|
).items()
|
|
}
|
|
return contexts, active_failures
|
|
|
|
def _download_movie_music_candidates(
|
|
self,
|
|
contexts: List[Context],
|
|
downloaded_list: List[Context],
|
|
active_failure_records: Dict[str, Optional[DownloadFailureSnapshot]],
|
|
save_path: Optional[str],
|
|
channel: Optional[NotificationChannel],
|
|
source: Optional[str],
|
|
userid: Optional[str],
|
|
username: Optional[str],
|
|
downloader: Optional[str],
|
|
custom_words: Optional[str],
|
|
governance: Optional[SubscriptionDownloadGovernance],
|
|
) -> None:
|
|
"""
|
|
处理电影与音乐的直接候选下载。
|
|
|
|
两类媒体都遵循成功后按身份去重、失败后继续尝试后续候选的规则,差异只在去重键。
|
|
"""
|
|
downloaded_keys: Dict[MediaType, Set[str]] = {
|
|
MediaType.MOVIE: set(),
|
|
MediaType.MUSIC: set(),
|
|
}
|
|
for context in contexts:
|
|
if runtime_stop_state.is_system_stopped:
|
|
break
|
|
media = context.media_info
|
|
if media is None:
|
|
continue
|
|
media_type = media.type
|
|
if media_type not in downloaded_keys:
|
|
continue
|
|
fingerprint = self._build_download_failure_fingerprint(context)
|
|
if fingerprint and fingerprint in active_failure_records:
|
|
self._log_download_failure_cooldown(
|
|
context,
|
|
active_failure_records[fingerprint],
|
|
)
|
|
continue
|
|
if media_type == MediaType.MOVIE:
|
|
download_key = media.title_year
|
|
label = "电影"
|
|
else:
|
|
media_source, media_id = resolve_media_identity(media=media)
|
|
download_key = build_media_key(media_source, media_id) or media.title_year
|
|
label = "音乐"
|
|
if download_key in downloaded_keys[media_type]:
|
|
continue
|
|
logger.info(f"开始下载{label} {context.torrent_info.title} ...")
|
|
if self.download_single(
|
|
context,
|
|
save_path=save_path,
|
|
channel=channel,
|
|
source=source,
|
|
userid=userid,
|
|
username=username,
|
|
downloader=downloader,
|
|
custom_words=custom_words,
|
|
governance=governance,
|
|
):
|
|
logger.info(f"{context.torrent_info.title} 添加下载成功")
|
|
downloaded_list.append(context)
|
|
downloaded_keys[media_type].add(download_key)
|
|
elif fingerprint:
|
|
active_failure_records[fingerprint] = None
|