-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRENAME-Series.py
334 lines (300 loc) · 10.3 KB
/
RENAME-Series.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
Code Written by ~KK~
To Automate the boring process of renaming files for your TV Shows library
Useful for organising bulk media files or frequent updation of
your XBMC library (Like Kodi, Plex and OSMC)
"""
import os
import re
import msvcrt
import shutil
import getopt
import sys
from imdb import IMDb
from similarity.damerau import Damerau
# Title
def Title():
os.system('mode con: cols=90 lines=30')
os.system("cls")
print(" _ _ _ _ ")
print(" /_\ _ _| |_ ___ _ __ __ _| |_ ___ __| | ")
print(" / _ \ || | _/ _ \ ' \/ _` | _/ -_) _` | ")
print(" /_/ \_\_,_|\__\___/_|_|_\__,_|\__\___\__,_| ")
print(" ")
print(" _ _ _ ")
print(" | | (_) |__ _ _ __ _ _ _ _ _ ")
print(" | |__| | '_ \ '_/ _` | '_| || | ")
print(" |____|_|_.__/_| \__,_|_| \_, | ")
print(" |__/ ")
print(" ___ _ ")
print(" / _ \ _ _ __ _ __ _ _ _ (_)___ ___ _ _ ")
print(" | (_) | '_/ _` / _` | ' \| (_-</ -_) '_|")
print(" \___/|_| \__, \__,_|_||_|_/__/\___|_| ")
print(" |___/ ")
print("")
# Find most apt name in Series List
def find_most_apt(name, series):
damerau = Damerau()
deg = []
for ss in series:
if(name.upper() == ss.upper()):
return(ss)
else:
deg.append(damerau.distance(name.upper(), ss.upper()))
indd = int(deg.index(min(deg)))
mostapt = series[indd]
return(mostapt)
# Retrieves name from imDB
def main_imdb(str21):
ia = IMDb()
s_result = ia.search_movie(str21)
series = []
for ss in s_result:
if(ss['kind'] == "tv series"):
str2 = ss['title']
series.append(str2)
return(series)
# Remove illegal characters from file name
def removeIllegal(str):
str=str.replace('<',"")
str=str.replace('>',"")
str=str.replace(':',"")
str=str.replace('"',"")
str=str.replace('/',"")
str=str.replace('\\',"")
str=str.replace('|',"")
str=str.replace('?',"")
str=str.replace('*',"")
str=str.strip()
return(str)
# has Condition #1
def hasX(inputString):
return bool(re.search(r'\dx\d', inputString) or re.search(r'\d x \d', inputString))
# has Condition #2
def hasSE(inputString):
return bool(re.search(r'S\d\dE\d\d', inputString) or re.search(r'S\dE\d\d', inputString) or re.search(r's\d\de\d\d', inputString) or re.search(r's\de\d\d', inputString))
# has Condition #3
def hasSEP(inputString):
return bool(re.search(r'S\d\dEP\d\d', inputString) or re.search(r'S\dEP\d\d', inputString) or re.search(r's\d\dep\d\d', inputString) or re.search(r's\dep\d\d', inputString))
def FindName(inputString):
inputString = inputString.replace(' x ', 'x', 1)
filteredList = filter(None, re.split(r'(\dx\d)', inputString))
for element in filteredList:
Name = element
Name = Name.replace('-',' ')
Name = Name.replace('.',' ')
Name = Name.strip()
return str(Name)
def FindDet(inputString):
inputString = inputString.replace(' x ', 'x', 1)
filteredList = filter(None, re.split(r'(\dx\d\d)', inputString))
i=0
for element in filteredList:
Det = element
Det = Det.replace('.',' ')
Det = Det.replace('-',' ')
Det = Det.strip()
if(i==1):
return str(Det)
i=i+1
def FindSeason(inputString):
Det = FindDet(inputString)
Season = Det.split('x')[0]
return str(Season)
def FindEpisode(inputString):
Det = FindDet(inputString)
Episode = Det.split('x')[1]
return str(Episode)
def AddZero(inputString):
if(int(inputString) <10):
return str('0' + str(int(inputString)))
return(inputString)
def split_line(text):
words = text.split()
return words
# Before Driver
def init(path="NULL"):
Title()
try:
os.mkdir(str(os.getcwd())+"\\Input")
except:
pass
try:
os.mkdir(str(os.getcwd())+"\\Input\\Series")
except:
pass
main(path)
# Driver Code
def main(path="NULL"):
if path == "NULL":
cwd = str(os.getcwd())
path = cwd+"\\Input\\Series\\"
case = 1
else:
cwd = path
print(path)
case = 2
path_new = path_new_1 = rest = Final = "NULL"
NAME=""
Copy=""
i=0
ErrorFlag=0
FileFlag=0
print("Reading Files....")
# main loop
for (dirpath, dirnames, filenames) in os.walk(path):
files = os.listdir(dirpath)
for file in files:
CheckFlag=0
temp = file
extn = file[(len(file)-4) : len(file)]
"""Logic Starts here"""
# All possible media extensions go here
media_extensions = [
".mp4",
".mkv",
".srt",
".avi",
".wmv"
]
if (file.endswith(ex1) for ex1 in media_extensions):
Title()
print(str(i)+" File(s) Processed....")
rest = temp.split(extn,1)[0]
unwanted_stuff = [
".1080p",
".720p",
"HDTV",
"x264",
"AAC",
"E-Subs",
"ESubs",
"WEBRip",
"WEB",
"BluRay",
]
for stuff in unwanted_stuff:
rest = rest.replace(stuff,"")
rest = rest.replace(".", " ")
# Specifically written for'x' type Files
if hasX(file):
CheckFlag=1
NAME = FindName(rest)
SEASON = FindSeason(rest)
EPISODE = AddZero(FindEpisode(rest))
Final = "S"+AddZero(FindSeason(rest))+"E"+EPISODE+extn
# Specifically written for 'S__E__' type Files
elif hasSE(file):
NAME=""
words = split_line(rest)
for word in words:
if(hasSE(word)):
Final = word
break
if(hasSEP(word)):
Final = word
break
else:
NAME = NAME + word + " "
brackets_ = re.findall('[(]+(.*?)[)]', NAME)
# print(brackets_)
for yy in brackets_:
try:
NAME = NAME.replace(yy,"")
except:
pass
if(NAME != Copy):
Copy = NAME
series = main_imdb(NAME)
NAME = find_most_apt(NAME, series)
NAME = removeIllegal(NAME)
NAME = NAME.strip()
Restore = NAME
else:
NAME = Restore
TEMP=Final=Final.strip()
TEMP=TEMP.replace('S',"")
SEASON = TEMP.split('E',1)[0]
EPISODE = TEMP.split('E',1)[1]
Final = Final + extn
CheckFlag=1
if CheckFlag == 1:
if case == 1:
path_new = cwd + "\\Output\\Series\\" + NAME
path_new_1 = cwd + "\\Output\\Series\\" + NAME +"\\Season "+ str(int(SEASON))
elif case == 2:
path_new = cwd + "\\" + NAME
path_new_1 = cwd + "\\" + NAME +"\\Season "+ str(int(SEASON))
"""Naming Logic Ends here"""
# For Testing
def TEST():
print ("\n\n")
print("NAME: " + NAME)
print("SEASON: " + SEASON)
print("EPISODE: " + EPISODE)
print("EXTN: " + extn)
# print("TEMP: " + temp)
print("REST: " + rest)
print("Final: " + Final)
print("PATH: "+ path_new)
print ("\n\n")
# TEST() # Test Call
if CheckFlag == 1:
if case == 1:
try:
os.mkdir(cwd+"\\Output")
except FileExistsError:
pass
try:
os.mkdir(cwd+"\\Output\\Series")
except FileExistsError:
pass
try:
os.mkdir(path_new)
except FileExistsError:
pass
try:
os.mkdir(path_new_1)
except FileExistsError:
pass
try:
os.rename(os.path.join(dirpath, file), os.path.join(path_new_1, Final ))
except FileExistsError:
print("Error - File Already Exist: \\"+ NAME +"\\Season "+ str(int(SEASON)) + "\\" + Final)
FileFlag=1
ErrorFlag=1
i=i-1;
i=i+1
"""Result Generation"""
Title()
print ("All Files Processed...")
if(FileFlag==1):
print("Solution: Try again after removing the above file(s) from Output folder")
if(i>0 or ErrorFlag==1):
print(str(i)+" File(s) Renamed and Organised Successfully")
# os.system("explorer.exe " + str(os.getcwd() + "\Output\\"))
else:
print("No Media File Found in Input Folder")
# Prints usage/help
def usageHelp():
print('\nUsage:\n RENAME-Series.py [options] <commands (or) path>')
print("\nGeneral Options:")
print(" -h \tShow help.")
print(" -p \tTo orgainise TV Shows in the specified directory\n")
sys.exit(2)
if __name__ == '__main__':
path = "NULL"
try:
opts, args = getopt.getopt(sys.argv[1:], 'p:h', ["path="])
except getopt.GetoptError:
usageHelp()
for opt, arg in opts:
if opt in ("-p", "--path"):
path = arg
elif opt == '-h':
usageHelp()
init(path)
print("Moving files..")
print("Enter any key to exit...")
print()
msvcrt.getch()