forked from navinsylvester/cinephile
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcinephile.py
282 lines (212 loc) · 8.98 KB
/
cinephile.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
#!/usr/bin/env python
from __future__ import absolute_import
import re
import httplib
import urllib2, urllib
import os
import sys
import json
import yaml
import argparse
import hashlib
def get_config():
config_file = None
config_paths = [
'cinephile.yaml',
os.path.expanduser('~/.cinephile.yaml'),
os.path.join(os.path.dirname(__file__), 'cinephile.yaml')
]
for path in config_paths:
if os.path.exists(path):
config_file = path
break
if not config_file:
print 'No cinephile.yaml found.'
return
#Read yaml file to get config
with open(config_file) as stream:
return yaml.load(stream)
def get_hash(name):
""" This hash function receives the name of the file
and returns the hash code
"""
readsize = 64 * 1024
with open(name, 'rb') as f:
size = os.path.getsize(name)
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()
def fetch_subtitles(filename, movie_name, quiet, language='en'):
""" Fetch subtitles from SubDB. Saves it in the same directory
as the movie file.
"""
file_hash = get_hash(filename)
base_url = 'http://api.thesubdb.com/?{0}'
user_agent = 'SubDB/1.0 (Cinephile/1.0; https://github.com/navinsylvester/cinephile)'
params = {
'action': 'download',
'language': language or 'en',
'hash': file_hash
}
url = base_url.format(urllib.urlencode(params))
req = urllib2.Request(url)
req.add_header('User-Agent', user_agent)
try:
if not quiet and movie_name: print 'Trying to fetch subtitle for "' + movie_name + '"...'
response = urllib2.urlopen(req)
ext = response.info()['Content-Disposition'].split(".")[1]
sub_file = os.path.splitext(filename)[0] + "." + ext
with open(sub_file, "wb") as fout:
fout.write(response.read())
print 'Subtitle saved to ' + sub_file
return True
except urllib2.URLError as e:
print 'Error fetching subtitles (Code: %r, Message: %s).' % (e.errno, e.reason)
except Exception:
print 'Unknown Error'
return False
def get_movie_info(movie_filename, order_list, rating, votes, full_path, genre=None):
api_uri = "www.omdbapi.com"
params = urllib.urlencode({'t': movie_filename})
connection = httplib.HTTPConnection(api_uri)
connection.request("GET", "/?" + params)
response = connection.getresponse()
response_json = response.read()
indent = 12
parsed_json = json.loads(response_json)
if parsed_json['Response'] == 'False':
return
if "imdbRating" in parsed_json and parsed_json['imdbRating'] != "N/A":
if genre is None or genre.capitalize() in parsed_json['Genre']:
if float(parsed_json['imdbRating']) >= rating and parsed_json['imdbVotes'] > votes:
for order in order_list:
spaces = ''
for i in range(indent - len(order)):
spaces += ' '
print u''.join((order, spaces, ': ', parsed_json[order])).encode('utf-8')
print "File path :", full_path[0]
print '\n'
connection.close()
def normalize_filename(movie_filename, purge_words_list, remove_year, convert_roman):
purge_words_list = "(?i)({0})(.*)$".format(purge_words_list)
purge_digit = "(\d{4})(.*)$"
purge_spl_chars = "(\[|\()(.*)$"
purge_dot_underscore = "(\.|_)"
purge_hypen_aps = "(\-|')"
re_str = re.sub(purge_spl_chars, "", movie_filename, )
re_str = re.sub(purge_words_list, "", re_str, re.I)
if remove_year is True:
re_str = re.sub(purge_digit, "", re_str, )
re_str = re.sub(purge_dot_underscore, " ", re_str, )
re_str = re.sub(purge_hypen_aps, "", re_str, )
if convert_roman is True:
re_comp = re.compile(r'\b(?=[MDCLXVI]+\b)M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})\b')
re_str = re_comp.sub(roman_to_int_repl, re_str)
return re_str.rstrip().lstrip()
def roman_to_int_repl(match):
return str(roman_to_int(match.group(0)))
def roman_to_int(n):
numeral_map = zip(
(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1),
('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
)
n = unicode(n).upper()
i = result = 0
for integer, numeral in numeral_map:
while n[i:i + len(numeral)] == numeral:
result += integer
i += len(numeral)
return result
def convert_roman(roman_str):
string = roman_str.upper()
total = 0
while string:
if len(string) == 1 or val[string[0]] >= val[string[1]]:
total += val[string[0]]
string = string[1:]
else:
total += val[string[1]] - val[string[0]]
string = string[2:]
print total
def get_movies_set(movie_dir, skip_dupes):
if not os.path.exists(movie_dir):
print "Scan directory doesn't exist"
return
config = get_config()
file_ext_list = config['file_ext'].replace(",", "|")
purge_words_list = config['purge_words'].replace(",", "|")
movies = []
# Skip duplicate movie names
dupes = set()
#Scan movie dir recursively
for root, dirnames, filenames in os.walk(movie_dir, ):
#Remove the directories mentioned in the ignore list
if len(config['ignore_dir']):
for ignore in config['ignore_dir']:
if ignore in dirnames:
dirnames.remove(ignore)
for filename in filenames:
# skip 'em pesky dot files
if filename[0] == '.': continue
full_path = []
if len(file_ext_list) > 1:
file_ext_expr = "(?P<name>.*)\.({0})".format(file_ext_list)
movie_filename = re.match(file_ext_expr, filename, re.I)
if movie_filename:
#Normalize filename
movie_filename = normalize_filename(movie_filename.group("name"), purge_words_list, config['remove_year'], config['convert_roman'])
full_path.append(os.path.join(root, filename))
#Get movie details
if movie_filename not in dupes:
movies.append({'movie_filename': movie_filename, 'full_path': full_path})
dupes.add(movie_filename)
else:
print "Add file extensions to scan in cinephile.yaml"
return
return movies
def run_movie_commands(args):
movie_dir = args.source_dir
rating = args.rating
genre = args.genre
movies = get_movies_set(movie_dir, True)
config = get_config()
for movie in movies:
get_movie_info(movie['movie_filename'], config['order_list'], rating, config['votes'], movie['full_path'], genre)
def run_subtitle_commands(args):
if args.recursive_scan:
movies = get_movies_set(args.filename, False)
total_movies = len(movies)
success_count = 0
print 'A total of %d movies found.' % (total_movies)
for movie in movies:
ret = fetch_subtitles(movie['full_path'][0], movie['movie_filename'], args.stfu, args.language)
if ret: success_count += 1
print '=' * 80
print 'Total movies: %d Got subtitles for: %d Failed for: %d' % (total_movies, success_count, total_movies - success_count)
print '=' * 80
else:
fetch_subtitles(args.filename, None, True, args.language)
def run():
parser = argparse.ArgumentParser(description='Fetch movie information from imdb')
sub_parsers = parser.add_subparsers(help='Cinephile commands')
movie_parser = sub_parsers.add_parser('movie', help='Movie commands')
movie_parser.add_argument('-s', '--source_dir', help='Source directory of movies', required=True)
movie_parser.add_argument('-r', '--rating', help='Fetch movies with imdb rating greater than or equal to provided value', type=float, required=True)
movie_parser.add_argument('-g', '--genre', help='Filter by movie genre')
movie_parser.set_defaults(func=run_movie_commands)
sub_parser = sub_parsers.add_parser('subtitle', help='Subtitles commands')
sub_parser.add_argument('-f', '--fetch', dest='filename', help='Name of file or directory to fetch subtitles for', required=True)
sub_parser.add_argument('-l', '--lang', dest='language', help='Choose Language (en,es,fr,it,nl,pl,pt,ro,sv,tr)')
sub_parser.add_argument('-R', '--recursive', dest='recursive_scan', help='Scan source directory recursively for movies to fetch subtitles for', action='store_true')
sub_parser.add_argument('-q', '--quiet', dest='stfu', help='Don\'t print what the program is up to while fetching subtitles', action='store_true')
sub_parser.set_defaults(func=run_subtitle_commands)
args = parser.parse_args()
args.func(args)
def main():
try:
run()
except KeyboardInterrupt:
print 'Exiting ...'
if __name__ == '__main__':
main()