From ece9132ca12f0d8a33faa2e7d66e2aa861be7465 Mon Sep 17 00:00:00 2001 From: Lucas Faber Date: Mon, 20 Apr 2026 12:57:59 +0200 Subject: [PATCH] fix: directory rename and add pattern --- rename_episodes.py | 49 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/rename_episodes.py b/rename_episodes.py index 763880f..baa9a49 100644 --- a/rename_episodes.py +++ b/rename_episodes.py @@ -2,9 +2,10 @@ import argparse import re import os +import sys from pathlib import Path -from serverLibary import safe_rename,safe_delete +from serverlibary import safe_rename,safe_delete VIDEO_EXTENSIONS = {".mpg", ".avi", ".mkv", ".mp4", ".m4v", ".srt"} @@ -17,7 +18,8 @@ UNWANTED_PATTERNS_POST = [ ] UNWANTED_PATTERNS_PRE = [ - r"www.UIndex.org", r"\[TorrentCounter\.to\]" + r"www.UIndex.org", r"\[TorrentCounter\.to\]", + r"www.Torrenting.org" ] GARBAGE_PATTERNS = [ @@ -68,7 +70,6 @@ def clean_name(name: Path, search=None, replace=None): base = base.strip(".") return base + ext - def capitalize_name(name: str): return re.sub( r"(^|[ ._\-])([a-z])", @@ -76,7 +77,6 @@ def capitalize_name(name: str): name.lower() ) - # ----------------------------- # Operations # ----------------------------- @@ -91,13 +91,12 @@ def rename_files(base: Path, args): new_path = file.with_name(new_name) safe_rename(file, new_path, args.dryrun, args.confirm) - def rename_directories(base: Path, args): # reverse order → avoid renaming parent first - for path in sorted(base.rglob("*"), reverse=True): + paths = [base] + list(base.rglob("*")) + for path in sorted(paths, reverse=True): if not path.is_dir(): continue - new_name = clean_name(path, args.search, args.replace) if args.capitalize: @@ -106,7 +105,6 @@ def rename_directories(base: Path, args): new_path = path.with_name(new_name) safe_rename(path, new_path, args.dryrun, args.confirm) - def repair_names(base: Path, args): for file in iter_media_files(base): new_name = file.name @@ -117,7 +115,6 @@ def repair_names(base: Path, args): new_path = file.with_name(new_name) safe_rename(file, new_path, args.dryrun, args.confirm) - def delete_garbage(base: Path, args): for path in base.rglob("*"): for pattern in GARBAGE_PATTERNS: @@ -126,7 +123,6 @@ def delete_garbage(base: Path, args): if not args.dryrun: safe_delete(path) - # ----------------------------- # CLI # ----------------------------- @@ -161,14 +157,47 @@ def main(): print("Invalid path") return + home = Path.home().resolve() + # block exactly home OR anything inside it (safer) + if base == home or home in base.parents: + print(f"Refusing to run on home directory: {base}") + sys.exit(1) + + #video_files = [] + #for file in iter_media_files(base): + # print(file) + # video_files.append(file) +# + #if len(video_files) == 0: + # print(f"Error: No media files found in {base}") + # sys.exit(1) +# + #for f in video_files: + # p = Path(f).resolve() +# + # # skip files not inside base + # try: + # rel = p.relative_to(base) + # except ValueError: + # continue +# + # # rel.parts includes directories + filename + # # depth 0: (file directly in base) -> len(parts) == 1 + # # depth 1: base/sub/file -> len(parts) == 2 + # if len(rel.parts) <= 2: + # raise ValueError(f"Invalid file location: {p} (inside base or depth 1 subdir)") +# # 1. fix broken names first (optional but recommended) if args.repair: + print("Repair names") repair_names(base, args) # 2. rename directories FIRST (important for path stability) + print("Rename directories") rename_directories(base, args) # 3. rename files AFTER + print("Rename files") rename_files(base, args) # 4. cleanup