@@ -40,7 +40,7 @@ def _get_http_response_etag_or_last_modified(resp: Response) -> str | None:
4040 return resp .headers .get ("etag" , resp .headers .get ("last-modified" ))
4141
4242
43- def _prepare_download (
43+ def _log_download (
4444 resp : Response ,
4545 link : Link ,
4646 progress_bar : str ,
@@ -175,13 +175,13 @@ def __init__(
175175 def batch (
176176 self , links : Iterable [Link ], location : str
177177 ) -> Iterable [tuple [Link , tuple [str , str ]]]:
178- """Download the files given by links into location ."""
178+ """Convenience method to download multiple links."""
179179 for link in links :
180180 filepath , content_type = self (link , location )
181181 yield link , (filepath , content_type )
182182
183183 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."""
185185 resp = self ._http_get (link )
186186 download_size = _get_http_response_size (resp )
187187
@@ -190,14 +190,14 @@ def __call__(self, link: Link, location: str) -> tuple[str, str]:
190190 download = _FileDownload (link , content_file , download_size )
191191 self ._process_response (download , resp )
192192 if download .is_incomplete ():
193- self ._attempt_resume (download , resp )
193+ self ._attempt_resumes_or_redownloads (download , resp )
194194
195195 content_type = resp .headers .get ("Content-Type" , "" )
196196 return filepath , content_type
197197
198198 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 (
201201 resp ,
202202 download .link ,
203203 self ._progress_bar ,
@@ -214,8 +214,10 @@ def _process_response(self, download: _FileDownload, resp: Response) -> None:
214214
215215 logger .warning ("Connection timed out while downloading." )
216216
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."""
219221
220222 while download .reattempts < self ._resume_retries and download .is_incomplete ():
221223 assert download .size is not None
@@ -250,6 +252,8 @@ def _http_get_resume(
250252 self , download : _FileDownload , should_match : Response
251253 ) -> Response :
252254 """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
253257 headers = HEADERS .copy ()
254258 headers ["Range" ] = f"bytes={ download .bytes_received } -"
255259 # If possible, use a conditional range request to avoid corrupted
0 commit comments