<s> import json <EOL> import threading <EOL> import urllib . error <EOL> import urllib . request <EOL> USER_AGENT = { } <EOL> def set_user_agent ( name , version ) : <EOL> USER_AGENT [ '<STR_LIT>' ] = '<STR_LIT>' . format ( name . capitalize ( ) , <EOL> '<STR_LIT:.>' . join ( map ( str , version ) ) ) <EOL> def _open_url ( url , file = None ) : <EOL> respond = urllib . request . urlopen ( urllib . request . Request ( url , headers = USER_AGENT ) ) <EOL> if file : <EOL> with open ( file , '<STR_LIT:wb>' ) as destination : <EOL> destination . write ( respond . read ( ) ) <EOL> return respond <EOL> class CoubletConnectionError ( Exception ) : pass <EOL> class CoubletDownloadPacketThread ( threading . Thread ) : <EOL> FILE_KEYS = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:user>' <EOL> def __init__ ( self , index , packet , queue ) : <EOL> super ( ) . __init__ ( ) <EOL> self . _index = index <EOL> self . _queue = queue <EOL> self . _packet = packet <EOL> def run ( self ) : <EOL> packet = self . _packet <EOL> for file_key in self . FILE_KEYS : <EOL> url , file = packet [ file_key ] <EOL> print ( '<STR_LIT>' . format ( url , file ) ) <EOL> if url and file : <EOL> try : <EOL> _open_url ( url , file ) <EOL> except urllib . error . URLError as e : <EOL> try : <EOL> packet [ '<STR_LIT:error>' ] = e . reason + '<STR_LIT>' <EOL> except TypeError : <EOL> packet [ '<STR_LIT:error>' ] = '<STR_LIT>' . format ( e . reason ) <EOL> print ( '<STR_LIT>' . format ( file ) ) <EOL> self . _queue . put ( ( self . _index , packet ) ) <EOL> class CoubletDownloadJsonThread ( threading . Thread ) : <EOL> def __init__ ( self , url , queue ) : <EOL> super ( ) . __init__ ( ) <EOL> self . _url = url <EOL> self . _queue = queue <EOL> def run ( self ) : <EOL> print ( '<STR_LIT>' ) <EOL> try : <EOL> self . _queue . put ( json . loads ( _open_url ( self . _url ) . read ( ) . decode ( '<STR_LIT:utf-8>' ) ) ) <EOL> except ( urllib . error . URLError , ConnectionResetError ) : <EOL> self . _queue . put ( CoubletConnectionError ) <EOL> print ( '<STR_LIT>' ) </s>
