Extract and handle traffic data

This commit is contained in:
Whispering Wind
2024-08-10 12:33:14 +03:30
committed by GitHub
parent 438307e908
commit eab1e31296

View File

@ -4,6 +4,7 @@ import qrcode
import io import io
import json import json
import os import os
import re
from dotenv import load_dotenv from dotenv import load_dotenv
from telebot import types from telebot import types
@ -87,19 +88,35 @@ def show_user(message):
def process_show_user(message): def process_show_user(message):
username = message.text.strip() username = message.text.strip()
command = f"python3 {CLI_PATH} get-user -u {username}"
result = run_cli_command(command)
if "Error" in result or "Invalid" in result: command = f"python3 {CLI_PATH} get-user -u {username}"
bot.reply_to(message, result) user_result = run_cli_command(command)
else:
user_details = json.loads(result) user_json_match = re.search(r'User Information:\s*(\{.*?\})\s*Traffic Information:\s*(\{.*?\})', user_result, re.DOTALL)
if not user_json_match:
bot.reply_to(message, "Failed to parse user details. The command output format may be incorrect.")
return
user_json, traffic_json = user_json_match.groups()
try:
user_details = json.loads(user_json)
traffic_data = json.loads(traffic_json)
except json.JSONDecodeError:
bot.reply_to(message, "Failed to parse JSON data. The command output may be malformed.")
return
formatted_details = ( formatted_details = (
f"Name: {username}\n" f"Name: {username}\n"
f"Traffic limit: {user_details['max_download_bytes'] / (1024 ** 3):.2f} GB\n" f"Traffic Limit: {user_details['max_download_bytes'] / (1024 ** 3):.2f} GB\n"
f"Days: {user_details['expiration_days']}\n" f"Days: {user_details['expiration_days']}\n"
f"Account Creation: {user_details['account_creation_date']}\n" f"Account Creation: {user_details['account_creation_date']}\n"
f"Blocked: {user_details['blocked']}" f"Blocked: {user_details['blocked']}\n\n"
f"**Traffic Data:**\n"
f"Upload: {traffic_data.get('upload_bytes', 0) / (1024 ** 2):.2f} MB\n"
f"Download: {traffic_data.get('download_bytes', 0) / (1024 ** 2):.2f} MB\n"
f"Status: {traffic_data.get('status', 'Unknown')}"
) )
qr_command = f"python3 {CLI_PATH} show-user-uri -u {username} -ip 4" qr_command = f"python3 {CLI_PATH} show-user-uri -u {username} -ip 4"
@ -108,8 +125,8 @@ def process_show_user(message):
if "Error" in qr_result or "Invalid" in qr_result: if "Error" in qr_result or "Invalid" in qr_result:
bot.reply_to(message, qr_result) bot.reply_to(message, qr_result)
return return
uri_v4 = qr_result.split('\n')[-1].strip()
uri_v4 = qr_result.split('\n')[-1].strip()
qr_v4 = qrcode.make(uri_v4) qr_v4 = qrcode.make(uri_v4)
bio_v4 = io.BytesIO() bio_v4 = io.BytesIO()
qr_v4.save(bio_v4, 'PNG') qr_v4.save(bio_v4, 'PNG')
@ -132,7 +149,6 @@ def process_show_user(message):
reply_markup=markup, reply_markup=markup,
parse_mode="Markdown" parse_mode="Markdown"
) )
@bot.message_handler(func=lambda message: is_admin(message.from_user.id) and message.text == 'Server Info') @bot.message_handler(func=lambda message: is_admin(message.from_user.id) and message.text == 'Server Info')
def server_info(message): def server_info(message):
command = f"python3 {CLI_PATH} server-info" command = f"python3 {CLI_PATH} server-info"