Refresh and added usage tracking
This commit is contained in:
Executable
+78
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Usage:
|
||||
remove_fname_last_n <number> (<directory>)
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import os
|
||||
from termcolor import colored
|
||||
|
||||
VIDEO_EXTENSIONS = {
|
||||
".mkv", ".mp4", ".avi", ".mov", ".wmv", ".flv", ".webm", ".m4v"
|
||||
}
|
||||
|
||||
def main():
|
||||
|
||||
args = sys.argv[1:]
|
||||
|
||||
undo = "--undo" in args
|
||||
if undo:
|
||||
args.remove("--undo")
|
||||
|
||||
if len(args) == 0:
|
||||
number = 0
|
||||
directory = Path(os.getcwd())
|
||||
elif len(args) == 1:
|
||||
try:
|
||||
number = int(args[0])
|
||||
directory = Path(os.getcwd())
|
||||
except:
|
||||
number = 0
|
||||
directory = Path(args[0])
|
||||
elif len(args) == 2:
|
||||
try:
|
||||
number = int(args[0])
|
||||
directory = Path(args[1])
|
||||
except:
|
||||
number = int(args[1])
|
||||
directory = Path(args[0])
|
||||
else:
|
||||
print("Usage: remove_fname_last_n <number> (<directory>)")
|
||||
sys.exit(1)
|
||||
|
||||
if not directory.exists():
|
||||
print(colored("Directory does not exist", "red"))
|
||||
sys.exit(1)
|
||||
|
||||
import subprocess
|
||||
subprocess.run(["python", "/home/honney/.bin/tracker.py", "add", "remove_fname_last_n"])
|
||||
|
||||
files = sorted(
|
||||
[f for f in directory.iterdir()
|
||||
if f.is_file() and f.suffix.lower() in VIDEO_EXTENSIONS],
|
||||
key=lambda f: f.name
|
||||
)
|
||||
|
||||
if not files:
|
||||
print(colored("No video files found", "yellow"))
|
||||
return
|
||||
|
||||
for f in files:
|
||||
new_name = f.parent/Path(f.stem[:-number]+f.suffix)
|
||||
if new_name.exists():
|
||||
print(colored(f"File {new_name.name} already exists", "red"))
|
||||
else:
|
||||
print(
|
||||
colored("Renamed: ", "green") +
|
||||
colored(f.name, "red") +
|
||||
colored(" -> ", "white") +
|
||||
colored(new_name.name, "green")
|
||||
)
|
||||
|
||||
f.rename(new_name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user