-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_movie.py
executable file
·106 lines (88 loc) · 2.39 KB
/
rename_movie.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
#!/usr/bin/env python
# import requests
import requests,json,sys
from sys import stdout, exit
from os import listdir, rename, _exit
from os.path import isfile, join
reload(sys)
sys.setdefaultencoding('utf8')
def query_yes_no(question,default='yes'):
valid={'yes':True,'ye':True,'y':True,
'no':False,'n':False,}
if default is None:
prompt=" [y/n] "
elif default == 'yes':
prompt=" [Y/n] "
elif default == 'no':
prompt=" [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
stdout.write(question+prompt)
choice=raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n'.\n")
def getData( title ):
url="http://www.omdbapi.com/?t="+title.replace(' ','+')+"&r=json"
r=requests.get(url)
parsed_data=json.loads(r.text)
return parsed_data
def main():
mypath="./"
badwords=['BRRip','BrRip','DVDRip','Dvdrip','720p','1080p','1080','BluRay','iPod','Unrated','UNRATED','Webrip','DVD']
files = [f for f in listdir(mypath) if isfile(join(mypath,f))]
files.sort();
for f in files:
print ('-----------------------------------------------------------')
file=f
file=file.replace('.',' ')
file=file.replace('_',' ')
file=file.replace('(',' ')
file=file.replace(')',' ')
file=file.replace('-',' ')
file=file.replace('[',' ')
file=file.replace(']',' ')
file=file.replace(':',' ')
file=file[:-4]
for w in badwords:
if w in file:
file=file[:file.index(w)]
# print file
data=getData(file)
if data['Response'] == "False":
dupfile=file
while data['Response'] == "False":
try:
dupfile=dupfile[:dupfile.rindex(' ')]
# print "Trying: "+dupfile
data=getData(dupfile)
except ValueError:
break
if data['Response'] == "True":
fname=data['Title']+" ("+data['Year']+")"+f[f.rindex('.'):]
fname=fname.replace('/','-')
if fname != f:
print (f+" --> "+fname)
if query_yes_no('Rename?','yes'):
rename(f,fname)
print ("Renamed "+f+" to "+fname)
else:
print ("Did not rename "+f)
else:
print (f+" was already named correctly")
else:
print ("Couldn't get data for "+f)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print ('Interrupted')
try:
exit(0)
except SystemExit:
_exit(0)