<s> import requests <EOL> import os <EOL> import shutil <EOL> import multiprocessing <EOL> import random <EOL> from config_data . site_config import SiteConfig <EOL> def download_file_async ( file_name , dest_folder ) : <EOL> print ( "<STR_LIT>" + file_name + "<STR_LIT>" + dest_folder ) <EOL> mgr = CacheFileManager ( file_name ) <EOL> mgr . perform_download ( ) <EOL> print ( "<STR_LIT>" ) <EOL> class CacheFileManager : <EOL> def __init__ ( self , file ) : <EOL> config = SiteConfig ( ) <EOL> self . cache_folder = os . path . abspath ( config . cache_folder ) <EOL> self . file_name = file <EOL> self . full_file_path = os . path . join ( self . cache_folder , self . file_name . lower ( ) ) <EOL> self . download_base_url = config . download_base_url <EOL> self . source_url = self . download_base_url . strip ( '<STR_LIT:/>' ) + "<STR_LIT:/>" + self . file_name <EOL> self . local_download_url = "<STR_LIT>" + self . file_name . lower ( ) <EOL> if self . file_name : <EOL> self . file_name = self . file_name . strip ( ) <EOL> @ property <EOL> def is_available ( self ) : <EOL> if not self . file_name : <EOL> return False <EOL> file_name = self . file_name . lower ( ) . strip ( ) <EOL> local_file = os . path . join ( self . cache_folder , file_name ) <EOL> available = os . path . exists ( local_file ) <EOL> print ( "<STR_LIT>" + local_file ) <EOL> return available <EOL> def cache_async ( self ) : <EOL> p = multiprocessing . Process ( <EOL> target = download_file_async , <EOL> args = ( self . file_name , self . cache_folder ) ) <EOL> p . daemon = True <EOL> p . start ( ) <EOL> def perform_download ( self ) : <EOL> if self . is_available : <EOL> return <EOL> print ( "<STR_LIT>" + self . source_url ) <EOL> r = requests . get ( self . source_url , timeout = <NUM_LIT:5> , stream = True ) <EOL> if r . status_code != <NUM_LIT:200> : <EOL> print ( "<STR_LIT>" ) <EOL> return <EOL> tmp_file = '<STR_LIT>' + '<STR_LIT>' . join ( random . choice ( '<STR_LIT>' ) for i in range ( <NUM_LIT:16> ) ) + "<STR_LIT:_>" + self . file_name <EOL> tmp_out = os . path . join ( self . cache_folder , tmp_file ) <EOL> try : <EOL> print ( "<STR_LIT>" ) <EOL> with open ( tmp_out , "<STR_LIT>" ) as fout : <EOL> shutil . copyfileobj ( r . raw , fout ) <EOL> print ( "<STR_LIT>" ) <EOL> if self . is_available : <EOL> print ( "<STR_LIT>" + repr ( tmp_file ) ) <EOL> if os . path . exists ( tmp_out ) : <EOL> os . remove ( tmp_out ) <EOL> return <EOL> print ( "<STR_LIT>" ) <EOL> os . rename ( tmp_out , self . full_file_path ) <EOL> print ( "<STR_LIT>" + self . file_name . lower ( ) ) <EOL> except Exception as x : <EOL> print ( "<STR_LIT>" + repr ( x ) ) <EOL> if os . path . exists ( tmp_out ) : <EOL> os . remove ( tmp_out ) </s>
