From 05723867ffb1fc43b3a86e41cb7c8ddae5bc0740 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sat, 25 Jun 2022 21:51:09 +0300 Subject: [PATCH] Refactoring --- src/app/services/fl_downloader.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/app/services/fl_downloader.py b/src/app/services/fl_downloader.py index ee43442..2bf930d 100644 --- a/src/app/services/fl_downloader.py +++ b/src/app/services/fl_downloader.py @@ -75,16 +75,16 @@ class FLDownloader(BaseDownloader): ) try: response = await client.send(request, stream=True) - except asyncio.CancelledError: + except (asyncio.CancelledError, httpx.ReadError, httpx.ConnectError) as e: await client.aclose() - raise + raise NotSuccess(str(e)) try: - content_type = response.headers.get("Content-Type") - if response.status_code != 200: raise NotSuccess(f"Status code is {response.status_code}!") + content_type = response.headers.get("Content-Type") + if "text/html" in content_type: raise ReceivedHTML() @@ -128,10 +128,13 @@ class FLDownloader(BaseDownloader): try: data = task.result() - await self._close_other_done( - {ttask for ttask in pending if not ttask.cancel()} - ) + for t_task in pending: + if not t_task.done() or t_task.cancelled(): + continue + t_task.cancel() + + await self._close_other_done(pending) await self._close_other_done( {ttask for ttask in done if ttask != task} ) @@ -213,12 +216,9 @@ class FLDownloader(BaseDownloader): converter_response = await converter_client.send( converter_request, stream=True ) - except httpx.ReadTimeout: + except (httpx.ConnectError, httpx.ReadTimeout, asyncio.CancelledError): await converter_client.aclose() - raise ConvertationError() - except asyncio.CancelledError: - await converter_client.aclose() - raise + raise ConvertationError finally: await aiofiles.os.remove(filename_to_convert)