<s> import logging <EOL> from babelfish import Language , language_converters <EOL> from requests import Session <EOL> from . import Provider , get_version <EOL> from . . import __version__ <EOL> from . . subtitle import Subtitle , fix_line_ending <EOL> logger = logging . getLogger ( __name__ ) <EOL> class TheSubDBSubtitle ( Subtitle ) : <EOL> provider_name = '<STR_LIT>' <EOL> def __init__ ( self , language , hash ) : <EOL> super ( TheSubDBSubtitle , self ) . __init__ ( language ) <EOL> self . hash = hash <EOL> @ property <EOL> def id ( self ) : <EOL> return self . hash + '<STR_LIT:->' + str ( self . language ) <EOL> def get_matches ( self , video , hearing_impaired = False ) : <EOL> matches = super ( TheSubDBSubtitle , self ) . get_matches ( video , hearing_impaired = hearing_impaired ) <EOL> if '<STR_LIT>' in video . hashes and video . hashes [ '<STR_LIT>' ] == self . hash : <EOL> matches . add ( '<STR_LIT>' ) <EOL> return matches <EOL> class TheSubDBProvider ( Provider ) : <EOL> languages = { Language . fromthesubdb ( l ) for l in language_converters [ '<STR_LIT>' ] . codes } <EOL> required_hash = '<STR_LIT>' <EOL> server_url = '<STR_LIT>' <EOL> def initialize ( self ) : <EOL> self . session = Session ( ) <EOL> self . session . headers = { '<STR_LIT>' : '<STR_LIT>' % <EOL> get_version ( __version__ ) } <EOL> def terminate ( self ) : <EOL> self . session . close ( ) <EOL> def query ( self , hash ) : <EOL> params = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT>' : hash } <EOL> logger . info ( '<STR_LIT>' , params ) <EOL> r = self . session . get ( self . server_url , params = params , timeout = <NUM_LIT:10> ) <EOL> if r . status_code == <NUM_LIT> : <EOL> logger . debug ( '<STR_LIT>' ) <EOL> return [ ] <EOL> r . raise_for_status ( ) <EOL> subtitles = [ ] <EOL> for language_code in r . text . split ( '<STR_LIT:U+002C>' ) : <EOL> language = Language . fromthesubdb ( language_code ) <EOL> subtitle = TheSubDBSubtitle ( language , hash ) <EOL> logger . debug ( '<STR_LIT>' , subtitle ) <EOL> subtitles . append ( subtitle ) <EOL> return subtitles <EOL> def list_subtitles ( self , video , languages ) : <EOL> return [ s for s in self . query ( video . hashes [ '<STR_LIT>' ] ) if s . language in languages ] <EOL> def download_subtitle ( self , subtitle ) : <EOL> logger . info ( '<STR_LIT>' , subtitle ) <EOL> params = { '<STR_LIT:action>' : '<STR_LIT>' , '<STR_LIT>' : subtitle . hash , '<STR_LIT>' : subtitle . language . alpha2 } <EOL> r = self . session . get ( self . server_url , params = params , timeout = <NUM_LIT:10> ) <EOL> r . raise_for_status ( ) <EOL> subtitle . content = fix_line_ending ( r . content ) </s>
