@@ -302,26 +302,26 @@ def query(self, languages, video):
302302 self .video = video
303303 if self .use_hash :
304304 file_hash = self .video .hashes .get ('opensubtitlescom' )
305- logger .debug (f'Searching using this hash: { file_hash } ' )
306305 else :
307306 file_hash = None
307+ logger .debug (f'Searching using this hash: { file_hash } ' )
308308
309- if isinstance (self .video , Episode ):
310- title = self .video .series
311- else :
312- title = self .video .title
313-
314- imdb_id = None
315- if isinstance (self .video , Episode ) and self .video .imdb_id :
316- imdb_id = self .sanitize_external_ids (self .video .imdb_id )
317- elif isinstance (self .video , Movie ) and self .video .imdb_id :
318- imdb_id = self .sanitize_external_ids (self .video .imdb_id )
309+ imdb_id = self .sanitize_external_ids (self .video .imdb_id ) if self .video .imdb_id else None
310+ logger .debug (f'Searching using this IMDB ID: { imdb_id } ' )
319311
320312 title_id = None
321- if not imdb_id :
313+ if ((isinstance (self .video , Episode ) and not self .video .series_imdb_id ) or
314+ (isinstance (self .video , Movie ) and not imdb_id )):
315+ if isinstance (self .video , Episode ):
316+ title = self .video .series
317+ else :
318+ title = self .video .title
319+ logger .debug (f'Searching for this title: { title } ' )
320+
322321 title_id = self .search_titles (title )
323- if not title_id :
324- return []
322+ logger .debug (f'Found this title ID: { title_id } ' )
323+ else :
324+ logger .debug (f"No need to search for a title ID. We'll use the IMDB ID instead." )
325325
326326 # be sure to remove duplicates using list(set())
327327 langs_list = sorted (list (set ([to_opensubtitlescom (lang .basename ).lower () for lang in languages ])))
@@ -331,31 +331,46 @@ def query(self, languages, video):
331331
332332 # define the proper query parameters based on the video type
333333 # query parameters must be alphabetically ordered to prevent redirect
334+ params : list [tuple [str , str | int ]] = [('languages' , langs )]
335+ if file_hash :
336+ params .append (('moviehash' , file_hash ))
337+ if imdb_id :
338+ params .append (('imdb_id' , imdb_id ))
339+
334340 if isinstance (self .video , Episode ):
335- params = [('episode_number' , self .video .episode ),
336- ('imdb_id' , imdb_id ),
337- ('languages' , langs ),
338- ('moviehash' , file_hash ),
339- ('parent_feature_id' , title_id ),
340- ('parent_imdb_id' , self .sanitize_external_ids (self .video .series_imdb_id ) if
341- self .video .series_imdb_id else None ),
342- ('season_number' , self .video .season )]
341+ if not imdb_id and not title_id and not self .video .series_imdb_id :
342+ logger .debug ("We don't have any ID to search for, returning empty list." )
343+ return []
344+
345+ if self .video .episode :
346+ params .append (('episode_number' , self .video .episode ))
347+ if self .video .season :
348+ params .append (('season_number' , self .video .season ))
349+ if self .video .series_imdb_id :
350+ params .append (('parent_imdb_id' , self .sanitize_external_ids (self .video .series_imdb_id )))
351+ if title_id :
352+ params .append (('parent_feature_id' , title_id ))
343353 else :
344- params = [('id' , title_id ),
345- ('imdb_id' , imdb_id ),
346- ('languages' , langs ),
347- ('moviehash' , file_hash )]
354+ if not imdb_id and not title_id :
355+ logger .debug ("We don't have any ID to search for, returning empty list." )
356+ return []
357+
358+ if title_id :
359+ params .append (('id' , title_id ))
348360
349361 # append the 'exclude' parameter to the list of query parameters if we don't want AI translated subtitles'
350362 if not self .include_ai_translated :
363+ logger .debug ("Excluding AI translated subtitles from search results" )
351364 params .append (('ai_translated' , 'exclude' ))
352365
353366 # append the 'exclude' parameter to the list of query parameters if we want machine translated subtitles'
354367 if self .include_machine_translated :
368+ logger .debug ("Including machine translated subtitles in search results" )
355369 params .append (('machine_translated' , 'include' ))
356370
357371 # sort params alphabetically to prevent redirect
358372 params = sorted (params , key = lambda param : param [0 ])
373+ logger .info (f'Query parameters used to query OpenSubtitles.com: { params } ' )
359374
360375 # query the server
361376 res = self .retry (
@@ -592,25 +607,28 @@ def log_request_response(response, non_standard=True):
592607 if 'Authorization' in redacted_request_headers and isinstance (redacted_request_headers ['Authorization' ], str ):
593608 redacted_request_headers ['Authorization' ] = redacted_request_headers ['Authorization' ][:- 8 ]+ 8 * 'x'
594609
610+ redacted_request_body = None
595611 if response .request .body :
596- redacted_request_body = json .loads (response .request .body )
597- if 'password' in redacted_request_body :
598- redacted_request_body ['password' ] = 'redacted'
599- else :
600- redacted_request_body = None
612+ try :
613+ redacted_request_body = json .loads (response .request .body )
614+ except json .JSONDecodeError :
615+ logger .debug (f"Response body could not be parsed as JSON: { response .request .body !r} ." )
616+ else :
617+ if 'password' in redacted_request_body :
618+ redacted_request_body ['password' ] = 'redacted'
601619
602620 redacted_response_body = json .loads (response .text )
603621 if 'token' in redacted_response_body and isinstance (redacted_response_body ['token' ], str ):
604622 redacted_response_body ['token' ] = redacted_response_body ['token' ][:- 8 ] + 8 * 'x'
605623
606624 if non_standard :
607625 logger .debug ("opensubtitlescom returned a non standard response. Logging request/response for debugging "
608- "purpose." )
626+ "purpose." )
609627 else :
610628 logger .debug ("opensubtitlescom returned a standard response. Logging request/response for debugging purpose." )
611629 logger .debug (f"Request URL: { response .request .url } " )
612630 logger .debug (f"Request Headers: { redacted_request_headers } " )
613- logger .debug (f"Request Body: { json .dumps (redacted_request_body )} " )
631+ logger .debug (f"Request Body: { json .dumps (redacted_request_body ) if redacted_request_body else None } " )
614632 logger .debug (f"Response Status Code: { response .status_code } " )
615633 logger .debug (f"Response Headers: { response .headers } " )
616- logger .debug (f"Response Body: { json .dumps (redacted_response_body )} " )
634+ logger .debug (f"Response Body: { json .dumps (redacted_response_body ) if redacted_request_body else None } " )
0 commit comments