import math from time import time from pyrogram.types import Message from utils.i18n import tr as _ from utils.messages import message_handler async def progress_for_pyrogram( # noqa: PLR0913 current: int, total: int, ud_type: str, message: Message, start: float, language: str ) -> None: now = time() diff = now - start if current == total: await message_handler(message.edit, _("video.to_processing_queue", locale=language)) elif round(diff % 10.00) == 0: # if round(current / total * 100, 0) % 5 == 0: percentage = current * 100 / total speed = current / diff elapsed_time = round(diff) * 1000 time_to_completion = round((total - current) / speed) * 1000 estimated_total_time = elapsed_time + time_to_completion elapsed_time = time_formatter(milliseconds=elapsed_time) estimated_total_time = time_formatter(milliseconds=estimated_total_time) progress = "[{}{}] \nP: {}%\n".format( "".join(["█" for i in range(math.floor(percentage / 5))]), "".join(["░" for i in range(20 - math.floor(percentage / 5))]), round(percentage, 2), ) tmp = progress + _( "video.progress_msg", locale=language, current=humanbytes(current), total=humanbytes(total), speed=humanbytes(speed), estimated_total_time=estimated_total_time if estimated_total_time != "" else "0 s", ) await message_handler(message.edit, text=f"{ud_type}\n {tmp}") def humanbytes(size: int) -> float: # https://stackoverflow.com/a/49361727/4723940 # 2**10 = 1024 if not size: return "" power = 2**10 n = 0 dic_powern = {0: " ", 1: "Ki", 2: "Mi", 3: "Gi", 4: "Ti"} while size > power: size /= power n += 1 return str(round(size, 2)) + " " + dic_powern[n] + "B" def time_formatter(milliseconds: int) -> str: seconds, milliseconds = divmod(int(milliseconds), 1000) minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) tmp = ( ((str(days) + "d, ") if days else "") + ((str(hours) + "h, ") if hours else "") + ((str(minutes) + "m, ") if minutes else "") + ((str(seconds) + "s, ") if seconds else "") + ((str(milliseconds) + "ms, ") if milliseconds else "") ) return tmp[:-2]