|
11 | 11 | import json |
12 | 12 | import logging |
13 | 13 | import os |
| 14 | +import psutil |
14 | 15 | import threading |
15 | 16 | import random |
16 | 17 | import re |
|
62 | 63 | spdl_download_entrance, |
63 | 64 | ) |
64 | 65 | from utils import ( |
| 66 | + sizeof_fmt, |
| 67 | + timeof_fmt, |
65 | 68 | auto_restart, |
66 | 69 | clean_tempfile, |
67 | 70 | customize_logger, |
@@ -232,18 +235,52 @@ def send_message_and_measure_ping(): |
232 | 235 |
|
233 | 236 | @app.on_message(filters.command(["stats"])) |
234 | 237 | def stats_handler(client: Client, message: types.Message): |
235 | | - redis = Redis() |
236 | 238 | chat_id = message.chat.id |
237 | 239 | client.send_chat_action(chat_id, enums.ChatAction.TYPING) |
238 | | - if os.uname().sysname == "Darwin" or ".heroku" in os.getenv("PYTHONHOME", ""): |
239 | | - bot_info = "Stats Unavailable." |
240 | | - else: |
241 | | - bot_info = get_runtime("ytdlbot_ytdl_1", "YouTube-dl") |
| 240 | + cpu_usage = psutil.cpu_percent() |
| 241 | + total, used, free, disk = psutil.disk_usage("/") |
| 242 | + swap = psutil.swap_memory() |
| 243 | + memory = psutil.virtual_memory() |
| 244 | + boot_time = psutil.boot_time() |
| 245 | + |
| 246 | + owner_stats = ( |
| 247 | + "\n\n⌬─────「 Stats 」─────⌬\n\n" |
| 248 | + f"<b>╭🖥️ **CPU Usage »**</b> __{cpu_usage}%__\n" |
| 249 | + f"<b>├💾 **RAM Usage »**</b> __{memory.percent}%__\n" |
| 250 | + f"<b>╰🗃️ **DISK Usage »**</b> __{disk}%__\n\n" |
| 251 | + f"<b>╭📤Upload:</b> {sizeof_fmt(psutil.net_io_counters().bytes_sent)}\n" |
| 252 | + f"<b>╰📥Download:</b> {sizeof_fmt(psutil.net_io_counters().bytes_recv)}\n\n\n" |
| 253 | + f"<b>Memory Total:</b> {sizeof_fmt(memory.total)}\n" |
| 254 | + f"<b>Memory Free:</b> {sizeof_fmt(memory.available)}\n" |
| 255 | + f"<b>Memory Used:</b> {sizeof_fmt(memory.used)}\n" |
| 256 | + f"<b>SWAP Total:</b> {sizeof_fmt(swap.total)} | <b>SWAP Usage:</b> {swap.percent}%\n\n" |
| 257 | + f"<b>Total Disk Space:</b> {sizeof_fmt(total)}\n" |
| 258 | + f"<b>Used:</b> {sizeof_fmt(used)} | <b>Free:</b> {sizeof_fmt(free)}\n\n" |
| 259 | + f"<b>Physical Cores:</b> {psutil.cpu_count(logical=False)}\n" |
| 260 | + f"<b>Total Cores:</b> {psutil.cpu_count(logical=True)}\n\n" |
| 261 | + f"<b>🤖Bot Uptime:</b> {timeof_fmt(time.time() - botStartTime)}\n" |
| 262 | + f"<b>⏲️OS Uptime:</b> {timeof_fmt(time.time() - boot_time)}\n" |
| 263 | + ) |
| 264 | + |
| 265 | + user_stats = ( |
| 266 | + "\n\n⌬─────「 Stats 」─────⌬\n\n" |
| 267 | + f"<b>╭🖥️ **CPU Usage »**</b> __{cpu_usage}%__\n" |
| 268 | + f"<b>├💾 **RAM Usage »**</b> __{memory.percent}%__\n" |
| 269 | + f"<b>╰🗃️ **DISK Usage »**</b> __{disk}%__\n\n" |
| 270 | + f"<b>╭📤Upload:</b> {sizeof_fmt(psutil.net_io_counters().bytes_sent)}\n" |
| 271 | + f"<b>╰📥Download:</b> {sizeof_fmt(psutil.net_io_counters().bytes_recv)}\n\n\n" |
| 272 | + f"<b>Memory Total:</b> {sizeof_fmt(memory.total)}\n" |
| 273 | + f"<b>Memory Free:</b> {sizeof_fmt(memory.available)}\n" |
| 274 | + f"<b>Memory Used:</b> {sizeof_fmt(memory.used)}\n" |
| 275 | + f"<b>Total Disk Space:</b> {sizeof_fmt(total)}\n" |
| 276 | + f"<b>Used:</b> {sizeof_fmt(used)} | <b>Free:</b> {sizeof_fmt(free)}\n\n" |
| 277 | + f"<b>🤖Bot Uptime:</b> {timeof_fmt(time.time() - botStartTime)}\n" |
| 278 | + ) |
| 279 | + |
242 | 280 | if message.chat.username == OWNER: |
243 | | - stats = BotText.ping_worker()[:1000] |
244 | | - client.send_document(chat_id, redis.generate_file(), caption=f"{bot_info}\n\n{stats}") |
| 281 | + message.reply_text(owner_stats, quote=True) |
245 | 282 | else: |
246 | | - client.send_message(chat_id, f"{bot_info.split('CPU')[0]}") |
| 283 | + message.reply_text(user_stats, quote=True) |
247 | 284 |
|
248 | 285 |
|
249 | 286 | @app.on_message(filters.command(["sub_count"])) |
@@ -709,6 +746,7 @@ def trx_notify(_, **kwargs): |
709 | 746 |
|
710 | 747 |
|
711 | 748 | if __name__ == "__main__": |
| 749 | + botStartTime = time.time() |
712 | 750 | MySQL() |
713 | 751 | TRX_SIGNAL.connect(trx_notify) |
714 | 752 | scheduler = BackgroundScheduler(timezone="Europe/London") |
|
0 commit comments