19
19
#===================================================================================================
20
20
# QueryOpenSubtitles
21
21
#===================================================================================================
22
- def QueryOpenSubtitles (hash_to_movie_filename , language ):
22
+ def QueryOpenSubtitles (movie_filenames , language ):
23
23
uri = 'http://api.opensubtitles.org/xml-rpc'
24
24
server = xmlrpclib .Server (uri , verbose = 0 , allow_none = True , use_datetime = True )
25
25
26
26
login_info = server .LogIn ('' , '' , 'en' , 'OS Test User Agent' )
27
27
token = login_info ['token' ]
28
28
29
29
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
+ ]
39
43
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
46
51
finally :
47
52
server .LogOut (token )
48
53
@@ -52,34 +57,21 @@ def QueryOpenSubtitles(hash_to_movie_filename, language):
52
57
#===================================================================================================
53
58
def FindBestSubtitleMatches (movie_filenames , language ):
54
59
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 )
62
61
63
- for hash , movie_filename in hash_to_movie_filename . iteritems () :
62
+ for movie_filename in movie_filenames :
64
63
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 , [])
70
65
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 )
76
69
77
70
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' ]
83
75
else :
84
76
yield movie_filename , None , None
85
77
@@ -186,8 +178,6 @@ def PrintStatus(text, status):
186
178
if spaces < 2 :
187
179
spaces = 2
188
180
sys .stdout .write ('%s%s%s\n ' % (text , ' ' * spaces , status ))
189
-
190
-
191
181
192
182
if not input_filenames :
193
183
sys .stdout .write ('No files to search subtitles for. Aborting.\n ' )
0 commit comments