-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
78 lines (68 loc) · 2.3 KB
/
Copy pathapi.py
File metadata and controls
78 lines (68 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from flask import Flask, jsonify
INI_FILE = "/data/statistics.ini"
app = Flask(__name__)
def load_stats(path: str) -> dict:
stats = {}
with open(path, "rb") as f:
raw = f.read()
for enc in ("utf-8-sig", "utf-16", "utf-16le", "utf-16be", "latin-1"):
try:
text = raw.decode(enc); break
except UnicodeDecodeError:
continue
else:
text = raw.decode("utf-8", errors="ignore")
for line in text.splitlines():
line = line.strip()
if not line or line.startswith("[") or "=" not in line:
continue
k, v = line.split("=", 1)
key = k.strip().lower()
val = v.strip()
try:
stats[key] = int(val)
except ValueError:
try:
stats[key] = float(val)
except ValueError:
stats[key] = val
return stats
@app.route("/api/emule")
def emule_stats():
s = load_stats(INI_FILE)
# Totales
up_total = int(float(s.get("totaluploadedbytes", 0)))
down_total = int(float(s.get("totaldownloadedbytes", 0)))
runtime_total = int(float(s.get("connruntime", 0)))
ratio_total = float(up_total / down_total) if down_total > 0 else 0.0
# Sesión
up_session = (
int(s.get("updata_file", 0)) +
int(s.get("updata_partfile", 0)) +
int(s.get("upoverheadtotal", 0))
)
down_session = (
int(s.get("downdata_emule", 0)) +
int(s.get("downdata_mldonkey", 0)) +
int(s.get("downdata_lmule", 0)) +
int(s.get("downdata_amule", 0)) +
int(s.get("downdata_shareaza", 0)) +
int(s.get("downoverheadtotal", 0))
)
runtime_session = runtime_total # en eMule, ConnRunTime se resetea junto con estadísticas
ratio_session = float(up_session / down_session) if down_session > 0 else 0.0
return jsonify({
# Totales
"upload_bytes": up_total,
"download_bytes": down_total,
"ratio_total": ratio_total,
"uptime_seconds": runtime_total,
# Sesión
"session_upload_bytes": up_session,
"session_download_bytes": down_session,
"session_ratio": ratio_session,
"session_runtime_seconds": runtime_session,
"status": "up"
})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)