1010from aiohttp import (
1111 ClientConnectionError ,
1212 ClientError ,
13+ ClientPayloadError ,
1314 ClientResponse ,
1415 ClientResponseError ,
1516 ClientSession ,
3132from tenacity .after import after_log
3233from tenacity .asyncio import AsyncRetrying
3334from 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
3936from tenacity .stop import stop_after_attempt
4037from tenacity .wait import wait_exponential
4138from tqdm import tqdm
@@ -157,7 +154,7 @@ async def _file_chunk_writer(
157154 progress_bar : ProgressBarData ,
158155):
159156 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 ):
161158 await file_pointer .write (chunk )
162159 if io_log_redirect_cb and pbar .update (len (chunk )):
163160 with log_catch (_logger , reraise = False ):
@@ -184,28 +181,25 @@ async def download_link_to_file(
184181 progress_bar : ProgressBarData ,
185182):
186183 _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 :
209203 tqdm_progress = stack .enter_context (
210204 tqdm_logging_redirect (
211205 desc = f"downloading { url .path } --> { file_path .name } \n " ,
@@ -237,8 +231,8 @@ async def download_link_to_file(
237231 sub_progress ,
238232 )
239233 _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
242236
243237
244238def _check_for_aws_http_errors (exc : BaseException ) -> bool :
0 commit comments