<s> import os as _os <EOL> import sys as _sys <EOL> import re as _re <EOL> import time as _time <EOL> import urllib2 as _urllib2 <EOL> import shutil as _shutil <EOL> class StaticFileHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . base_dir = _os . path . expanduser ( '<STR_LIT>' ) <EOL> if not _os . path . exists ( self . base_dir ) : <EOL> _os . makedirs ( self . base_dir ) <EOL> self . expiry_seconds = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:3> <EOL> def download ( self , url , output_folder ) : <EOL> """<STR_LIT>""" <EOL> filename = self . url_to_filename ( url ) <EOL> full_filename = _os . path . join ( self . base_dir , filename ) <EOL> folder = _os . path . join ( self . base_dir , filename . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> if not _os . path . exists ( folder ) : <EOL> _os . makedirs ( folder ) <EOL> file_modified_timestamp = <NUM_LIT:0> <EOL> if _os . path . exists ( full_filename ) : <EOL> file_modified_timestamp = _os . path . getmtime ( full_filename ) <EOL> if self . file_expired ( file_modified_timestamp ) : <EOL> try : <EOL> content = _urllib2 . urlopen ( '<STR_LIT>' . format ( url ) ) . read ( ) <EOL> with open ( full_filename , '<STR_LIT:wb>' ) as output_file : <EOL> output_file . write ( content ) <EOL> except _urllib2 . HTTPError as e : <EOL> return False <EOL> except _urllib2 . URLError as e : <EOL> return False <EOL> output_folder = _os . path . abspath ( output_folder ) <EOL> full_output_folder = _os . path . join ( output_folder , filename . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> full_output_filename = _os . path . join ( output_folder , filename ) <EOL> if not _os . path . exists ( full_output_folder ) : <EOL> _os . makedirs ( full_output_folder ) <EOL> _shutil . copyfile ( full_filename , full_output_filename ) <EOL> def file_expired ( self , modified_time ) : <EOL> """<STR_LIT>""" <EOL> expiry_time = _time . time ( ) - self . expiry_seconds <EOL> if modified_time < expiry_time : <EOL> return True <EOL> else : <EOL> return False <EOL> def url_to_filename ( self , url ) : <EOL> """<STR_LIT>""" <EOL> filename = url . lstrip ( '<STR_LIT>' ) . lstrip ( '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) <EOL> return filename <EOL> def extract_static_urls ( self , content ) : <EOL> """<STR_LIT>""" <EOL> return set ( _re . findall ( r"<STR_LIT>" , content ) ) </s>
