Refresh and added usage tracking

This commit is contained in:
Hannes
2026-04-24 00:47:51 +02:00
parent 8519d9f62e
commit 477b1bf985
45 changed files with 726 additions and 166 deletions
+94
View File
@@ -0,0 +1,94 @@
#!/usr/bin/env python3
import sys
import re
from pathlib import Path
EP_REGEX = re.compile(r'(E)(\d{4})(.*)')
def main():
args = sys.argv[1:]
dry_run = False
if args and args[0] in ("--dry-run", "-n"):
dry_run = True
args = args[1:]
if len(args) != 3:
print("Usage: script.py [--dry-run|-n] <directory> <threshold> <subtract>")
sys.exit(1)
directory = Path(args[0])
threshold = int(args[1])
subtract = int(args[2])
if not directory.is_dir():
print(f"Error: '{directory}' is not a directory.")
sys.exit(1)
# Step 1: build array of original filenames and parsed numbers
old_array = []
for f in sorted(directory.iterdir()):
if not f.is_file():
continue
m = EP_REGEX.match(f.name)
if m:
prefix, num_str, rest = m.groups()
num = int(num_str)
old_array.append([prefix, num, rest])
# Step 2: compute new filenames
new_array = []
for prefix, num, rest in old_array:
if num >= threshold:
new_num = num - subtract
else:
new_num = num
if new_num < 0 or new_num > 9999:
raise ValueError(f"Invalid resulting number for file: {prefix}{num:04d}{rest}")
new_array.append([prefix, new_num, rest])
if subtract < 0:
new_array.reverse()
old_array.reverse()
# Step 3: sequential rename simulation / real rename
for i in range(len(old_array)):
old_name = f"{old_array[i][0]}{old_array[i][1]:04d}{old_array[i][2]}"
new_name = f"{new_array[i][0]}{new_array[i][1]:04d}{new_array[i][2]}"
for j in range(i):
if old_array[j][0] == new_array[i][0] and old_array[j][1] == new_array[i][1] and old_array[j][2] == new_array[i][2]:
raise RuntimeError(
f"Rename conflict in simulation:\n Source: {old_name}\n Target: {new_name}\n Conflicts with: {old_array[j][0]}{old_array[j][1]:04d}{old_array[j][2]}"
)
if old_array[j][1] > new_array[i][1]:
break
print(f"\"{old_name}\" -> \"{new_name}\"", end=" ")
if old_name == new_name:
print("Skipped")
continue
else:
print()
# Update simulated old_array to reflect that this file has been "renamed"
old_array[i][1] = new_array[i][1]
# Actually rename unless dry-run
if not dry_run:
target_path = directory / new_name
if target_path.exists():
raise RuntimeError(f"Rename would overwrite existing file on disk:\n Source: {old_name}\n Target: {new_name}")
(directory / old_name).rename(target_path)
if dry_run:
print("Dry-run complete. No files were renamed.")
else:
print("Done.")
if __name__ == "__main__":
import subprocess
subprocess.run(["python", "/home/honney/.bin/tracker.py", "add", "substract_episode_num"])
main()