fix(variety): 按 TMDB 季号归位 + 附属文件跟随/认领 + 跳过 extras
- 特典不再留在正季目录:统一按 TMDB 季号放进 Season 00 / Season XX (此前只处理「根目录」的文件,Season 04 里的 S00Exx 特典一直不挪窝) - 视频改名时同步改名附属文件:<旧名>.nfo/.ass/-thumb.jpg → <新名>.xxx - 老名字的附属文件按「同一日期」兜底认领: 花儿与少年 74 个 nfo 中 71 个修复,其余 3 个是没有对应视频的孤儿 - 扫描跳过 extras/ 等额外内容目录(此前会把 extras 里的文件拖出来改名) - 实测:你好星期六 198 个特典从 Season 04/05 移入 Season 00,正片剩 51+35
This commit is contained in:
@@ -21,6 +21,7 @@ from collections import defaultdict
|
||||
from pathlib import Path
|
||||
|
||||
from core.logger import log
|
||||
from core import config
|
||||
from core import tmdb_store as store
|
||||
from core.utils import StringUtil
|
||||
from services.mediainfo import MediaInfoService
|
||||
@@ -31,8 +32,10 @@ _DATE_FULL = [
|
||||
re.compile(r"(20\d{2})[-._年/](\d{1,2})[-._月/](\d{1,2})"),
|
||||
re.compile(r"(?<!\d)(20\d{2})(\d{2})(\d{2})(?!\d)"),
|
||||
]
|
||||
# 只有 月.日(无年份),例如 S00E017.02.24出发
|
||||
_DATE_SHORT = re.compile(r"(?<!\d)(\d{1,2})[.\-_/](\d{1,2})(?!\d)")
|
||||
# 只有 月.日(无年份),例如 S00E017.02.24出发。
|
||||
# 必须位于开头或分隔符之后——否则 "S01E02.2020" 里的 02.20 会被误当成 2 月 20 日。
|
||||
_DATE_SHORT = re.compile(
|
||||
r"(?:^|(?<=[.\s_\-()\[\]]))(\d{1,2})[.\-_/](\d{1,2})(?![0-9])")
|
||||
|
||||
# 特典类关键词:仅用于「同日期多个候选」时的排序,不用于判定归属
|
||||
_SPECIAL_WORDS = re.compile(r"加更|会员|特别|特辑|企划|花絮|彩蛋|先导|前瞻|精编|回顾|纯享|未播")
|
||||
@@ -57,8 +60,10 @@ class VarietyService:
|
||||
continue
|
||||
match = _DATE_SHORT.search(name)
|
||||
if match:
|
||||
month, day = match.groups()
|
||||
return f"????-{int(month):02d}-{int(day):02d}", False
|
||||
month, day = int(match.group(1)), int(match.group(2))
|
||||
# 只接受合法的月/日,避免把集号、分辨率之类的数字当成日期
|
||||
if 1 <= month <= 12 and 1 <= day <= 31:
|
||||
return f"????-{month:02d}-{day:02d}", False
|
||||
return None, False
|
||||
|
||||
@staticmethod
|
||||
@@ -169,13 +174,29 @@ class VarietyService:
|
||||
index = cls.build_index(show_id)
|
||||
videos = [p for p in sorted(folder.rglob("*"))
|
||||
if p.is_file() and p.suffix.lower() in cls.VIDEO_EXTS
|
||||
and ".qkdownloading" not in p.name]
|
||||
and ".qkdownloading" not in p.name
|
||||
and not (set(part.lower() for part in p.relative_to(folder).parts[:-1])
|
||||
& set(config.EXTRAS_DIRS))]
|
||||
|
||||
# 按日期分组;无日期的先放着
|
||||
groups: dict[str, list[Path]] = defaultdict(list)
|
||||
undated: list[Path] = []
|
||||
|
||||
# 反查表:(季, 集) → 播出日期。库里已整理过的文件名没有日期,
|
||||
# 但带着 SxxEyy,可以用它反查日期,进而补上「-日期.期名」尾巴。
|
||||
ep_index: dict[tuple[str, int], str] = {}
|
||||
for air, candidates in index.items():
|
||||
for cand in candidates:
|
||||
ep_index[(cand["season"], cand["ep"])] = air
|
||||
|
||||
for file in videos:
|
||||
date, complete = cls.extract_date(file.name)
|
||||
# 已有 SxxEyy 时以集号为准(文件名里的其它数字可能被误当成日期)
|
||||
match = re.search(r"S(\d{2})E(\d{1,4})", file.stem, re.I)
|
||||
if match:
|
||||
by_ep = ep_index.get((match.group(1), int(match.group(2))))
|
||||
if by_ep:
|
||||
date, complete = by_ep, True
|
||||
if date and not complete:
|
||||
hint = cls.season_hint(file.relative_to(folder)) or cls.season_hint(folder)
|
||||
year = season_years.get(hint or "", "")
|
||||
@@ -287,19 +308,85 @@ class VarietyService:
|
||||
log.info(f" [删除] {file.name[:60]} ← {reason}")
|
||||
log.info("=" * 60)
|
||||
|
||||
@classmethod
|
||||
def _follow_sidecars(cls, src_video: Path, dst_video: Path, moved: bool = False) -> int:
|
||||
"""让附属文件跟着视频走:<旧名>.xxx → <新名>.xxx。返回处理数量。"""
|
||||
count = 0
|
||||
for side in list(src_video.parent.iterdir()):
|
||||
if not side.is_file() or side.suffix.lower() not in config.SIDECAR_EXTS:
|
||||
continue
|
||||
if not side.name.startswith(src_video.stem):
|
||||
continue
|
||||
rest = side.name[len(src_video.stem):]
|
||||
if rest and rest[0].isalnum():
|
||||
continue
|
||||
target_side = dst_video.parent / f"{dst_video.stem}{rest}"
|
||||
if target_side == side:
|
||||
continue
|
||||
try:
|
||||
target_side.parent.mkdir(parents=True, exist_ok=True)
|
||||
side.rename(target_side)
|
||||
count += 1
|
||||
except OSError as exc:
|
||||
log.warning(f"⚠️ 附属文件改名失败 {side.name}: {exc}")
|
||||
return count
|
||||
|
||||
@classmethod
|
||||
def adopt_orphan_sidecars(cls, folder: Path, stats: dict) -> int:
|
||||
"""兜底认领:老名字的附属文件(视频早已改名)按「同一日期」认领到对应视频。
|
||||
|
||||
典型场景:花儿与少年的 .nfo 还叫 花儿与少年.S01E20220610.nfo,
|
||||
而视频已经变成 …-20220610.第四季…,两者共享同一个 YYYYMMDD。
|
||||
"""
|
||||
videos = [p for p in folder.rglob("*")
|
||||
if p.is_file() and p.suffix.lower() in cls.VIDEO_EXTS]
|
||||
by_date: dict[str, Path] = {}
|
||||
for video in videos:
|
||||
match = re.search(r"-(\d{8})\.", video.name)
|
||||
if match:
|
||||
by_date.setdefault(match.group(1), video)
|
||||
video_stems = {p.stem for p in videos}
|
||||
|
||||
adopted = 0
|
||||
for side in list(folder.rglob("*")):
|
||||
if not side.is_file() or side.suffix.lower() not in config.SIDECAR_EXTS:
|
||||
continue
|
||||
if side.stem in video_stems or side.stem in {"season", "tvshow"}:
|
||||
continue
|
||||
match = re.search(r"(\d{8})", side.name)
|
||||
if not match:
|
||||
continue
|
||||
video = by_date.get(match.group(1))
|
||||
if not video or video.parent != side.parent:
|
||||
continue
|
||||
rest = re.sub(r"^.*?\d{8}", "", side.stem)
|
||||
target = video.with_name(f"{video.stem}{rest}{side.suffix.lower()}")
|
||||
if target == side or target.exists():
|
||||
continue
|
||||
try:
|
||||
side.rename(target)
|
||||
adopted += 1
|
||||
except OSError as exc:
|
||||
log.warning(f"⚠️ 附属文件认领失败 {side.name}: {exc}")
|
||||
if adopted:
|
||||
stats["sidecars"] = stats.get("sidecars", 0) + adopted
|
||||
return adopted
|
||||
|
||||
@classmethod
|
||||
def execute(cls, plan: dict, season_years: dict, media_title: str) -> dict:
|
||||
"""执行改名 / 移入 extras / 删除。返回统计。"""
|
||||
folder: Path = plan["folder"]
|
||||
extras_dir = folder / "extras"
|
||||
stats = {"renamed": 0, "extras": 0, "deleted": 0, "skipped": 0}
|
||||
stats = {"renamed": 0, "extras": 0, "deleted": 0, "skipped": 0, "sidecars": 0}
|
||||
|
||||
for file, candidate in plan["renames"]:
|
||||
target = file.with_name(cls._target_name(file, candidate, media_title, season_years))
|
||||
# 散在剧集根目录的正片按季归位:根目录 → Season XX
|
||||
if file.parent == folder and candidate["season"]:
|
||||
# 一律按 TMDB 季号归位:特典进 Season 00,正片进 Season XX。
|
||||
# 不这样做的话,「源文件本来就在 Season 04」的特典会一直留在 Season 04。
|
||||
if candidate["season"]:
|
||||
target = folder / f"Season {candidate['season']}" / target.name
|
||||
if target == file:
|
||||
cls._follow_sidecars(file, target)
|
||||
continue
|
||||
if target.exists():
|
||||
log.warning(f"⚠️ 目标已存在,跳过: {target.name}")
|
||||
@@ -309,6 +396,7 @@ class VarietyService:
|
||||
file.rename(target)
|
||||
log.info(f"✅ 改名: {file.name} → {target.relative_to(folder)}")
|
||||
stats["renamed"] += 1
|
||||
stats["sidecars"] = stats.get("sidecars", 0) + cls._follow_sidecars(file, target, moved=True)
|
||||
|
||||
for file in plan["extras"]:
|
||||
extras_dir.mkdir(parents=True, exist_ok=True)
|
||||
@@ -330,6 +418,9 @@ class VarietyService:
|
||||
log.warning(f"⚠️ 删除失败 {file.name}: {exc}")
|
||||
stats["skipped"] += 1
|
||||
|
||||
# 老名字的附属文件按日期兜底认领
|
||||
cls.adopt_orphan_sidecars(folder, stats)
|
||||
|
||||
# 留一个标识:综艺目录名往往不规范(「花儿与少年」会误匹配到衍生剧),
|
||||
# 常规扫描看到 .variety.json 就按这里记录的 id 识别,不会再用错剧名。
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user