From dfee0049fb02131e9ed9be4efd4fcf51b8935e9e Mon Sep 17 00:00:00 2001 From: Hraulein Date: Tue, 17 Nov 2026 11:32:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=E6=8A=BD=E5=87=BA=E6=AD=A3?= =?UTF-8?q?=E5=88=99=E3=80=81=E6=8E=A2=E9=92=88=E4=B8=8E=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E7=AD=89=E5=85=AC=E5=85=B1=E5=9F=BA=E7=A1=80=E8=AE=BE=E6=96=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 core/constants.py:集中管理结构解析、命名与字幕识别用到的正则表达式 - 新增 core/utils.py:StringUtil(标题清洗)、FFprobeUtil(ffprobe JSON 调用)、MediaUtil(字幕语言推断) - 新增 core/cache.py:JsonCacheManager 统一 JSON 缓存读写,替代 TMDBService 内联的 _load_cache/_save_cache --- media/src/core/cache.py | 42 +++++++++++++++++++++++++++++++++ media/src/core/constants.py | 31 +++++++++++++++++++++++++ media/src/core/utils.py | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 media/src/core/cache.py create mode 100644 media/src/core/constants.py create mode 100644 media/src/core/utils.py diff --git a/media/src/core/cache.py b/media/src/core/cache.py new file mode 100644 index 0000000..6ca347e --- /dev/null +++ b/media/src/core/cache.py @@ -0,0 +1,42 @@ +# src/core/cache.py + +import json +from pathlib import Path +from core.logger import log + +class JsonCacheManager: + """全局无状态 JSON 缓存管理器""" + _caches = {} + + @classmethod + def _load_if_needed(cls, cache_path: Path): + path_str = str(cache_path) + if path_str not in cls._caches: + if cache_path.exists(): + try: + cls._caches[path_str] = json.loads(cache_path.read_text('utf-8')) + except Exception as e: + log.warning(f"⚠️ 读取缓存失败 {cache_path}: {e}") + cls._caches[path_str] = {} + else: + cls._caches[path_str] = {} + return cls._caches[path_str] + + @classmethod + def get(cls, cache_path: Path, key: str, default=None): + cache_dict = cls._load_if_needed(cache_path) + return cache_dict.get(key, default) + + @classmethod + def set(cls, cache_path: Path, key: str, value): + cache_dict = cls._load_if_needed(cache_path) + cache_dict[key] = value + + @classmethod + def save(cls, cache_path: Path): + path_str = str(cache_path) + if path_str in cls._caches: + try: + cache_path.write_text(json.dumps(cls._caches[path_str], ensure_ascii=False, indent=2), 'utf-8') + except Exception as e: + log.error(f"❌ 保存缓存落盘失败 {cache_path}: {e}") diff --git a/media/src/core/constants.py b/media/src/core/constants.py new file mode 100644 index 0000000..f5dc19b --- /dev/null +++ b/media/src/core/constants.py @@ -0,0 +1,31 @@ +# src/core/constants.py + +import re + +class RegexPatterns: + # 1. 结构与目录解析 (Parser) + SEASON_DIR = re.compile(r'^(?:S(?:eason)?[\s\-\_]*)?(\d{1,2})$', re.IGNORECASE) + FLAT_TV = re.compile(r'S(\d{1,2})E\d{1,3}|(?|,!&:,!?、\s]+|\.+') diff --git a/media/src/core/utils.py b/media/src/core/utils.py new file mode 100644 index 0000000..61b08ff --- /dev/null +++ b/media/src/core/utils.py @@ -0,0 +1,46 @@ +# src/core/utils.py + +import json +import subprocess +from pathlib import Path +from core.constants import RegexPatterns + +class StringUtil: + """字符串与文件命名安全处理工具""" + @staticmethod + def sanitize_title(title: str) -> str: + if not title: + return "" + # 接入核心正则 + return RegexPatterns.FILE_SANITIZE.sub('.', title.replace("'", "").replace("’", "")).strip('.') + +class FFprobeUtil: + """FFprobe 探针调用与 JSON 解析工具""" + @staticmethod + def get_json(file_path: Path, *args) -> dict: + cmd = ["ffprobe", "-v", "quiet", "-print_format", "json"] + list(args) + [str(file_path)] + try: + res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + if res.stdout.strip(): + return json.loads(res.stdout) + return {} + except Exception: + return {} + +class MediaUtil: + """媒体文件分析与推断辅助工具""" + @staticmethod + def detect_subtitle_language(sub_path: Path) -> str: + name = sub_path.name.lower() + if RegexPatterns.SUB_LANG_CHT.search(name): return "zh-TW" + if RegexPatterns.SUB_LANG_CHS.search(name): return "zh-CN" + if RegexPatterns.SUB_LANG_ENG.search(name): return "en" + + try: + content = sub_path.read_text(encoding='utf-8', errors='ignore')[:5000] + if RegexPatterns.SUB_LANG_ZH_CHARS.search(content): + return "zh-TW" if len(RegexPatterns.SUB_LANG_CHT_CHARS.findall(content)) > 5 else "zh-CN" + return "und" + except: + return "und" +