-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovieFileRenamer.py
91 lines (77 loc) · 2.51 KB
/
MovieFileRenamer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import enzyme
import urllib2,urllib,json,os
def listFiles(dir):
return glob.glob(dir)
def getVideoMetadata(file):
return enzyme.parse(file).convert()
def searchMovieInTmdb(searchKey):
headers = {"Accept": "application/json"}
data={"api_key":"2d47f493560c897a2b6471bc6dc66bf7","query":searchKey,"language":"zh"}
request=urllib2.Request("http://api.themoviedb.org/3/search/movie?"+urllib.urlencode(data), headers=headers)
response=urllib2.urlopen(request).read().decode('utf-8')
jsonResult=json.loads(response)
return jsonResult
def getName(id,language):
headers = {"Accept": "application/json"}
data={"api_key":"2d47f493560c897a2b6471bc6dc66bf7","language":language}
request=urllib2.Request("http://api.themoviedb.org/3/movie/"+str(id)+"?"+urllib.urlencode(data), headers=headers)
response=urllib2.urlopen(request).read().decode('utf-8')
jsonResult=json.loads(response)
return jsonResult["title"]
def process(file):
selectedItem=None
while not selectedItem:
searchKey=raw_input("Please input the search key:")
result=searchMovieInTmdb(searchKey)
while result["total_results"]==0:
searchKey=raw_input("Please input the search key:")
result=searchMovieInTmdb(searchKey)
seq=1
for movie in result["results"]:
print(str(seq)+"\t"\
+movie["original_title"]+"\t"\
+movie["title"]+"\t"\
+movie["release_date"])
seq=seq+1
print(str(0)+"\t input again")
replaceId=input("Please input the choice:")
if replaceId==0:
continue
else:
selectedItem=result["results"][replaceId-1]
break
newname=generateFileName(file, selectedItem)
newname=os.path.join(os.path.dirname(file),newname)
print(newname)
prompt=raw_input("Change to ?(y/n)")
if prompt=="y":
os.rename(file, newname)
def generateFileName(file,tmdbInfo):
metadata=getVideoMetadata(file)
# print(metadata)
fileName, fileExtension = os.path.splitext(file)
newname=getName(tmdbInfo["id"], "en")\
+" "+getName(tmdbInfo["id"], "zh")\
+" "+tmdbInfo["release_date"][0:4]\
+" "+str(metadata["video"][0]["height"])+"p"\
+" "+metadata["video"][0]["codec"]\
+fileExtension
return newname.replace(" ", ".")
def promptConfirmation(file):
result=raw_input("R U sure to process this file:"+file+"?(y/n)")
if result=="y":
return True
else:
return False
if __name__ == '__main__':
path=raw_input("Please input the dir or the file path:")
if os.path.isdir(path):
files=listFiles(path+"/*")
for file in files:
if promptConfirmation(file):
process(file)
else:
process(path)