Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
anasty17
GitHub Repository: anasty17/mirror-leech-telegram-bot
Path: blob/master/bot/modules/stats.py
1621 views
1
from time import time
2
from re import search as research
3
from asyncio import gather
4
from aiofiles.os import path as aiopath
5
from psutil import (
6
disk_usage,
7
cpu_percent,
8
swap_memory,
9
cpu_count,
10
virtual_memory,
11
net_io_counters,
12
boot_time,
13
)
14
15
from .. import bot_start_time
16
from ..helper.ext_utils.status_utils import get_readable_file_size, get_readable_time
17
from ..helper.ext_utils.bot_utils import cmd_exec, new_task
18
from ..helper.telegram_helper.message_utils import send_message
19
20
commands = {
21
"aria2": (["aria2c", "--version"], r"aria2 version ([\d.]+)"),
22
"qBittorrent": (["qbittorrent-nox", "--version"], r"qBittorrent v([\d.]+)"),
23
"SABnzbd+": (["sabnzbdplus", "--version"], r"sabnzbdplus-([\d.]+)"),
24
"python": (["python3", "--version"], r"Python ([\d.]+)"),
25
"rclone": (["rclone", "--version"], r"rclone v([\d.]+)"),
26
"yt-dlp": (["yt-dlp", "--version"], r"([\d.]+)"),
27
"ffmpeg": (["ffmpeg", "-version"], r"ffmpeg version ([\d.]+(-\w+)?).*"),
28
"7z": (["7z", "i"], r"7-Zip ([\d.]+)"),
29
}
30
31
32
@new_task
33
async def bot_stats(_, message):
34
total, used, free, disk = disk_usage("/")
35
swap = swap_memory()
36
memory = virtual_memory()
37
per_cpu = cpu_percent(interval=1, percpu=True)
38
per_cpu_str = " | ".join([f"CPU{i+1}: {round(p)}%" for i, p in enumerate(per_cpu)])
39
stats = f"""
40
<b>Commit Date:</b> {commands["commit"]}
41
42
<b>Bot Uptime:</b> {get_readable_time(time() - bot_start_time)}
43
<b>OS Uptime:</b> {get_readable_time(time() - boot_time())}
44
45
<b>Total Disk Space:</b> {get_readable_file_size(total)}
46
<b>Used:</b> {get_readable_file_size(used)} | <b>Free:</b> {get_readable_file_size(free)}
47
48
<b>Upload:</b> {get_readable_file_size(net_io_counters().bytes_sent)}
49
<b>Download:</b> {get_readable_file_size(net_io_counters().bytes_recv)}
50
51
<b>CPU:</b> {cpu_percent(interval=1)}%
52
<b>CPU Cores:</b>
53
{per_cpu_str}
54
55
<b>RAM:</b> {memory.percent}%
56
<b>DISK:</b> {disk}%
57
58
<b>Physical Cores:</b> {cpu_count(logical=False)}
59
<b>Total Cores:</b> {cpu_count()}
60
<b>SWAP:</b> {get_readable_file_size(swap.total)} | <b>Used:</b> {swap.percent}%
61
62
<b>Memory Total:</b> {get_readable_file_size(memory.total)}
63
<b>Memory Free:</b> {get_readable_file_size(memory.available)}
64
<b>Memory Used:</b> {get_readable_file_size(memory.used)}
65
66
<b>python:</b> {commands["python"]}
67
<b>aria2:</b> {commands["aria2"]}
68
<b>qBittorrent:</b> {commands["qBittorrent"]}
69
<b>SABnzbd+:</b> {commands["SABnzbd+"]}
70
<b>rclone:</b> {commands["rclone"]}
71
<b>yt-dlp:</b> {commands["yt-dlp"]}
72
<b>ffmpeg:</b> {commands["ffmpeg"]}
73
<b>7z:</b> {commands["7z"]}
74
"""
75
await send_message(message, stats)
76
77
78
async def get_version_async(command, regex):
79
try:
80
out, err, code = await cmd_exec(command)
81
if code != 0:
82
return f"Error: {err}"
83
match = research(regex, out)
84
return match.group(1) if match else "Version not found"
85
except Exception as e:
86
return f"Exception: {str(e)}"
87
88
89
@new_task
90
async def get_packages_version():
91
tasks = [get_version_async(command, regex) for command, regex in commands.values()]
92
versions = await gather(*tasks)
93
for tool, version in zip(commands.keys(), versions):
94
commands[tool] = version
95
if await aiopath.exists(".git"):
96
last_commit = await cmd_exec(
97
"git log -1 --date=short --pretty=format:'%cd <b>From</b> %cr'", True
98
)
99
last_commit = last_commit[0]
100
else:
101
last_commit = "No UPSTREAM_REPO"
102
commands["commit"] = last_commit
103
104