|
| 1 | +import os |
| 2 | +import imdb |
| 3 | +import argparse |
| 4 | +from termcolor import colored |
| 5 | + |
| 6 | +arg_parser = argparse.ArgumentParser( |
| 7 | + usage='python rename.py [-h] [options]') |
| 8 | + |
| 9 | +arg_parser.add_argument( |
| 10 | + '-c', |
| 11 | + '--code', |
| 12 | + type=str, |
| 13 | + help='IMDb CODE for the Series', |
| 14 | + required=False, |
| 15 | + metavar='') |
| 16 | + |
| 17 | +arg_parser.add_argument( |
| 18 | + '-e', |
| 19 | + '--episode', |
| 20 | + type=str, |
| 21 | + help='Rename EPISODES of the series in the specified Directory', |
| 22 | + default=False, |
| 23 | + metavar='') |
| 24 | + |
| 25 | +arg_parser.add_argument('-s', '--subtitle', type=str, |
| 26 | + help='Rename SUBTITLES of the Series in the Directory', |
| 27 | + default=False, metavar='') |
| 28 | + |
| 29 | +arg_parser.add_argument( |
| 30 | + '-t', |
| 31 | + '--tag', |
| 32 | + type=str, |
| 33 | + help='Rename VIDEO TAGS for MKV Videos', |
| 34 | + required=False, |
| 35 | + metavar='') |
| 36 | + |
| 37 | +args = arg_parser.parse_args() |
| 38 | + |
| 39 | +not_allowed = r":\*<>\|" |
| 40 | + |
| 41 | + |
| 42 | +def get_episodes(imdb_code): |
| 43 | + ia = imdb.IMDb() |
| 44 | + if "tt" in imdb_code: |
| 45 | + imdb_code = imdb_code[2:] |
| 46 | + |
| 47 | + series = ia.get_movie(imdb_code) |
| 48 | + print("SERIES: {}".format(colored(series, "magenta"))) |
| 49 | + |
| 50 | + ia.update(series, "episodes") |
| 51 | + episodes = series.data['episodes'] |
| 52 | + |
| 53 | + episode_list = [] |
| 54 | + |
| 55 | + for season in episodes.keys(): |
| 56 | + for episode in episodes[season]: |
| 57 | + title = episodes[season][episode]['title'] |
| 58 | + |
| 59 | + if season < 10: |
| 60 | + if episode < 10: |
| 61 | + ep_name = "{} S0{}E0{} - {}".format(series, |
| 62 | + season, episode, title) |
| 63 | + else: |
| 64 | + ep_name = "{} S0{}E{} - {}".format(series, |
| 65 | + season, episode, title) |
| 66 | + else: |
| 67 | + if episode < 10: |
| 68 | + ep_name = "{} S{}E0{} - {}".format(series, |
| 69 | + season, episode, title) |
| 70 | + else: |
| 71 | + ep_name = "{} S{}E{} - {}".format(series, |
| 72 | + season, episode, title) |
| 73 | + |
| 74 | + for i in ep_name: |
| 75 | + if i in not_allowed: |
| 76 | + ep_name = ep_name.replace(i, " -") |
| 77 | + if i in "\\/": |
| 78 | + ep_name = ep_name.replace(i, "-") |
| 79 | + if i in "?": |
| 80 | + ep_name = ep_name.replace(i, "") |
| 81 | + episode_list.append(ep_name) |
| 82 | + return episode_list |
| 83 | + |
| 84 | + |
| 85 | +def rename_episode(directory, episode_list): |
| 86 | + files = os.listdir(directory) |
| 87 | + |
| 88 | + print("Renaming episodes...") |
| 89 | + |
| 90 | + for i in range(len(files)): |
| 91 | + ep_name = episode_list[i] |
| 92 | + og_name = files[i] |
| 93 | + ext = og_name[-4:] |
| 94 | + if ext != ".srt": |
| 95 | + ep_name += ext |
| 96 | + filename = os.path.join(directory, og_name) |
| 97 | + newfilename = os.path.join(directory, ep_name) |
| 98 | + print("Renaming {} to {}".format( |
| 99 | + colored(filename, 'yellow'), |
| 100 | + colored(newfilename, 'green'))) |
| 101 | + os.rename(filename, newfilename) |
| 102 | + print( |
| 103 | + colored( |
| 104 | + "RENAMING", "red"), colored( |
| 105 | + "EPISODES", "cyan"), colored( |
| 106 | + "FINISHED!\n", "red")) |
| 107 | + |
| 108 | + |
| 109 | +def rename_subtitle(directory, episode_list): |
| 110 | + files = os.listdir(directory) |
| 111 | + print("Renaming subtitles...") |
| 112 | + |
| 113 | + for i in range(len(files)): |
| 114 | + ep_name = episode_list[i] |
| 115 | + og_name = files[i] |
| 116 | + ext = og_name[-4:] |
| 117 | + if ext == ".srt": |
| 118 | + ep_name += ext |
| 119 | + filename = os.path.join(directory, og_name) |
| 120 | + newfilename = os.path.join(directory, ep_name) |
| 121 | + print( |
| 122 | + "Renaming {} to {}".format( |
| 123 | + colored( |
| 124 | + filename, 'yellow'), colored( |
| 125 | + newfilename, 'green'))) |
| 126 | + os.rename(filename, newfilename) |
| 127 | + print( |
| 128 | + colored( |
| 129 | + "RENAMING", "red"), colored( |
| 130 | + "SUBTITLES", "cyan"), colored( |
| 131 | + "FINISHED!\n", "red")) |
| 132 | + |
| 133 | + |
| 134 | +def edit_tag(directory): |
| 135 | + for file in os.listdir(directory): |
| 136 | + if ".mkv" in file: |
| 137 | + title = file.replace(".mkv", "") |
| 138 | + command = r'mkvpropedit "{}\{}" --set "title={}"'.format( |
| 139 | + directory, file, title) |
| 140 | + os.system(command) |
| 141 | + |
| 142 | + if ".mp4" in file: |
| 143 | + title = file.replace(".mp4", "") |
| 144 | + command = r'mkvpropedit "{}\{}" --set "title={}"'.format( |
| 145 | + directory, file, title) |
| 146 | + os.system(command) |
| 147 | + |
| 148 | + print( |
| 149 | + colored( |
| 150 | + "RENAMING", "red"), colored( |
| 151 | + "VIDEO TAGS", "cyan"), colored( |
| 152 | + "FINISHED!\n", "red")) |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + |
| 157 | + if args.code: |
| 158 | + episodes = get_episodes(args.code) |
| 159 | + |
| 160 | + if args.episode: |
| 161 | + args.episode = r'{}'.format(args.episode) |
| 162 | + rename_episode(args.episode, episodes) |
| 163 | + |
| 164 | + if args.subtitle: |
| 165 | + args.subtitle = r'{}'.format(args.subtitle) |
| 166 | + rename_subtitle(args.subtitle, episodes) |
| 167 | + |
| 168 | + if args.tag: |
| 169 | + args.tag = r'{}'.format(args.tag) |
| 170 | + edit_tag(args.tag) |
0 commit comments