refactor(pipeline): 拆分 ParserService,把流水线抽离为 MediaPipeline
- 新增 services/parser.py:接管 infer_structure / get_target_info,纯计算无状态 - renamer.py 只保留扫描编排,物理改名委托 FileOpsService,移除 do_rename_single - 恢复被误删的季目录/根目录重命名调用(task.py 侧已改用 FileOpsService) - 新增 core/pipeline.py:接管文件扫描、电影/剧集混排与重复集号防爆锁、执行流程 - main.py 只保留 CLI 入口,watch 与手动模式统一调用 MediaPipeline
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
# src/services/parser.py
|
||||
|
||||
from pathlib import Path
|
||||
from core import config
|
||||
from core.logger import log
|
||||
from core.constants import RegexPatterns
|
||||
from core.utils import StringUtil
|
||||
from services.mediainfo import MediaInfoService
|
||||
from services.tmdb import TMDBService
|
||||
|
||||
class ParserService:
|
||||
"""纯业务计算与媒体结构解析器 (无状态)"""
|
||||
|
||||
@staticmethod
|
||||
def infer_structure(file_path: Path, context_cache: dict, forced_type: str = None):
|
||||
parent_name = file_path.parent.name
|
||||
grandparent_name = file_path.parent.parent.name
|
||||
|
||||
season_match = RegexPatterns.SEASON_DIR.match(parent_name)
|
||||
flat_tv_match = RegexPatterns.FLAT_TV.search(file_path.stem)
|
||||
pure_number_match = RegexPatterns.PURE_NUMBER.fullmatch(file_path.stem)
|
||||
|
||||
if forced_type == "tv" or (not forced_type and season_match):
|
||||
m_type = "tv"
|
||||
if season_match:
|
||||
season_num = season_match.group(1).zfill(2)
|
||||
target_season_folder = f"Season {season_num}"
|
||||
target_name, root_dir_path = grandparent_name, file_path.parent.parent
|
||||
else:
|
||||
season_num = (flat_tv_match.group(1) if flat_tv_match and flat_tv_match.lastindex >= 1 and flat_tv_match.group(1) else "01").zfill(2)
|
||||
target_season_folder = None
|
||||
target_name, root_dir_path = parent_name, file_path.parent
|
||||
|
||||
elif forced_type == "movie" or (not forced_type and not season_match and not (flat_tv_match or pure_number_match)):
|
||||
m_type = "movie"
|
||||
season_num = "01"
|
||||
target_season_folder = None
|
||||
|
||||
if season_match:
|
||||
target_name, root_dir_path = grandparent_name, file_path.parent.parent
|
||||
else:
|
||||
target_name, root_dir_path = parent_name, file_path.parent
|
||||
|
||||
elif not forced_type and (flat_tv_match or pure_number_match):
|
||||
m_type = "tv"
|
||||
season_num = (flat_tv_match.group(1) if flat_tv_match and flat_tv_match.lastindex >= 1 and flat_tv_match.group(1) else "01").zfill(2)
|
||||
target_season_folder = None
|
||||
target_name, root_dir_path = parent_name, file_path.parent
|
||||
else:
|
||||
return None
|
||||
|
||||
root_key = str(root_dir_path)
|
||||
if root_key not in context_cache:
|
||||
clean_name = RegexPatterns.TMDB_CLEAN.sub('', target_name).strip()
|
||||
tm = RegexPatterns.TMDB_CLEAN.search(target_name)
|
||||
if tm:
|
||||
title, year = tm.group(1).strip().replace(' ', '.'), tm.group(2)
|
||||
else:
|
||||
title, year = clean_name.strip().replace(' ', '.'), ""
|
||||
|
||||
if not year:
|
||||
context_path = " / ".join(file_path.parts[-3:])
|
||||
log.warning(f"目录命名不规范: {root_dir_path.name}")
|
||||
log.warning(f"📁 案发现场: {context_path}")
|
||||
|
||||
user_title = input(f" ➤ 确认标题 (回车默认 '{title}'): ").strip()
|
||||
if user_title: title = user_title.replace(' ', '.')
|
||||
while not RegexPatterns.YEAR_EXACT.match(year):
|
||||
year = input(f" ➤ 请输入 4 位年份: ").strip()
|
||||
|
||||
context_cache[root_key] = (title, year)
|
||||
|
||||
title, year = context_cache[root_key]
|
||||
return m_type, title, year, season_num, target_season_folder, root_dir_path, f"{title.replace('.', ' ')} ({year})"
|
||||
|
||||
@staticmethod
|
||||
def get_target_info(f_path: Path, m_type: str, title: str, year: str, season_num: str, is_multi_version: bool = False):
|
||||
tags = MediaInfoService.get_scene_tags(f_path)
|
||||
tmdb = TMDBService.search_media(title, year, m_type, season_num)
|
||||
|
||||
orig = tmdb['original_title'] if tmdb else ""
|
||||
if orig:
|
||||
display_name = orig
|
||||
else:
|
||||
display_name = StringUtil.sanitize_title(title)
|
||||
|
||||
tid = f"[tmdbid-{tmdb['tmdb_id']}]" if (tmdb and config.INCLUDE_TMDB_ID) else ""
|
||||
|
||||
source = "BluRay.REMUX" if tags['is_remux'] else "WEB-DL"
|
||||
tech = f"{tags['res']}.{source}.{tags['v_codec']}.{tags['bits']}.{tags['a_info']}"
|
||||
|
||||
target_folder = f"{title.replace('.', ' ')} ({year}) {tid}".strip()
|
||||
|
||||
if m_type == "movie":
|
||||
if is_multi_version:
|
||||
return f"{title} ({year}) - {tech}", target_folder
|
||||
return f"{display_name}.{year}.{tech}", target_folder
|
||||
else:
|
||||
match = RegexPatterns.TARGET_EP.search(f_path.stem)
|
||||
ep = next((g for g in match.groups() if g), "01").zfill(2) if match else "01"
|
||||
|
||||
episode_year = year
|
||||
if tmdb and "season_years" in tmdb:
|
||||
episode_year = tmdb["season_years"].get(season_num, year)
|
||||
|
||||
return f"{display_name}.S{season_num}E{ep}.{episode_year}.{tech}", target_folder
|
||||
Reference in New Issue
Block a user