Backend Monitorink: serveur PNG (Claude usage + météo + HA)
This commit is contained in:
44
dev/probe_usage.py
Normal file
44
dev/probe_usage.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Sonde de l'endpoint /usage Claude pour valider le parser de Monitorink.
|
||||
|
||||
Usage (sur le homelab, après `claude setup-token`) :
|
||||
MONITORINK_CLAUDE_TOKEN="sk-ant-oat01-..." python3 dev/probe_usage.py
|
||||
|
||||
Affiche la structure JSON brute renvoyée par l'API (uniquement des % d'usage, aucun secret)
|
||||
+ l'interprétation que Monitorink en fait. Sortie partageable sans risque.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
URL = "https://api.anthropic.com/api/oauth/usage"
|
||||
token = os.environ.get("MONITORINK_CLAUDE_TOKEN", "").strip()
|
||||
if not token:
|
||||
sys.exit("MONITORINK_CLAUDE_TOKEN manquant (export-le ou préfixe la commande)")
|
||||
|
||||
req = urllib.request.Request(URL, headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"anthropic-beta": "oauth-2025-04-20",
|
||||
"User-Agent": "claude-code/2.1.172",
|
||||
"Content-Type": "application/json",
|
||||
})
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=15) as r:
|
||||
data = json.load(r)
|
||||
print("HTTP", r.status)
|
||||
except urllib.error.HTTPError as e:
|
||||
sys.exit(f"HTTP {e.code}: {e.read().decode()[:300]}")
|
||||
|
||||
print("=== RÉPONSE BRUTE ===")
|
||||
print(json.dumps(data, indent=2, ensure_ascii=False))
|
||||
|
||||
print("\n=== INTERPRÉTATION MONITORINK ===")
|
||||
for key in ("five_hour", "seven_day", "seven_day_opus", "seven_day_sonnet"):
|
||||
w = data.get(key)
|
||||
if isinstance(w, dict):
|
||||
util = w.get("utilization", 0)
|
||||
print(f"{key:18s}: {100 - util:.0f}% restant (util={util}, reset={w.get('resets_at')})")
|
||||
else:
|
||||
print(f"{key:18s}: {w!r}")
|
||||
Reference in New Issue
Block a user