Skip to content

Commit

Permalink
fix: download_coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Gorn committed Jun 9, 2024
1 parent f486b76 commit b90ae5b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
70 changes: 37 additions & 33 deletions Bot/functions/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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()
6 changes: 3 additions & 3 deletions Bot/plugins/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit b90ae5b

Please sign in to comment.