#NEWLINE# def download( session , uri , timeout = 1.0 , chunk_size = 4096 ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# response = session . get ( response , stream = True , timeout = timeout ) #NEWLINE# #UNINDENT# except requests . exceptions . Timeout : #NEWLINE# #INDENT# logger . warning ( 'Download of %r failed due to connection timeout after %.3fs' , uri , timeout ) #NEWLINE# return None #NEWLINE# #UNINDENT# except requests . exceptions . InvalidSchema : #NEWLINE# #INDENT# logger . warning ( 'Download of %r failed due to unsupported schema' , uri ) #NEWLINE# return None #NEWLINE# #UNINDENT# except requests . exceptions . RequestException as exc : #NEWLINE# #INDENT# logger . warning ( 'Download of %r failed: %s' , uri , exc ) #NEWLINE# logger . debug ( 'Download exception details' , exc_info = True ) #NEWLINE# return None #NEWLINE# #UNINDENT# content = [ ] #NEWLINE# deadline = ( time . time ( ) + timeout ) #NEWLINE# for chunk in response . iter_content ( chunk_size ) : #NEWLINE# #INDENT# content . append ( chunk ) #NEWLINE# if ( time . time ( ) > deadline ) : #NEWLINE# #INDENT# logger . warning ( 'Download of %r failed due to download taking more than %.3fs' , uri , timeout ) #NEWLINE# return None #NEWLINE# #UNINDENT# #UNINDENT# if ( not response . ok ) : #NEWLINE# #INDENT# logger . warning ( 'Problem downloading %r: %s' , uri , response . reason ) #NEWLINE# return None #NEWLINE# #UNINDENT# return b'' . join ( content )