diff --git a/bot/helper/ext_utils/status_utils.py b/bot/helper/ext_utils/status_utils.py index 84fc710ac..f6de1d633 100644 --- a/bot/helper/ext_utils/status_utils.py +++ b/bot/helper/ext_utils/status_utils.py @@ -57,11 +57,11 @@ class MirrorStatus: async def get_task_by_gid(gid: str): async with task_dict_lock: - for tk in task_dict.values(): - if hasattr(tk, "seeding"): - await sync_to_async(tk.update) - if tk.gid() == gid: - return tk + for task in task_dict.values(): + if hasattr(task, "seeding"): + await sync_to_async(task.update) + if task.gid().startswith(gid): + return task return None @@ -179,6 +179,7 @@ def get_progress_bar_string(pct): return p_str +""" async def get_readable_message(sid, is_user, page_no=1, status="All", page_step=1): msg = "" button = None @@ -258,3 +259,79 @@ async def get_readable_message(sid, is_user, page_no=1, status="All", page_step= msg += f"CPU: {cpu_percent()}% | FREE: {get_readable_file_size(disk_usage(DOWNLOAD_DIR).free)}" msg += f"\nRAM: {virtual_memory().percent}% | UPTIME: {get_readable_time(time() - bot_start_time)}" return msg, button +""" + +def source(self): + return ( + sender_chat.title + if (sender_chat := self.message.sender_chat) + else self.message.from_user.username or self.message.from_user.id + ) + + +async def get_readable_message(sid, is_user, page_no=1, status="All", page_step=1): + msg = "" + button = None + + tasks = await sync_to_async(getSpecificTasks, status, sid if is_user else None) + + STATUS_LIMIT = 4 + tasks_no = len(tasks) + pages = (max(tasks_no, 1) + STATUS_LIMIT - 1) // STATUS_LIMIT + if page_no > pages: + page_no = (page_no - 1) % pages + 1 + status_dict[sid]["page_no"] = page_no + elif page_no < 1: + page_no = pages - (abs(page_no) % pages) + status_dict[sid]["page_no"] = page_no + start_position = (page_no - 1) * STATUS_LIMIT + + for index, task in enumerate( + tasks[start_position : STATUS_LIMIT + start_position], start=1 + ): + tstatus = await sync_to_async(task.status) if status == "All" else status + msg += f"{index + start_position}. {tstatus}:\n" + msg += f"{escape(f'{task.name()}')}" + if tstatus not in [ + MirrorStatus.STATUS_SPLIT, + MirrorStatus.STATUS_SEED, + MirrorStatus.STATUS_CONVERT, + MirrorStatus.STATUS_FFMPEG, + MirrorStatus.STATUS_QUEUEUP, + MirrorStatus.STATUS_SAMVID, + ]: + progress = ( + await task.progress() + if iscoroutinefunction(task.progress) + else task.progress() + ) + msg += f"\n{get_progress_bar_string(progress)} {progress}" + msg += f"\n{task.processed_bytes()} of {task.size()}" + msg += f"\nSpeed: {task.speed()}\nEstimated: {task.eta()}" + if hasattr(task, "seeders_num"): + with suppress(Exception): + msg += f"\nSeeders: {task.seeders_num()} Leechers: {task.leechers_num()}" + elif tstatus == MirrorStatus.STATUS_SEEDING: + msg += f"\nSize: {task.size()}" + msg += f"\nSpeed: {task.seed_speed()}" + msg += f"\nUploaded: {task.uploaded_bytes()}" + msg += f"\nRatio: {task.ratio()}" + else: + msg += f"\nSize: {task.size()}" + msg += f"\nElapsed: {get_readable_time(time() - task.message.date.timestamp())}" + msg += f"\nBy: {source (task.listener)}" + msg += f"\n/stop_{task.gid()[:7]}\n\n" + + if len(msg) == 0: + if status == "All": + return None, None + msg = f"No Active {status} Tasks!\n\n" + if tasks_no > STATUS_LIMIT: + buttons = ButtonMaker() + msg += f"\nPage: {page_no}/{pages} | Tasks: {tasks_no}" + buttons.data_button("Prev", f"status {sid} pre", position="header") + buttons.data_button("Next", f"status {sid} nex", position="header") + button = buttons.build_menu(2) + msg += f"\nFree disk: {get_readable_file_size(disk_usage(DOWNLOAD_DIR).free)}" + msg += f"\nBot uptime: {get_readable_time(time() - bot_start_time)}" + return msg, button