fix: directory rename and add pattern
This commit is contained in:
1 parent
a56a4dfbf6
commit
ece9132ca1
1 file changed
+39
-10
+39
-10
@@ -2,9 +2,10 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
from pathlib import Path
|
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"}
|
VIDEO_EXTENSIONS = {".mpg", ".avi", ".mkv", ".mp4", ".m4v", ".srt"}
|
||||||
|
|
||||||
@@ -17,7 +18,8 @@ UNWANTED_PATTERNS_POST = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
UNWANTED_PATTERNS_PRE = [
|
UNWANTED_PATTERNS_PRE = [
|
||||||
r"www.UIndex.org", r"\[TorrentCounter\.to\]"
|
r"www.UIndex.org", r"\[TorrentCounter\.to\]",
|
||||||
|
r"www.Torrenting.org"
|
||||||
]
|
]
|
||||||
|
|
||||||
GARBAGE_PATTERNS = [
|
GARBAGE_PATTERNS = [
|
||||||
@@ -68,7 +70,6 @@ def clean_name(name: Path, search=None, replace=None):
|
|||||||
base = base.strip(".")
|
base = base.strip(".")
|
||||||
return base + ext
|
return base + ext
|
||||||
|
|
||||||
|
|
||||||
def capitalize_name(name: str):
|
def capitalize_name(name: str):
|
||||||
return re.sub(
|
return re.sub(
|
||||||
r"(^|[ ._\-])([a-z])",
|
r"(^|[ ._\-])([a-z])",
|
||||||
@@ -76,7 +77,6 @@ def capitalize_name(name: str):
|
|||||||
name.lower()
|
name.lower()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Operations
|
# Operations
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
@@ -91,13 +91,12 @@ def rename_files(base: Path, args):
|
|||||||
new_path = file.with_name(new_name)
|
new_path = file.with_name(new_name)
|
||||||
safe_rename(file, new_path, args.dryrun, args.confirm)
|
safe_rename(file, new_path, args.dryrun, args.confirm)
|
||||||
|
|
||||||
|
|
||||||
def rename_directories(base: Path, args):
|
def rename_directories(base: Path, args):
|
||||||
# reverse order → avoid renaming parent first
|
# 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():
|
if not path.is_dir():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
new_name = clean_name(path, args.search, args.replace)
|
new_name = clean_name(path, args.search, args.replace)
|
||||||
|
|
||||||
if args.capitalize:
|
if args.capitalize:
|
||||||
@@ -106,7 +105,6 @@ def rename_directories(base: Path, args):
|
|||||||
new_path = path.with_name(new_name)
|
new_path = path.with_name(new_name)
|
||||||
safe_rename(path, new_path, args.dryrun, args.confirm)
|
safe_rename(path, new_path, args.dryrun, args.confirm)
|
||||||
|
|
||||||
|
|
||||||
def repair_names(base: Path, args):
|
def repair_names(base: Path, args):
|
||||||
for file in iter_media_files(base):
|
for file in iter_media_files(base):
|
||||||
new_name = file.name
|
new_name = file.name
|
||||||
@@ -117,7 +115,6 @@ def repair_names(base: Path, args):
|
|||||||
new_path = file.with_name(new_name)
|
new_path = file.with_name(new_name)
|
||||||
safe_rename(file, new_path, args.dryrun, args.confirm)
|
safe_rename(file, new_path, args.dryrun, args.confirm)
|
||||||
|
|
||||||
|
|
||||||
def delete_garbage(base: Path, args):
|
def delete_garbage(base: Path, args):
|
||||||
for path in base.rglob("*"):
|
for path in base.rglob("*"):
|
||||||
for pattern in GARBAGE_PATTERNS:
|
for pattern in GARBAGE_PATTERNS:
|
||||||
@@ -126,7 +123,6 @@ def delete_garbage(base: Path, args):
|
|||||||
if not args.dryrun:
|
if not args.dryrun:
|
||||||
safe_delete(path)
|
safe_delete(path)
|
||||||
|
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# CLI
|
# CLI
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
@@ -161,14 +157,47 @@ def main():
|
|||||||
print("Invalid path")
|
print("Invalid path")
|
||||||
return
|
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)
|
# 1. fix broken names first (optional but recommended)
|
||||||
if args.repair:
|
if args.repair:
|
||||||
|
print("Repair names")
|
||||||
repair_names(base, args)
|
repair_names(base, args)
|
||||||
|
|
||||||
# 2. rename directories FIRST (important for path stability)
|
# 2. rename directories FIRST (important for path stability)
|
||||||
|
print("Rename directories")
|
||||||
rename_directories(base, args)
|
rename_directories(base, args)
|
||||||
|
|
||||||
# 3. rename files AFTER
|
# 3. rename files AFTER
|
||||||
|
print("Rename files")
|
||||||
rename_files(base, args)
|
rename_files(base, args)
|
||||||
|
|
||||||
# 4. cleanup
|
# 4. cleanup
|
||||||
|
|||||||
Reference in new issue
Block a user