Skip to content

Commit fa8f575

Browse files
committed
Now it will match the most downloaded subtitle
1 parent b250bf3 commit fa8f575

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

ss.py

+31-41
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,35 @@
1919
#===================================================================================================
2020
# QueryOpenSubtitles
2121
#===================================================================================================
22-
def QueryOpenSubtitles(hash_to_movie_filename, language):
22+
def QueryOpenSubtitles(movie_filenames, language):
2323
uri = 'http://api.opensubtitles.org/xml-rpc'
2424
server = xmlrpclib.Server(uri, verbose=0, allow_none=True, use_datetime=True)
2525

2626
login_info = server.LogIn('', '', 'en', 'OS Test User Agent')
2727
token = login_info['token']
2828

2929
try:
30-
search_queries = []
31-
for moviehash, movie_filename in hash_to_movie_filename.iteritems():
32-
moviebytesize = os.path.getsize(movie_filename)
33-
search_query = dict(
34-
moviehash=moviehash,
35-
moviebytesize=moviebytesize,
36-
sublanguageid=language,
37-
)
38-
search_queries.append(search_query)
30+
result = {}
31+
32+
for movie_filename in movie_filenames:
33+
search_queries = [
34+
dict(
35+
moviehash=CalculateHashForFile(movie_filename),
36+
moviebytesize=os.path.getsize(movie_filename),
37+
sublanguageid=language,
38+
),
39+
dict(
40+
query=os.path.basename(os.path.splitext(movie_filename)[0]),
41+
)
42+
]
3943

40-
response = server.SearchSubtitles(token, search_queries)
41-
search_results = response['data']
42-
if search_results:
43-
return search_results
44-
else:
45-
return []
44+
response = server.SearchSubtitles(token, search_queries)
45+
search_results = response['data']
46+
47+
if search_results:
48+
result[movie_filename] = search_results
49+
50+
return result
4651
finally:
4752
server.LogOut(token)
4853

@@ -52,34 +57,21 @@ def QueryOpenSubtitles(hash_to_movie_filename, language):
5257
#===================================================================================================
5358
def FindBestSubtitleMatches(movie_filenames, language):
5459

55-
hash_to_movie_filename = dict((CalculateHashForFile(x), x) for x in movie_filenames)
56-
57-
search_results = QueryOpenSubtitles(hash_to_movie_filename, language)
58-
59-
hash_to_search_results = {}
60-
for search_data in search_results:
61-
hash_to_search_results.setdefault(search_data['MovieHash'], []).append(search_data)
60+
all_search_results = QueryOpenSubtitles(movie_filenames, language)
6261

63-
for hash, movie_filename in hash_to_movie_filename.iteritems():
62+
for movie_filename in movie_filenames:
6463

65-
if hash not in hash_to_search_results:
66-
yield movie_filename, None, None
67-
continue
68-
69-
search_results = hash_to_search_results[hash]
64+
search_results = all_search_results.get(movie_filename, [])
7065

71-
possibilities = []
72-
for search_result in search_results:
73-
possibilities.append(search_result['SubFileName']) # this does not include the file extension
74-
75-
closest_matches = difflib.get_close_matches(os.path.basename(movie_filename), possibilities)
66+
possibilities = [search_result['SubFileName'] for search_result in search_results]
67+
basename = os.path.splitext(os.path.basename(movie_filename))[0]
68+
closest_matches = difflib.get_close_matches(basename, possibilities)
7669

7770
if closest_matches:
78-
closest_match = closest_matches[0]
79-
for search_result in search_results:
80-
if search_result['SubFileName'] == closest_match:
81-
yield movie_filename, search_result['SubDownloadLink'], '.' + search_result['SubFormat']
82-
break
71+
filtered = [x for x in search_results if x['SubFileName'] in closest_matches]
72+
filtered.sort(key=lambda x: x['SubDownloadsCnt'])
73+
search_result = filtered[0]
74+
yield movie_filename, search_result['SubDownloadLink'], '.' + search_result['SubFormat']
8375
else:
8476
yield movie_filename, None, None
8577

@@ -186,8 +178,6 @@ def PrintStatus(text, status):
186178
if spaces < 2:
187179
spaces = 2
188180
sys.stdout.write('%s%s%s\n' % (text, ' ' * spaces, status))
189-
190-
191181

192182
if not input_filenames:
193183
sys.stdout.write('No files to search subtitles for. Aborting.\n')

0 commit comments

Comments
 (0)