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" +