Added episode renamer (to S##E##, use at own risk)

This commit is contained in:
Thord Johansson 2024-02-28 11:10:00 +01:00
parent 5e57814205
commit a87ae1f1ac

69
downloads/episodes.py Executable file
View File

@ -0,0 +1,69 @@
#!/usr/bin/python
import os
import sys
import re
def preview_rename(original_name, new_name):
print(f"Preview: {original_name} -> {new_name}")
def perform_rename(original_name, new_name):
os.rename(original_name, new_name)
print(f"Renamed: {original_name} -> {new_name}")
def main():
# Check if the regex is provided as an argument
if len(sys.argv) < 3:
print("Please provide a regex pattern with show title, season, and episode groups.")
sys.exit(1)
# Get folder path from command-line argument if provided, else use current directory
folder_path = sys.argv[1] if len(sys.argv) > 1 else os.getcwd()
# Get the regex pattern from the command-line arguments
regex_pattern = sys.argv[2]
for filename in os.listdir(folder_path):
# Check if the file is a regular file
if os.path.isfile(os.path.join(folder_path, filename)):
# Use regex to extract show title, season, episode, and episode name
match = re.match(regex_pattern, filename)
if match:
show_title = match.group(1)
season_number = match.group(2).zfill(2)
episode_number = match.group(3).zfill(2)
# Check if there is a fourth group for episode name
episode_name_group = match.group(4)
episode_name = f" - {episode_name_group}" if episode_name_group else ""
# Create the new filename in the desired format
new_filename = f"{show_title} S{season_number}E{episode_number}{episode_name}.avi"
# Preview the changes
preview_rename(filename, new_filename)
# Ask for user confirmation
user_input = input("Do you want to proceed with the renaming? (y/n): ").lower()
if user_input == 'y':
# Perform the renaming
for filename in os.listdir(folder_path):
if os.path.isfile(os.path.join(folder_path, filename)):
match = re.match(regex_pattern, filename)
if match:
show_title = match.group(1)
season_number = match.group(2).zfill(2)
episode_number = match.group(3).zfill(2)
episode_name_group = match.group(4)
episode_name = f" - {episode_name_group}" if episode_name_group else ""
new_filename = f"{show_title} S{season_number}E{episode_number}{episode_name}.avi"
perform_rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))
else:
print("Renaming canceled.")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: ./episodes.py <directory> <regex>\nIn the regex, group 1 should be show title, group 2 season and group 3 episode, optionally group 4 for episode title.")
sys.exit(1)
main()