Refactor users.json
This commit is contained in:
@ -5,7 +5,7 @@ import os
|
|||||||
|
|
||||||
# Define static variables for paths and URLs
|
# Define static variables for paths and URLs
|
||||||
CONFIG_FILE = '/etc/hysteria/config.json'
|
CONFIG_FILE = '/etc/hysteria/config.json'
|
||||||
TRAFFIC_FILE = '/etc/hysteria/traffic_data.json'
|
USERS_FILE = '/etc/hysteria/users.json'
|
||||||
TRAFFIC_API_URL = 'http://127.0.0.1:25413/traffic?clear=1'
|
TRAFFIC_API_URL = 'http://127.0.0.1:25413/traffic?clear=1'
|
||||||
ONLINE_API_URL = 'http://127.0.0.1:25413/online'
|
ONLINE_API_URL = 'http://127.0.0.1:25413/online'
|
||||||
|
|
||||||
@ -47,40 +47,38 @@ def traffic_status():
|
|||||||
response_dict = json.loads(response)
|
response_dict = json.loads(response)
|
||||||
online_dict = json.loads(online_response)
|
online_dict = json.loads(online_response)
|
||||||
|
|
||||||
traffic_data = {}
|
# Load the current users.json data
|
||||||
|
users_data = {}
|
||||||
for user in response_dict.keys():
|
if os.path.exists(USERS_FILE):
|
||||||
tx_bytes = response_dict[user].get('tx', 0)
|
|
||||||
rx_bytes = response_dict[user].get('rx', 0)
|
|
||||||
online = online_dict.get(user, 0)
|
|
||||||
|
|
||||||
traffic_data[user] = {
|
|
||||||
"upload_bytes": tx_bytes,
|
|
||||||
"download_bytes": rx_bytes,
|
|
||||||
"status": "Online" if online == 1 else "Offline"
|
|
||||||
}
|
|
||||||
|
|
||||||
existing_data = {}
|
|
||||||
if os.path.exists(TRAFFIC_FILE):
|
|
||||||
try:
|
try:
|
||||||
with open(TRAFFIC_FILE, 'r') as json_file:
|
with open(USERS_FILE, 'r') as users_file:
|
||||||
existing_data = json.load(json_file)
|
users_data = json.load(users_file)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
print("Error: Failed to parse existing traffic data JSON file.")
|
print("Error: Failed to parse existing users data JSON file.")
|
||||||
return
|
return
|
||||||
|
|
||||||
for user, data in traffic_data.items():
|
# Update users.json with traffic data
|
||||||
if user in existing_data:
|
for user, traffic_info in response_dict.items():
|
||||||
existing_data[user]["upload_bytes"] += data["upload_bytes"]
|
tx_bytes = traffic_info.get('tx', 0)
|
||||||
existing_data[user]["download_bytes"] += data["download_bytes"]
|
rx_bytes = traffic_info.get('rx', 0)
|
||||||
existing_data[user]["status"] = data["status"]
|
online_status = online_dict.get(user, 0)
|
||||||
|
|
||||||
|
if user in users_data:
|
||||||
|
users_data[user]["upload_bytes"] = users_data[user].get("upload_bytes", 0) + tx_bytes
|
||||||
|
users_data[user]["download_bytes"] = users_data[user].get("download_bytes", 0) + rx_bytes
|
||||||
|
users_data[user]["status"] = "Online" if online_status == 1 else "Offline"
|
||||||
else:
|
else:
|
||||||
existing_data[user] = data
|
users_data[user] = {
|
||||||
|
"upload_bytes": tx_bytes,
|
||||||
|
"download_bytes": rx_bytes,
|
||||||
|
"status": "Online" if online_status == 1 else "Offline"
|
||||||
|
}
|
||||||
|
|
||||||
with open(TRAFFIC_FILE, 'w') as json_file:
|
# Save the updated data back to users.json
|
||||||
json.dump(existing_data, json_file, indent=4)
|
with open(USERS_FILE, 'w') as users_file:
|
||||||
|
json.dump(users_data, users_file, indent=4)
|
||||||
|
|
||||||
display_traffic_data(existing_data, green, cyan, NC)
|
display_traffic_data(users_data, green, cyan, NC)
|
||||||
|
|
||||||
def display_traffic_data(data, green, cyan, NC):
|
def display_traffic_data(data, green, cyan, NC):
|
||||||
if not data:
|
if not data:
|
||||||
@ -93,9 +91,9 @@ def display_traffic_data(data, green, cyan, NC):
|
|||||||
print("-------------------------------------------------")
|
print("-------------------------------------------------")
|
||||||
|
|
||||||
for user, entry in data.items():
|
for user, entry in data.items():
|
||||||
upload_bytes = entry["upload_bytes"]
|
upload_bytes = entry.get("upload_bytes", 0)
|
||||||
download_bytes = entry["download_bytes"]
|
download_bytes = entry.get("download_bytes", 0)
|
||||||
status = entry["status"]
|
status = entry.get("status", "Offline")
|
||||||
|
|
||||||
formatted_tx = format_bytes(upload_bytes)
|
formatted_tx = format_bytes(upload_bytes)
|
||||||
formatted_rx = format_bytes(download_bytes)
|
formatted_rx = format_bytes(download_bytes)
|
||||||
@ -114,3 +112,6 @@ def format_bytes(bytes):
|
|||||||
return f"{bytes / 1073741824:.2f}GB"
|
return f"{bytes / 1073741824:.2f}GB"
|
||||||
else:
|
else:
|
||||||
return f"{bytes / 1099511627776:.2f}TB"
|
return f"{bytes / 1099511627776:.2f}TB"
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
traffic_status()
|
||||||
|
|||||||
Reference in New Issue
Block a user