Skip to content

Commit

Permalink
update status_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
5hojib committed Dec 8, 2024
1 parent 319bb7e commit f8b9dfe
Showing 1 changed file with 82 additions and 5 deletions.
87 changes: 82 additions & 5 deletions bot/helper/ext_utils/status_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -258,3 +259,79 @@ async def get_readable_message(sid, is_user, page_no=1, status="All", page_step=
msg += f"<b>CPU:</b> {cpu_percent()}% | <b>FREE:</b> {get_readable_file_size(disk_usage(DOWNLOAD_DIR).free)}"
msg += f"\n<b>RAM:</b> {virtual_memory().percent}% | <b>UPTIME:</b> {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"<b>{index + start_position}. {tstatus}:</b>\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"\n<b>Speed:</b> {task.speed()}\n<b>Estimated:</b> {task.eta()}"
if hasattr(task, "seeders_num"):
with suppress(Exception):
msg += f"\n<b>Seeders:</b> {task.seeders_num()} <b>Leechers:</b> {task.leechers_num()}"
elif tstatus == MirrorStatus.STATUS_SEEDING:
msg += f"\n<b>Size: </b>{task.size()}"
msg += f"\n<b>Speed: </b>{task.seed_speed()}"
msg += f"\n<b>Uploaded: </b>{task.uploaded_bytes()}"
msg += f"\n<b>Ratio: </b>{task.ratio()}"
else:
msg += f"\n<b>Size: </b>{task.size()}"
msg += f"\n<b>Elapsed: </b>{get_readable_time(time() - task.message.date.timestamp())}"
msg += f"\n<b>By: {source (task.listener)}</b>"
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"\n<b>Page:</b> {page_no}/{pages} | <b>Tasks:</b> {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"<b>\nFree disk:</b> {get_readable_file_size(disk_usage(DOWNLOAD_DIR).free)}"
msg += f"\n<b>Bot uptime:</b> {get_readable_time(time() - bot_start_time)}"
return msg, button

0 comments on commit f8b9dfe

Please sign in to comment.