@@ -71,18 +71,19 @@ def download_path(url):
71
71
72
72
return os .path .join (temp_path (), filename )
73
73
74
+
74
75
def _http_request (url , headers = None , time_out = 30 ):
75
76
"""Make a robust HTTP request handling redirections."""
76
77
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
83
84
except (HTTPError , URLError ) as err :
84
85
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 ))):
86
87
return _http_request (url , headers , time_out )
87
88
return None
88
89
except timeout as err :
@@ -110,8 +111,7 @@ def http_head(url):
110
111
except HTTPError as exc :
111
112
return exc .getcode ()
112
113
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
115
115
"""Makes HTTP request and displays a progress dialog on download."""
116
116
if checksum :
117
117
from hashlib import md5 , sha1
@@ -123,19 +123,16 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
123
123
log (4 , 'Invalid hash algorithm specified: {}' .format (hash_alg ))
124
124
checksum = None
125
125
126
- req = _http_request (url )
127
- if req is None :
126
+ response = _http_request (url )
127
+ if response is None :
128
128
return None
129
129
130
130
dl_path = download_path (url )
131
131
filename = os .path .basename (dl_path )
132
- if not message : # display "downloading [filename]"
132
+ if not message :
133
133
message = localize (30015 , filename = filename ) # Downloading file
134
134
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 ))
139
136
if dl_size and dl_size != total_length :
140
137
log (2 , 'The given file size does not match the request!' )
141
138
dl_size = total_length
@@ -151,7 +148,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
151
148
size = 0
152
149
with open (compat_path (dl_path ), 'wb' ) as image :
153
150
while True :
154
- chunk = req .read (chunk_size )
151
+ chunk = response .read (chunk_size )
155
152
if not chunk :
156
153
break
157
154
@@ -162,7 +159,7 @@ def http_download(url, message=None, checksum=None, hash_alg='sha1', dl_size=Non
162
159
percent = int (round (size * 100 / total_length )) if total_length > 0 else 0
163
160
if not background and progress .iscanceled ():
164
161
progress .close ()
165
- req .close ()
162
+ response .close ()
166
163
return False
167
164
if time () - starttime > 5 and size > 0 :
168
165
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
175
172
progress .update (percent , prog_message )
176
173
177
174
progress .close ()
178
- req .close ()
175
+ response .close ()
179
176
180
177
checksum_ok = (not checksum or calc_checksum .hexdigest () == checksum )
181
178
size_ok = (not dl_size or stat_file (dl_path ).st_size () == dl_size )
0 commit comments