Last active
August 29, 2015 14:15
-
-
Save krmarien/eea144ec061b513dd6d0 to your computer and use it in GitHub Desktop.
TV Show rename
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import argparse | |
import sys | |
import os | |
import re | |
from datetime import datetime | |
def log(text, format='normal'): | |
text_color = { | |
'normal': "\033[0m", | |
'error': "\033[31m", | |
'success': "\033[32m", | |
'warning': "\033[33m", | |
'info': "\033[0m", | |
}[format] | |
print "\033[34m[" + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "] " + text_color + text + "\033[0m" | |
def scan_directory(directory, mode): | |
log('Scanning directory \'' + directory + '\'') | |
for root, sub_folders, files in os.walk(directory): | |
for folder in sub_folders: | |
if folder.startswith('Season'): | |
scan_season(root, folder, mode) | |
elif folder.startswith('Episode'): | |
scan_episode(root, folder, mode) | |
def scan_season(root, directory, mode): | |
log('Scanning season \'' + directory + '\'') | |
for root, sub_folders, files in os.walk(root + '/' + directory): | |
for folder in sub_folders: | |
if ((re.match(r'S[0-9]{2}E[0-9]{2}\s-', folder) or re.match(r'S[0-9]{2}E[0-9]{2}-E[0-9]{2}', folder)) and mode == 'long') or \ | |
(folder.startswith('Episode') and mode == 'short'): | |
scan_episode(root, folder, mode) | |
else: | |
log('Unrecognized directory \'' + folder + '\'', 'error') | |
def scan_episode(root, directory, mode): | |
log('Scanning episode \'' + directory + '\'') | |
if mode == 'short': | |
search = re.search(r'Season\s([0-9]+)', root) | |
if search is not None: | |
season_number = search.group(1) | |
else: | |
season_number = '1' | |
search = re.search(r'^Episode\s([0-9]+)(?:\s&\s([0-9]+))?\s-', directory) | |
if search is None: | |
log('Episode number not found in \'' + directory + '\'', 'error') | |
return | |
episode_number = search.group(1) | |
if search.group(2): | |
code = 'S' + season_number.zfill(2) + 'E' + episode_number.zfill(2) + '-E' + search.group(2).zfill(2) | |
else: | |
code = 'S' + season_number.zfill(2) + 'E' + episode_number.zfill(2) | |
new_directory = re.sub(r'^Episode\s([0-9]+)(?:\s&\s([0-9]+))?\s-', code + ' -', directory) | |
else: | |
search = re.search(r'^S[0-9]{2}E([0-9]{2})(?:-E([0-9]{2}))?\s-', directory) | |
if search is None: | |
log('Episode number not found in \'' + directory + '\'', 'error') | |
return | |
code = search.group(1).lstrip('0') | |
if search.group(2): | |
code += ' & ' + search.group(2).lstrip('0') | |
new_directory = re.sub(r'^S[0-9]{2}E([0-9]{2})(?:-E([0-9]{2}))?\s-', 'Episode ' + code + ' -', directory) | |
rename_episode_files(root, directory, code, mode) | |
log('Rename \'' + directory + '\' to \'' + new_directory + '\'') | |
os.rename(root + '/' + directory, root + '/' + new_directory) | |
def rename_episode_files(root, directory, code, mode): | |
for root, sub_folders, files in os.walk(root + '/' + directory): | |
for file in files: | |
if mode == 'short': | |
new_file = re.sub(r'^Episode\s([0-9]+)(?:\s&\s([0-9]+))?\s-', code + ' -', file) | |
else: | |
new_file = re.sub(r'^S[0-9]{2}E([0-9]{2})(?:-E([0-9]{2}))?\s-', 'Episode ' + code + ' -', file) | |
log('Rename \'' + file + '\' to \'' + new_file + '\'') | |
os.rename(root + '/' + file, root + '/' + new_file) | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-m', '--mode', choices=['long', 'short'], required=True, help='the format to renam to') | |
args = parser.parse_args() | |
scan_directory('TV Shows', args.mode) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment