-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRENAME-Movies.py
199 lines (189 loc) · 5.79 KB
/
RENAME-Movies.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
"""
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, shutil
from imdb import IMDb
from similarity.damerau import Damerau
# Returns most closest Movie name
def find_most_apt(name, movies):
damerau = Damerau()
deg = []
for movie in movies:
if(name.upper() == movie.upper()):
return(movie)
else:
deg.append(damerau.distance(name.upper(), movie.upper()))
indd = int(deg.index(min(deg)))
mostapt = movies[indd]
if (mostapt == ""):
mostapt = name
return(mostapt)
# Find any URLs present in FileName
def Find(string):
url = re.findall('www.(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\)]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', string)
return (url[0])
def lastIndexOf(str1,toFind):
index = len(str1)-1
i = 0
for ch in str1:
if(ch == toFind):
index = i
i+=1
return(index)
# Returns a List Movie name and release year using imDB from Old_FileName
def main_imdb(str21):
ia = IMDb()
s_result = ia.search_movie(str21)
movies = []
for movie in s_result:
if(movie['kind'] == 'movie'):
str2 = movie['title']
try:
year_str= movie['year']
except:
year_str= "----"
movies.append(str2+" ("+str(year_str)+")")
return(movies)
# Remove Illegal Name characters
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('*',"")
return(str)
# Print Program Title
def Title():
os.system("cls")
os.system('mode con: cols=75 lines=30')
print(" _ _ _ _ ")
print(" /_\ _ _| |_ ___ _ __ __ _| |_ ___ __| | ")
print(" / _ \ || | _/ _ \ ' \/ _` | _/ -_) _` | ")
print(" /_/ \_\_,_|\__\___/_|_|_\__,_|\__\___\__,_| ")
print(" ")
print(" _ _ _ ")
print(" | | (_) |__ _ _ __ _ _ _ _ _ ")
print(" | |__| | '_ \ '_/ _` | '_| || | ")
print(" |____|_|_.__/_| \__,_|_| \_, | ")
print(" |__/ ")
print(" ___ _ ")
print(" / _ \ _ _ __ _ __ _ _ _ (_)___ ___ _ _ ")
print(" | (_) | '_/ _` / _` | ' \| (_-</ -_) '_|")
print(" \___/|_| \__, \__,_|_||_|_/__/\___|_| ")
print(" |___/ ")
print("")
# Primitive FileName Formatting
def FormatStr(temp):
rest = temp
if ".1080p" in temp:
sep = ".1080p"
elif ".720p" in temp:
sep = ".720p"
elif "[" in temp:
sep = "["
elif "1080p" in temp:
sep = "1080p"
elif "720p" in temp:
sep = "720p"
if "TamilRockers" in temp:
temp = temp.split(' - ',1)[1]
try:
rest = temp.split(sep,1)[0]
except:
pass
rest = rest.replace("."," ")
rest = rest.replace("-"," ")
rest = rest.replace("(","")
rest = rest.replace(")","")
return(rest.strip())
# Driver Code
def main():
Title()
try:
os.mkdir("Input")
except FileExistsError:
pass
try:
os.mkdir("Input\\Movies")
except FileExistsError:
pass
try:
os.mkdir("Output")
except FileExistsError:
pass
path = "Input\\Movies"
i=0
ErrorFlag=0
FileFlag=0
files = os.listdir(path)
for file in files:
temp = file
extn = file[(len(file)-4) : len(file)]
if(file.endswith(".mp4") or file.endswith(".mkv") or file.endswith(".srt")) :
Title()
print(str(i)+" File(s) Processed....")
try:
url = Find(temp)
# print(url)
temp = temp.replace(url,"")
except:
pass
rest = FormatStr(temp.strip())
year_str = '('+ rest[len(rest)-4 : len(rest)] +')'
rest = rest[0:len(rest)-4]
Final = rest + year_str
print("Derived: ",Final)
movies = main_imdb(rest + year_str)
rest = find_most_apt(Final, movies) # Sometimes causes error
rest = removeIllegal(Final)
Final = rest + extn
print("Most Apt: ",Final)
print()
# Rename from old -> new happens below
path_new = os.getcwd() + "\\Output\\Movies\\" + rest
try:
os.mkdir("Output")
except FileExistsError:
pass
try:
os.mkdir("Output\\Movies")
except FileExistsError:
pass
try:
os.mkdir(path_new)
except FileExistsError:
pass
try:
os.rename(os.path.join(path, file), os.path.join(path_new, Final ))
except:
print("Error - File Already Exist: "+rest )
FileFlag=1
ErrorFlag=1
i=i-1;
# print(file)
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")
# if(i>0): os.system("explorer.exe " + str(os.getcwd() + "\Output\\"))
else:
print("No Media File Found in Input Folder")
if __name__ == '__main__':
main()
print("Moving files..")
print("Enter any key to exit...")
print()
msvcrt.getch()