from time import time
from re import search as research
from asyncio import gather
from aiofiles.os import path as aiopath
from psutil import (
disk_usage,
cpu_percent,
swap_memory,
cpu_count,
virtual_memory,
net_io_counters,
boot_time,
)
from .. import bot_start_time
from ..helper.ext_utils.status_utils import get_readable_file_size, get_readable_time
from ..helper.ext_utils.bot_utils import cmd_exec, new_task
from ..helper.telegram_helper.message_utils import send_message
commands = {
"aria2": (["aria2c", "--version"], r"aria2 version ([\d.]+)"),
"qBittorrent": (["qbittorrent-nox", "--version"], r"qBittorrent v([\d.]+)"),
"SABnzbd+": (["sabnzbdplus", "--version"], r"sabnzbdplus-([\d.]+)"),
"python": (["python3", "--version"], r"Python ([\d.]+)"),
"rclone": (["rclone", "--version"], r"rclone v([\d.]+)"),
"yt-dlp": (["yt-dlp", "--version"], r"([\d.]+)"),
"ffmpeg": (["ffmpeg", "-version"], r"ffmpeg version ([\d.]+(-\w+)?).*"),
"7z": (["7z", "i"], r"7-Zip ([\d.]+)"),
}
@new_task
async def bot_stats(_, message):
total, used, free, disk = disk_usage("/")
swap = swap_memory()
memory = virtual_memory()
per_cpu = cpu_percent(interval=1, percpu=True)
per_cpu_str = " | ".join([f"CPU{i+1}: {round(p)}%" for i, p in enumerate(per_cpu)])
stats = f"""
Commit Date: {commands["commit"]}
Bot Uptime: {get_readable_time(time() - bot_start_time)}
OS Uptime: {get_readable_time(time() - boot_time())}
Total Disk Space: {get_readable_file_size(total)}
Used: {get_readable_file_size(used)} | Free: {get_readable_file_size(free)}
Upload: {get_readable_file_size(net_io_counters().bytes_sent)}
Download: {get_readable_file_size(net_io_counters().bytes_recv)}
CPU: {cpu_percent(interval=1)}%
CPU Cores:
{per_cpu_str}
RAM: {memory.percent}%
DISK: {disk}%
Physical Cores: {cpu_count(logical=False)}
Total Cores: {cpu_count()}
SWAP: {get_readable_file_size(swap.total)} | Used: {swap.percent}%
Memory Total: {get_readable_file_size(memory.total)}
Memory Free: {get_readable_file_size(memory.available)}
Memory Used: {get_readable_file_size(memory.used)}
python: {commands["python"]}
aria2: {commands["aria2"]}
qBittorrent: {commands["qBittorrent"]}
SABnzbd+: {commands["SABnzbd+"]}
rclone: {commands["rclone"]}
yt-dlp: {commands["yt-dlp"]}
ffmpeg: {commands["ffmpeg"]}
7z: {commands["7z"]}
"""
await send_message(message, stats)
async def get_version_async(command, regex):
try:
out, err, code = await cmd_exec(command)
if code != 0:
return f"Error: {err}"
match = research(regex, out)
return match.group(1) if match else "Version not found"
except Exception as e:
return f"Exception: {str(e)}"
@new_task
async def get_packages_version():
tasks = [get_version_async(command, regex) for command, regex in commands.values()]
versions = await gather(*tasks)
for tool, version in zip(commands.keys(), versions):
commands[tool] = version
if await aiopath.exists(".git"):
last_commit = await cmd_exec(
"git log -1 --date=short --pretty=format:'%cd From %cr'", True
)
last_commit = last_commit[0]
else:
last_commit = "No UPSTREAM_REPO"
commands["commit"] = last_commit