10
10
from aiohttp import (
11
11
ClientConnectionError ,
12
12
ClientError ,
13
+ ClientPayloadError ,
13
14
ClientResponse ,
14
15
ClientResponseError ,
15
16
ClientSession ,
31
32
from tenacity .after import after_log
32
33
from tenacity .asyncio import AsyncRetrying
33
34
from tenacity .before_sleep import before_sleep_log
34
- from tenacity .retry import (
35
- retry_if_exception ,
36
- retry_if_exception_type ,
37
- retry_if_not_exception_type ,
38
- )
35
+ from tenacity .retry import retry_if_exception , retry_if_exception_type
39
36
from tenacity .stop import stop_after_attempt
40
37
from tenacity .wait import wait_exponential
41
38
from tqdm import tqdm
@@ -157,7 +154,7 @@ async def _file_chunk_writer(
157
154
progress_bar : ProgressBarData ,
158
155
):
159
156
async with aiofiles .open (file , "wb" ) as file_pointer :
160
- async for chunk in response .content .iter_chunked (CHUNK_SIZE ):
157
+ while chunk := await response .content .read (CHUNK_SIZE ):
161
158
await file_pointer .write (chunk )
162
159
if io_log_redirect_cb and pbar .update (len (chunk )):
163
160
with log_catch (_logger , reraise = False ):
@@ -184,28 +181,25 @@ async def download_link_to_file(
184
181
progress_bar : ProgressBarData ,
185
182
):
186
183
_logger .debug ("Downloading from %s to %s" , url , file_path )
187
- try :
188
- async for attempt in AsyncRetrying (
189
- reraise = True ,
190
- wait = wait_exponential (min = 1 , max = 10 ),
191
- stop = stop_after_attempt (num_retries ),
192
- retry = retry_if_not_exception_type (
193
- (exceptions .InvalidDownloadLinkError , exceptions .TransferError )
194
- ),
195
- before_sleep = before_sleep_log (_logger , logging .WARNING , exc_info = True ),
196
- after = after_log (_logger , log_level = logging .ERROR ),
197
- ):
198
- with attempt :
199
- async with AsyncExitStack () as stack :
200
- response = await stack .enter_async_context (session .get (url ))
201
- if response .status == status .HTTP_404_NOT_FOUND :
202
- raise exceptions .InvalidDownloadLinkError (url )
203
- if response .status > _VALID_HTTP_STATUS_CODES :
204
- raise exceptions .TransferError (url )
205
- file_path .parent .mkdir (parents = True , exist_ok = True )
206
- # SEE https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
207
- file_size = int (response .headers .get ("Content-Length" , 0 )) or None
208
-
184
+ async for attempt in AsyncRetrying (
185
+ reraise = True ,
186
+ wait = wait_exponential (min = 1 , max = 10 ),
187
+ stop = stop_after_attempt (num_retries ),
188
+ retry = retry_if_exception_type (ClientConnectionError ),
189
+ before_sleep = before_sleep_log (_logger , logging .WARNING , exc_info = True ),
190
+ after = after_log (_logger , log_level = logging .ERROR ),
191
+ ):
192
+ with attempt :
193
+ async with AsyncExitStack () as stack :
194
+ response = await stack .enter_async_context (session .get (url ))
195
+ if response .status == status .HTTP_404_NOT_FOUND :
196
+ raise exceptions .InvalidDownloadLinkError (url )
197
+ if response .status > _VALID_HTTP_STATUS_CODES :
198
+ raise exceptions .TransferError (url )
199
+ file_path .parent .mkdir (parents = True , exist_ok = True )
200
+ # SEE https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
201
+ file_size = int (response .headers .get ("Content-Length" , 0 )) or None
202
+ try :
209
203
tqdm_progress = stack .enter_context (
210
204
tqdm_logging_redirect (
211
205
desc = f"downloading { url .path } --> { file_path .name } \n " ,
@@ -237,8 +231,8 @@ async def download_link_to_file(
237
231
sub_progress ,
238
232
)
239
233
_logger .debug ("Download complete" )
240
- except Exception as exc :
241
- raise exceptions .TransferError (url ) from exc
234
+ except ClientPayloadError as exc :
235
+ raise exceptions .TransferError (url ) from exc
242
236
243
237
244
238
def _check_for_aws_http_errors (exc : BaseException ) -> bool :
0 commit comments