From b90ae5b746d0dab0821140d84bcb96aea3813c6e Mon Sep 17 00:00:00 2001 From: X-Gorn Date: Sun, 9 Jun 2024 18:03:50 +0900 Subject: [PATCH] fix: download_coroutine --- Bot/functions/download.py | 70 +++++++++++++++++++++------------------ Bot/plugins/echo.py | 6 ++-- 2 files changed, 40 insertions(+), 36 deletions(-) diff --git a/Bot/functions/download.py b/Bot/functions/download.py index 35be004..cc034dc 100644 --- a/Bot/functions/download.py +++ b/Bot/functions/download.py @@ -8,7 +8,7 @@ from .. import client -async def download_coroutine(bot: Client, session: ClientSession, url: str, file_name: str, chat_id: Union[str, int], message_id: int, start: float, headers: dict): +async def download_coroutine(bot: Union[Client, None], session: ClientSession, url: str, file_name: str, chat_id: Union[str, int, None], message_id: Union[int, None], start: float, headers: dict): downloaded = 0 display_message = "" async with session.get(url, timeout=client.config.PROCESS_MAX_TIMEOUT, headers=headers) as response: @@ -17,12 +17,13 @@ async def download_coroutine(bot: Client, session: ClientSession, url: str, file if "text" in content_type and total_length < 500 and total_length: return await response.release() if total_length: - await bot.edit_message_text( - chat_id, - message_id, - text="Initiating Download\nURL: {}\nFile Size: {}".format( - url, humanbytes(total_length)) - ) + if bot: + await bot.edit_message_text( + chat_id, + message_id, + text="Initiating Download\nURL: {}\nFile Size: {}".format( + url, humanbytes(total_length)) + ) with open(file_name, "wb") as f_handle: while True: chunk = await response.content.read(client.config.CHUNK_SIZE) @@ -41,35 +42,38 @@ async def download_coroutine(bot: Client, session: ClientSession, url: str, file (total_length - downloaded) / speed) * 1000 estimated_total_time = elapsed_time + time_to_completion try: - current_message = "Download Status\nURL: {}\nFile Size: {}\nDownloaded: {}\nETA: {}".format( - url, humanbytes(total_length), humanbytes(downloaded), TimeFormatter(estimated_total_time)) + current_message = "Download Status {}%\nURL: {}\nFile Size: {}\nDownloaded: {}\nETA: {}".format(percentage, + url, humanbytes(total_length), humanbytes(downloaded), TimeFormatter(estimated_total_time)) if current_message != display_message: + if bot: + await bot.edit_message_text( + chat_id, + message_id, + text=current_message + ) + display_message = current_message + except FloodWait: + pass + except Exception as e: + if bot: + error = str(e) await bot.edit_message_text( chat_id, message_id, - text=current_message + text=f"Error: {error}" ) - display_message = current_message - except FloodWait: - pass - except Exception as e: - error = str(e) - await bot.edit_message_text( - chat_id, - message_id, - text=f"Error: {error}" - ) - try: - await bot.edit_message_text( - chat_id, - message_id, - text=f"Download Completed." - ) - except FloodWait as e: - await asyncio.sleep(e.value) - await bot.edit_message_text( - chat_id, - message_id, - text=f"Download Completed." - ) + if bot: + try: + await bot.edit_message_text( + chat_id, + message_id, + text=f"Download Completed." + ) + except FloodWait as e: + await asyncio.sleep(e.value) + await bot.edit_message_text( + chat_id, + message_id, + text=f"Download Completed." + ) return await response.release() diff --git a/Bot/plugins/echo.py b/Bot/plugins/echo.py index 3efbcef..79cdd9b 100644 --- a/Bot/plugins/echo.py +++ b/Bot/plugins/echo.py @@ -246,12 +246,12 @@ async def echo_http(bot: Client, update: Message): thumb_image_path = client.config.DOWNLOAD_LOCATION + \ "/" + str(update.from_user.id) + ".webp" await download_coroutine( - bot=bot, + bot=None, session=client.session, url=thumbnail_image, file_name=thumb_image_path, - chat_id=update.from_user.id, - message_id=update.id, + chat_id=None, + message_id=None, start=time.time(), headers=None )