@@ -40,7 +40,7 @@ def _get_http_response_etag_or_last_modified(resp: Response) -> str | None:
40
40
return resp .headers .get ("etag" , resp .headers .get ("last-modified" ))
41
41
42
42
43
- def _prepare_download (
43
+ def _log_download (
44
44
resp : Response ,
45
45
link : Link ,
46
46
progress_bar : str ,
@@ -175,13 +175,13 @@ def __init__(
175
175
def batch (
176
176
self , links : Iterable [Link ], location : str
177
177
) -> Iterable [tuple [Link , tuple [str , str ]]]:
178
- """Download the files given by links into location ."""
178
+ """Convenience method to download multiple links."""
179
179
for link in links :
180
180
filepath , content_type = self (link , location )
181
181
yield link , (filepath , content_type )
182
182
183
183
def __call__ (self , link : Link , location : str ) -> tuple [str , str ]:
184
- """Download the file given by link into location."""
184
+ """Download a link and save it under location."""
185
185
resp = self ._http_get (link )
186
186
download_size = _get_http_response_size (resp )
187
187
@@ -190,14 +190,14 @@ def __call__(self, link: Link, location: str) -> tuple[str, str]:
190
190
download = _FileDownload (link , content_file , download_size )
191
191
self ._process_response (download , resp )
192
192
if download .is_incomplete ():
193
- self ._attempt_resume (download , resp )
193
+ self ._attempt_resumes_or_redownloads (download , resp )
194
194
195
195
content_type = resp .headers .get ("Content-Type" , "" )
196
196
return filepath , content_type
197
197
198
198
def _process_response (self , download : _FileDownload , resp : Response ) -> None :
199
- """Process the response and write the chunks to the file ."""
200
- chunks = _prepare_download (
199
+ """Download and save chunks from a response ."""
200
+ chunks = _log_download (
201
201
resp ,
202
202
download .link ,
203
203
self ._progress_bar ,
@@ -214,8 +214,10 @@ def _process_response(self, download: _FileDownload, resp: Response) -> None:
214
214
215
215
logger .warning ("Connection timed out while downloading." )
216
216
217
- def _attempt_resume (self , download : _FileDownload , resp : Response ) -> None :
218
- """Attempt to resume the download if connection was dropped."""
217
+ def _attempt_resumes_or_redownloads (
218
+ self , download : _FileDownload , resp : Response
219
+ ) -> None :
220
+ """Attempt to resume/restart the download if connection was dropped."""
219
221
220
222
while download .reattempts < self ._resume_retries and download .is_incomplete ():
221
223
assert download .size is not None
@@ -250,6 +252,8 @@ def _http_get_resume(
250
252
self , download : _FileDownload , should_match : Response
251
253
) -> Response :
252
254
"""Issue a HTTP range request to resume the download."""
255
+ # To better understand the download resumption logic, see the mdn web docs:
256
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Range_requests
253
257
headers = HEADERS .copy ()
254
258
headers ["Range" ] = f"bytes={ download .bytes_received } -"
255
259
# If possible, use a conditional range request to avoid corrupted
0 commit comments