-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesnake.py
More file actions
220 lines (158 loc) · 6.67 KB
/
filesnake.py
File metadata and controls
220 lines (158 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import argparse
import os
import requests
import json
import generate_test_data
from pathlib import Path
from natsort import natsorted
parser = argparse.ArgumentParser()
parser.add_argument("-src", "--source",
type=str,
default="Dragon Ball GT",
help="Directory containing files to be renamed",
metavar='')
parser.add_argument("-id", "--showid",
type=str,
help="Provide the TVDB show ID",
metavar='')
generate_test_data.usecase2_test("Dragon Ball GT", 64)
# Variables
args = parser.parse_args()
# Possible flag variables
append_episode_names = True
change_source_path = True # Unused
captial_sande = True # Unused
###########################################################################
def source_path(source):
source = args.source.strip(' " \\ . ')
source = Path(source)
# Check that the directory exists and exit the program if it doesn't
if source.exists() == False:
print('Directory does not exist.')
print('Program terminated.')
exit()
return source.name, source.resolve()
series_name, source_directory = source_path(args.source)
###########################################################################
# request a token from the TVDB
headers={'Content-type':'application/json', 'Accept':'application/json'}
r = requests.post('https://api.thetvdb.com/login', data = '{"apikey":"I7KOIY59UWQL3JPS"}', headers=headers)
token = r.json()['token']
###########################################################################
def series_search():
# Search the TVDB using the given directory name and return
print("Searching the TVDB for " + '"' + series_name + '"....')
headers={'Content-type':'application/json', 'Authorization':'Bearer ' + token}
search_results = requests.get('https://api.thetvdb.com/search/series?name=' + requests.utils.quote(series_name), headers=headers)
#print(search_results.json())
print("")
if search_results.status_code == 404:
print("No results found for " + series_name)
print('Program terminated.')
exit()
elif search_results.status_code == 200:
# List the search results
print("Option - Series Name - Show ID")
for i in range(len(search_results.json()['data'])):
print(i, search_results.json()['data'][i]["seriesName"], search_results.json()['data'][i]["id"])
print()
print("Select an option.")
choice = int(input())
return search_results.json()['data'][choice]["seriesName"], str(search_results.json()['data'][choice]["id"])
series_name, series_id = series_search()
###########################################################################
def create_file_hierarchy():
# return a dictionary describing seasons and episode hierarchy for series_id
file_hierarchy = {}
episode_names = []
# Find the number of seasons for show
headers={'Content-type':'application/json', 'Authorization':'Bearer ' + token}
b = requests.get('https://api.thetvdb.com/series/' + series_id + '/episodes/summary', headers=headers)
seasons = b.json()['data']['airedSeasons']
print(seasons)
# removes the specials season folder if present
if seasons.count("0") > 0:
seasons.remove("0")
# Loop over the seasons starting from Season 1, missing out the specials season
for i in range(1, len(seasons)+1):
episodes = requests.get('https://api.thetvdb.com/series/' + series_id + '/episodes/query?airedSeason='+ str(i), headers=headers)
file_hierarchy[str(i)] = len(episodes.json()['data'])
for i in range(len(episodes.json()['data'])):
episode_names.append(episodes.json()['data'][i]["episodeName"])
return file_hierarchy, episode_names
file_hierarchy, episode_names = create_file_hierarchy()
###########################################################################
def use_case1():
pass
def use_case2():
pass
###########################################################################
# Search the source directory for files and directories and then sort by natural order
for root, dirs, files in os.walk(source_directory):
files = natsorted(files)
print(root)
print(dirs)
print(files)
############################################################################
# Calculate the total episodes in the target show
print("Seasons and Episode Hierarchy")
print("")
total_episodes = 0
for k, v in file_hierarchy.items():
print("Season", k, "Episodes", v)
total_episodes += v
# Check that the number of video files in source matches that of the target show
if len(files) == total_episodes:
print()
print("Total Source files:", len(files))
print("Total Target files:", total_episodes)
print()
print("The total number of target files matches the total number of episodes in the chosen series.")
else:
print()
print("Total Source files:", len(files))
print("Total Target files:", total_episodes)
print()
print("The total number of target files does not match the total number of episodes in the chosen series.")
############################################################################
# Make a list of all the new filenames and list of seasons
new_names = []
seasons = []
for k, v in file_hierarchy.items():
for i in range(1, v+1):
new_names.append(series_name + " - s{:0>2}".format(k) + "e{:0>2}".format(i) )
seasons.append("Season {:0>2}//".format(k))
# Append episode names to filename and file extension
if append_episode_names == True:
for i in range(len(files)):
p = Path(files[i])
new_names[i] = new_names[i] + " - " + episode_names[i].translate({ord(i): None for i in '?*."/\\[]:;|,'}) + p.suffix
else:
for i in range(len(files)):
p = Path(files[i])
new_names[i] = new_names[i] + p.suffix
# Preview the changes
for i in range(len(files)):
print(source_directory / files[i])
print(source_directory / seasons[i] / new_names[i])
print()
# Confirm the file changes
print()
print("Would you like to proceed with these file changes? y/n")
response = input()
# Proceed with renaming the files
if response == "y" or response == "Y":
# create empty season folders
for k, v in file_hierarchy.items():
season_path = source_directory / "Season {:0>2}".format(k)
os.mkdir(season_path)
# Perform the renaming using path objects
for i in range(len(files)):
src = source_directory / files[i]
dst = source_directory / seasons[i] / new_names[i]
os.rename(src, dst)
print()
print("Operation completed")
else:
print()
print("Operation aborted")