-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovierenamer.py
executable file
·152 lines (133 loc) · 4.49 KB
/
movierenamer.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
#!/usr/bin/python
import re
import sys
import urllib2
import json
import getopt
import os
SOURCES = 'brrip|r5|dvdrip|src|dvdscr|pvvrip|cam|telesync|ts|wp|workprint'
CODECS = 'xvid|x264|264|h264|divx'
RESOLUTION = '720|1080|PAL|NTSC'
debug=0
dryrun=0
imdb=0
finalsep='.'
formatting=['title','disk','year','source','codec']
def get_data(filename):
d = dict()
d['filename'] = filename
d['extension'] = filename.split('.')[-1]
#get rid of the extension
l = filename.split('.')[:-1]
#turn list back into a string with spaces instead of dots
filename = ''
for x in l:
filename += x+' '
filename = filename.strip()
#may as well fix '_'s while we're at it
filename = re.sub('_', ' ', filename)
#discover source of rip
result = re.search(SOURCES, filename, flags=re.IGNORECASE)
if result:
d['source'] = result.group()
#discover codec
result = re.search(CODECS, filename, flags=re.IGNORECASE)
if result:
d['codec'] = result.group()
#discover year
result = re.search('\d{4}', filename)
if result:
d['year'] = result.group()
#part of a series of disks?
result = re.search('cd(\d*)', filename, flags=re.IGNORECASE)
if result:
disk = result.group()
disknum = re.search('(\d+)', disk, flags=re.IGNORECASE)
d['disk'] = int(disknum.group())
#720p or 1080?
result = re.search(RESOLUTION, filename, flags=re.IGNORECASE)
if result:
d['res'] = result.group()
#now we need to dump everything that isn't going to be the title
result = re.search('\[|\(|\{|\d{4}|cd(\d*)|disk(\d*)|'+SOURCES+'|'+CODECS+'|'+RESOLUTION, filename, flags=re.IGNORECASE)
if result:
d['title'] = filename[:result.start()].strip(' -')
else:
d['title'] = filename.strip(' -')
return d
def get_imdb_info(title, year=''):
s='http://imdbapi.com/?t='+title+'&y='+str(year)
url = urllib2.urlopen(s.replace(' ','.'))
res = json.loads(url.read())
if res['Response'] == 'True':
return res
else:
return None
def usage(argv0):
print('usage: '+argv0+' [OPTION] FILE...')
print('''
--format= order of attributes, comma seperated
default, format=title,disk,year,source,codec
available attributes are title,source,codec,year,res
attributes not discovered will be skipped
--dry don't rename files, just show what would happen
--debug turn on debuging mode
-h
--help show help
--imdb also output imdb info if available
--spaces use spaces instead of dots for the final name
--underscores use underscores instead of dots for the final name
''')
sys.exit(1)
if __name__ == "__main__":
try:
args, files = getopt.gnu_getopt(sys.argv[1:], 'h',[
'dry','debug','help','imdb', 'format=', 'spaces', 'underscores'])
except:
print(e)
usage(sys.arg[0])
for opt, arg in args:
if opt in ['--help', '-h']:
usage(sys.argv[0])
elif opt in ['--dry']:
dryrun=1
elif opt in ['--debug']:
debug=1
elif opt in ['--imdb']:
imdb=1
elif opt in ['--format']:
formatting = arg.split(',')
elif opt in ['--spaces']:
finalsep=' '
elif opt in ['--underscores']:
finalsep='_'
else:
print('unknown option '+opt)
usage(sys.argv[0])
for f in files:
filename = f.split('/')[-1]
d=get_data(filename)
if not 'year' in d:
d['year'] = ''
if imdb:
imdbinfo = get_imdb_info(d['title'],d['year'])
if imdbinfo:
for x in imdbinfo.keys():
print x+': '+imdbinfo[x]
if 'Year' in imdbinfo:
d['year'] = imdbinfo['Year']
else:
print('imdb: nothing found')
newfilename = ''
for x in formatting:
if x in d:
newfilename += d[x]+'.'
newfilename = newfilename.strip(' .')
newfilename += '.'+d['extension']
newfilename = newfilename.replace(' ','.')
print('new filename: '+newfilename)
if not dryrun: #uhh this is bad, too late to find a better solution
if len(f.rsplit('/',1)) == 1:
os.rename(f, newfilename)
else:
os.rename(f, f.rsplit('/',1)[0]+'/'+newfilename)