forked from nicoddemus/ss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss.py
339 lines (267 loc) · 12.6 KB
/
ss.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from __future__ import with_statement
import xmlrpclib
import difflib
import os
import calculate_hash
import gzip
import urllib
import tempfile
import shutil
import time
import optparse
#===================================================================================================
# QueryOpenSubtitles
#===================================================================================================
def QueryOpenSubtitles(movie_filenames, language):
uri = 'http://api.opensubtitles.org/xml-rpc'
server = xmlrpclib.Server(uri, verbose=0, allow_none=True, use_datetime=True)
login_info = server.LogIn('', '', 'en', 'OS Test User Agent')
token = login_info['token']
try:
result = {}
for movie_filename in movie_filenames:
search_queries = [
dict(
moviehash=calculate_hash.CalculateHashForFile(movie_filename),
moviebytesize=os.path.getsize(movie_filename),
sublanguageid=language,
),
dict(
query=os.path.basename(os.path.splitext(movie_filename)[0]),
sublanguageid=language,
)
]
response = server.SearchSubtitles(token, search_queries)
search_results = response['data']
if search_results:
result[movie_filename] = search_results
return result
finally:
server.LogOut(token)
#===================================================================================================
# FindBestSubtitleMatches
#===================================================================================================
def FindBestSubtitleMatches(movie_filenames, language):
all_search_results = QueryOpenSubtitles(movie_filenames, language)
for movie_filename in movie_filenames:
search_results = all_search_results.get(movie_filename, [])
possibilities = [search_result['SubFileName'] for search_result in search_results]
basename = os.path.splitext(os.path.basename(movie_filename))[0]
closest_matches = difflib.get_close_matches(basename, possibilities)
if closest_matches:
filtered = [x for x in search_results if x['SubFileName'] in closest_matches]
filtered.sort(key=lambda x: (closest_matches.index(x['SubFileName']), -int(x['SubDownloadsCnt'])))
search_result = filtered[0]
yield movie_filename, search_result['SubDownloadLink'], '.' + search_result['SubFormat']
else:
yield movie_filename, None, None
#===================================================================================================
# ObtainSubtitleFilename
#===================================================================================================
def ObtainSubtitleFilename(movie_filename, language, subtitle_ext):
dirname = os.path.dirname(movie_filename)
basename = os.path.splitext(os.path.basename(movie_filename))[0]
# possibilities where we don't override
filenames = [
# -> movie.srt
os.path.join(dirname, basename + subtitle_ext),
# -> movie.eng.srt
os.path.join(dirname, '%s.%s%s' % (basename, language, subtitle_ext)),
]
for filename in filenames:
if not os.path.isfile(filename):
return filename
# use also ss on the extension and always overwrite
# -> movie.eng.ss.srt
return os.path.join(dirname, '%s.%s.%s%s' % (basename, language, 'ss', subtitle_ext))
#===================================================================================================
# DownloadSub
#===================================================================================================
def DownloadSub(subtitle_url, subtitle_filename):
# first download it and save to a temp dir
urlfile = urllib.urlopen(subtitle_url)
try:
gzip_subtitle_contents = urlfile.read()
finally:
urlfile.close()
tempdir = tempfile.mkdtemp()
try:
basename = subtitle_url.split('/')[-1]
tempfilename = os.path.join(tempdir, basename)
with file(tempfilename, 'wb') as f:
f.write(gzip_subtitle_contents)
f = gzip.GzipFile(tempfilename, 'r')
try:
subtitle_contents = f.read()
finally:
f.close()
# copy it over the new filename
with file(subtitle_filename, 'w') as f:
f.write(subtitle_contents)
finally:
shutil.rmtree(tempdir)
#===================================================================================================
# FindMovieFiles
#===================================================================================================
def FindMovieFiles(input_names, recursive=False):
extensions = set(['.avi', '.mp4', '.mpg', '.mkv'])
returned = set()
for input_name in input_names:
if os.path.isfile(input_name) and input_name not in returned:
yield input_name
returned.add(input_name)
else:
names = os.listdir(input_name)
for name in names:
result = os.path.join(input_name, name)
if name[-4:] in extensions:
if result not in returned:
yield result
returned.add(result)
elif os.path.isdir(result) and recursive:
for x in FindMovieFiles([result], recursive):
yield x
#===================================================================================================
# HasSubtitle
#===================================================================================================
def HasSubtitle(filename):
# list of subtitle formats obtained from opensubtitles' advanced search page.
formats = ['.sub', '.srt', '.ssa', '.smi', '.mpl']
basename = os.path.splitext(filename)[0]
for format in formats:
if os.path.isfile(basename + format):
return True
return False
#===================================================================================================
# ChangeConfiguration
#===================================================================================================
def ChangeConfiguration(params, filename):
config = LoadConfiguration(filename)
config.SetFromLines(params)
with file(filename, 'w') as f:
for line in config.GetLines():
f.write(line + '\n')
return config
#===================================================================================================
# LoadConfiguration
#===================================================================================================
def LoadConfiguration(filename):
if os.path.isfile(filename):
with file(filename) as f:
lines = f.readlines()
else:
lines = []
config = Configuration()
config.SetFromLines(lines)
return config
#===================================================================================================
# Configuration
#===================================================================================================
class Configuration(object):
def __init__(self, language='eng', recursive=False, skip=False):
self.language = language
self.recursive = recursive
self.skip = skip
def SetFromLines(self, strings):
def ParseBool(value):
return int(value.lower() in ('1', 'true', 'yes'))
for line in strings:
if '=' in line:
name, value = [x.strip() for x in line.split('=', 1)]
if name == 'language':
self.language = value
elif name == 'recursive':
self.recursive = ParseBool(value)
elif name == 'skip':
self.skip = ParseBool(value)
def GetLines(self):
return [
'language=%s' % self.language,
'recursive=%s' % self.recursive,
'skip=%s' % self.skip,
]
def __eq__(self, other):
return self.language == other.language and \
self.recursive == other.recursive and \
self.skip == other.skip
def __ne__(self, other):
return not self == other
#===================================================================================================
# Main
#===================================================================================================
def Main(argv):
parser = optparse.OptionParser(
usage='Usage: %prog [options] <file or dir> <file or dir>...',
description='Searches for subtitles using OpenSubtitles (http://www.opensubtitles.org).',
epilog='If a directory is given, search for subtitles for all movies on it (non-recursively).',
)
parser.add_option('-c', '--config', help='configuration mode.', action='store_true')
options, args = parser.parse_args(args=argv)
if not options.config and len(args) < 2:
parser.print_help()
return 2
config_filename = os.path.join(os.path.dirname(__file__), '.ss.ini')
if options.config:
config = ChangeConfiguration(args, config_filename)
for line in config.GetLines():
print line
return 0
else:
config = LoadConfiguration(config_filename)
input_filenames = list(FindMovieFiles(args[1:], recursive=config.recursive))
if not input_filenames:
sys.stdout.write('No files to search subtitles for. Aborting.\n')
return 1
skipped_filenames = []
if config.skip:
new_input_filenames = []
for input_filename in input_filenames:
if HasSubtitle(input_filename):
skipped_filenames.append(input_filename)
else:
new_input_filenames.append(input_filename)
input_filenames = new_input_filenames
def PrintStatus(text, status):
spaces = 70 - len(text)
if spaces < 2:
spaces = 2
sys.stdout.write('%s%s%s\n' % (text, ' ' * spaces, status))
sys.stdout.write('Language: %s\n' % config.language)
if config.skip and skipped_filenames:
print 'Skipping %d files that already have subtitles.' % len(skipped_filenames)
if not input_filenames:
return 1
sys.stdout.write('Querying OpenSubtitles.org for %d file(s)...\n' % len(input_filenames))
sys.stdout.write('\n')
matches = []
for (movie_filename, subtitle_url, subtitle_ext) in sorted(FindBestSubtitleMatches(input_filenames, language=config.language)):
if subtitle_url:
status = 'OK'
else:
status = 'No matches found.'
PrintStatus('- %s' % os.path.basename(movie_filename), status)
if subtitle_url:
subtitle_filename = ObtainSubtitleFilename(movie_filename, config.language, subtitle_ext)
matches.append((movie_filename, subtitle_url, subtitle_ext, subtitle_filename))
if not matches:
return 0
sys.stdout.write('\n')
sys.stdout.write('Downloading...\n')
for (movie_filename, subtitle_url, subtitle_ext, subtitle_filename) in matches:
DownloadSub(subtitle_url, subtitle_filename)
PrintStatus(' - %s' % os.path.basename(subtitle_filename), 'DONE')
#===================================================================================================
# Entry
#===================================================================================================
if __name__ == '__main__':
try:
import sys
Main(sys.argv)
except:
import traceback
with file(__file__ + '.log', 'a+') as log_file:
log_file.write('ERROR ' + ('=' * 80) + '\n')
log_file.write('Date: %s' % time.strftime('%c'))
log_file.write('args: ' + repr(sys.argv))
traceback.print_exc(file=log_file)
raise