@@ -71,18 +71,19 @@ def download_path(url):
7171
7272 return os .path .join (temp_path (), filename )
7373
74+
7475def _http_request (url , headers = None , time_out = 30 ):
7576 """Make a robust HTTP request handling redirections."""
7677 try :
77- with urlopen (url , timeout = time_out ) as response :
78- if response .status in [301 , 302 , 303 , 307 , 308 ]: # Handle redirections
79- new_url = response .getheader ('Location' )
80- log (1 , f"Redirecting to { new_url } " )
81- return _http_request (new_url , time_out )
82- return response
78+ response = urlopen (url , timeout = time_out ) # pylint: disable=consider-using-with:w
79+ if response .status in [301 , 302 , 303 , 307 , 308 ]: # Handle redirections
80+ new_url = response .getheader ('Location' )
81+ log (1 , f"Redirecting to { new_url } " )
82+ return _http_request (new_url , time_out )
83+ return response # Return the response for streaming
8384 except (HTTPError , URLError ) as err :
8485 log (2 , 'Download failed with error {}' .format (err ))
85- if yesno_dialog (localize (30004 ), '{line1}\n {line2}' .format (line1 = localize (30063 ), line2 = localize (30065 ))): # Internet down, try again?
86+ if yesno_dialog (localize (30004 ), '{line1}\n {line2}' .format (line1 = localize (30063 ), line2 = localize (30065 ))):
8687 return _http_request (url , headers , time_out )
8788 return None
8889 except timeout as err :
@@ -110,8 +111,7 @@ def http_head(url):
110111 except HTTPError as exc :
111112 return exc .getcode ()
112113
113-
114- def http_download (url , message = None , checksum = None , hash_alg = 'sha1' , dl_size = None , background = False ): # pylint: disable=too-many-statements
114+ def http_download (url , message = None , checksum = None , hash_alg = 'sha1' , dl_size = None , background = False ): # pylint: disable=too-many-statements
115115 """Makes HTTP request and displays a progress dialog on download."""
116116 if checksum :
117117 from hashlib import md5 , sha1
@@ -123,19 +123,16 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
123123 log (4 , 'Invalid hash algorithm specified: {}' .format (hash_alg ))
124124 checksum = None
125125
126- req = _http_request (url )
127- if req is None :
126+ response = _http_request (url )
127+ if response is None :
128128 return None
129129
130130 dl_path = download_path (url )
131131 filename = os .path .basename (dl_path )
132- if not message : # display "downloading [filename]"
132+ if not message :
133133 message = localize (30015 , filename = filename ) # Downloading file
134134
135- total_length = int (req .info ().get ('content-length' , 0 ))
136- if total_length == 0 :
137- log (2 , 'No content-length header available, download may not progress as expected.' )
138-
135+ total_length = int (response .info ().get ('content-length' , 0 ))
139136 if dl_size and dl_size != total_length :
140137 log (2 , 'The given file size does not match the request!' )
141138 dl_size = total_length
@@ -151,7 +148,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
151148 size = 0
152149 with open (compat_path (dl_path ), 'wb' ) as image :
153150 while True :
154- chunk = req .read (chunk_size )
151+ chunk = response .read (chunk_size )
155152 if not chunk :
156153 break
157154
@@ -162,7 +159,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
162159 percent = int (round (size * 100 / total_length )) if total_length > 0 else 0
163160 if not background and progress .iscanceled ():
164161 progress .close ()
165- req .close ()
162+ response .close ()
166163 return False
167164 if time () - starttime > 5 and size > 0 :
168165 time_left = int (round ((total_length - size ) * (time () - starttime ) / size ))
@@ -175,7 +172,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
175172 progress .update (percent , prog_message )
176173
177174 progress .close ()
178- req .close ()
175+ response .close ()
179176
180177 checksum_ok = (not checksum or calc_checksum .hexdigest () == checksum )
181178 size_ok = (not dl_size or stat_file (dl_path ).st_size () == dl_size )
0 commit comments