update 202605101839
This commit is contained in:
+82
-12
@@ -2,6 +2,7 @@
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from core.logger import log
|
||||
@@ -17,26 +18,46 @@ def run_pipeline(target_dir: Path, args, auto_execute=False):
|
||||
"""流水线作业:可被手动或守护进程触发"""
|
||||
files_to_process = target_dir.rglob("*") if args.mode in ["auto", "watch"] else target_dir.glob("*")
|
||||
|
||||
media_types_found = set()
|
||||
# 💡 注意这里:用 media_types_by_root 替换掉最老的 media_types_found
|
||||
media_types_by_root = {}
|
||||
valid_files = []
|
||||
context_cache = {}
|
||||
|
||||
if not auto_execute: log.info("🔍 开始执行文件结构分析...")
|
||||
for f in files_to_process:
|
||||
if f.is_file() and f.suffix.lower() in config.SUPPORTED_EXTS:
|
||||
struct = RenamerService.infer_structure(f, context_cache)
|
||||
|
||||
# 🛡️ 神器 1:黑名单绝对拦截机制
|
||||
if any(excl in str(f) for excl in config.EXCLUDE_DIRS):
|
||||
log.debug(f"🛑 [黑名单拦截] 命中排除规则,跳过扫描: {f.parent.name}/{f.name}")
|
||||
continue
|
||||
|
||||
# 🛠️ 神器 2:结构分析 (带上一票否决权 force_type)
|
||||
struct = RenamerService.infer_structure(f, context_cache, forced_type=getattr(args, 'force_type', None))
|
||||
if not struct: continue
|
||||
|
||||
m_type = struct[0]
|
||||
media_types_found.add(m_type)
|
||||
if len(media_types_found) > 1:
|
||||
log.error("❌ 严重安全拦截:同一扫描目录下混合了电影和电视剧,请分目录存放!")
|
||||
root_dir_path = struct[5]
|
||||
|
||||
log.debug(f"判定: [{m_type.upper():^5}] <- {f.parent.name}/{f.name}")
|
||||
|
||||
# 💣 神器 3:精确制导防爆锁 (防撞判定缩小到单个剧集/电影目录下)
|
||||
if root_dir_path not in media_types_by_root:
|
||||
media_types_by_root[root_dir_path] = set()
|
||||
|
||||
media_types_by_root[root_dir_path].add(m_type)
|
||||
|
||||
if len(media_types_by_root[root_dir_path]) > 1:
|
||||
log.error(f"❌ 严重安全拦截:在同一目录 [{root_dir_path.name}] 下混合了电影和电视剧!")
|
||||
log.error(f"⚠️ 触发拦截的嫌疑文件: {f.name} (被判定为 {m_type})")
|
||||
log.error("💡 提示:通常是因为剧集根目录下散落了未标明 S01E01 格式的花絮(SP/NCOP等)。")
|
||||
if not auto_execute: sys.exit(1)
|
||||
return # 守护模式下直接 return,不中断进程
|
||||
|
||||
valid_files.append((f, *struct))
|
||||
|
||||
if not valid_files:
|
||||
if not auto_execute: log.warning("⚠️ 扫描结束:未检测到任何支持的媒体文件。")
|
||||
if not auto_execute: log.warning("⚠️ 扫描结束:未检测到任何需要处理的媒体文件。")
|
||||
return
|
||||
|
||||
dir_video_counts = {f.parent: 0 for f, *_ in valid_files}
|
||||
@@ -46,31 +67,78 @@ def run_pipeline(target_dir: Path, args, auto_execute=False):
|
||||
if not auto_execute: log.info("📝 正在生成执行计划...")
|
||||
task_mgr = TaskManager()
|
||||
|
||||
# 🛡️ 神器 4:集数追尾防爆追踪器
|
||||
# 用于记录:{(剧集根目录, SxxExx) : 原始文件路径}
|
||||
tv_episode_tracker = {}
|
||||
|
||||
for f, m_type, title, year, season_num, target_season_folder, root_dir_path, target_root_folder in valid_files:
|
||||
is_multi = (m_type == "movie" and dir_video_counts.get(f.parent, 0) > 1)
|
||||
target_stem, folder_name = RenamerService.get_target_info(f, m_type, title, year, season_num, is_multi)
|
||||
target_stem, folder_name = RenamerService.get_target_info(f, m_type, title, year, season_num, is_multi)
|
||||
|
||||
# 💡 [保留原样] 新增:集数防撞绝对拦截逻辑
|
||||
if m_type == "tv":
|
||||
ep_match = re.search(r'(S\d{1,2}E\d{1,3})', target_stem)
|
||||
if ep_match:
|
||||
ep_id = ep_match.group(1) # 例如:S01E06
|
||||
tracker_key = (str(root_dir_path), ep_id)
|
||||
|
||||
if tracker_key in tv_episode_tracker:
|
||||
conflict_file = tv_episode_tracker[tracker_key]
|
||||
log.error("=" * 80)
|
||||
log.error(f"❌ 严重安全拦截:同剧集下发现重复的集号 [{ep_id}]!")
|
||||
log.error(f" ├─ [冲突文件 A]: {conflict_file.absolute()}")
|
||||
log.error(f" └─ [冲突文件 B]: {f.absolute()}")
|
||||
log.error("💡 提示:这通常是因为花絮/SP未加 S00 标签,或命名极其不规范导致正则提取碰撞。")
|
||||
log.error("🛡️ 为保护您的数据不被覆盖或错乱命名,流水线已紧急拉闸。请手动排查上述两个文件。")
|
||||
log.error("=" * 80)
|
||||
if not auto_execute: sys.exit(1)
|
||||
return # 守护模式下安全退出,不中断进程
|
||||
else:
|
||||
tv_episode_tracker[tracker_key] = f
|
||||
|
||||
# ==================== 💡 NEW: 智能路由决策中心 ====================
|
||||
is_flat_tv = (m_type == "tv" and f.parent == root_dir_path)
|
||||
|
||||
if is_flat_tv:
|
||||
# 扁平结构的 TV,提取季号,强制分配到根目录下的 Season 目录
|
||||
ep_match = re.search(r'S(\d{1,2})E', target_stem)
|
||||
s_num = ep_match.group(1) if ep_match else "01"
|
||||
dest_dir = root_dir_path / f"Season {s_num}"
|
||||
else:
|
||||
# 电影,或原本就已经在子目录的 TV,目标仍是当前目录(由后期的文件夹重命名接管)
|
||||
dest_dir = f.parent
|
||||
# ==================================================================
|
||||
|
||||
is_perfect = (f.name == f"{target_stem}{f.suffix.lower()}")
|
||||
skip_heavy = config.SKIP_PERFECT_MATCH and is_perfect and args.mode not in ["clean-ads", "extract-subs"]
|
||||
|
||||
if skip_heavy and not auto_execute:
|
||||
if args.extract_internal:
|
||||
log.info(f"💡 [特权放行] 《{title}》检测到强制参数,放行底层扫描。")
|
||||
log.info(f"💡 [特权放行] {f.name} 检测到强制参数,放行底层扫描。")
|
||||
else:
|
||||
log.info(f"✨ [完美豁免] 《{title}》已达最高工业标准,本次体检直接放行~")
|
||||
log.debug(f"✨ [完美豁免] {f.name} 已达最高工业标准,直接放行~")
|
||||
|
||||
if args.mode != "clean-ads":
|
||||
RenamerService.scan_existing_subtitles(f, target_stem, m_type, task_mgr)
|
||||
# 💡 传入 dest_dir
|
||||
RenamerService.scan_existing_subtitles(f, target_stem, m_type, dest_dir, task_mgr)
|
||||
if (config.EXTRACT_INTERNAL_SUBS or args.extract_internal) and (not skip_heavy or args.extract_internal):
|
||||
SubtitleService.scan_subtitles(f, task_mgr, target_stem)
|
||||
# 💡 传入 dest_dir
|
||||
SubtitleService.scan_subtitles(f, task_mgr, target_stem, dest_dir)
|
||||
|
||||
if args.mode != "extract-subs" and not skip_heavy:
|
||||
MetadataService.scan_ads(f, task_mgr)
|
||||
MetadataService.scan_audio_tracks(f, task_mgr)
|
||||
|
||||
if args.mode not in ["clean-ads", "extract-subs"]:
|
||||
RenamerService.scan_rename(f, target_stem, m_type, target_season_folder, root_dir_path, folder_name, task_mgr)
|
||||
# 💡 传入 dest_dir
|
||||
RenamerService.scan_rename(f, target_stem, m_type, dest_dir, target_season_folder, root_dir_path, folder_name, task_mgr)
|
||||
|
||||
# 💡 [保留原样] 核心修复:提前拦截空任务,不再进入预览流程产生逻辑报错
|
||||
if not task_mgr.has_tasks():
|
||||
if not auto_execute: log.info("✨ 您的媒体库目前处于完美状态,无需任何物理变更。")
|
||||
return
|
||||
|
||||
# 这里才真正进入交互流程
|
||||
if task_mgr.preview_and_confirm(auto_execute):
|
||||
if not auto_execute: log.info("⚙️ 正在执行物理任务...")
|
||||
task_mgr.execute()
|
||||
@@ -78,11 +146,13 @@ def run_pipeline(target_dir: Path, args, auto_execute=False):
|
||||
elif not auto_execute:
|
||||
log.info("💤 任务已由用户取消。")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="🎬 流媒体高级自动化管理工具")
|
||||
parser.add_argument("mode", nargs='?', default="manual", choices=["manual", "auto", "clean-ads", "extract-subs", "watch"])
|
||||
parser.add_argument("-p", "--path", default=".")
|
||||
parser.add_argument("--extract-internal", action="store_true")
|
||||
parser.add_argument("-t", "--force-type", choices=["tv", "movie"], help="强制指定媒体类型 (tv 或 movie),解决正则误判")
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user