46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
"""Fetch DeepSeek API balance and save to nav page data directory."""
|
|
import json
|
|
import os
|
|
|
|
import requests
|
|
from datetime import datetime, timezone, timedelta
|
|
|
|
API_KEY = "sk-bbca4a0380d549389f0d27cdea0b5228"
|
|
OUTPUT = "/var/www/nav/data/balance.json"
|
|
|
|
tz = timezone(timedelta(hours=8))
|
|
now = datetime.now(tz).strftime("%Y-%m-%d %H:%M")
|
|
|
|
try:
|
|
resp = requests.get(
|
|
"https://api.deepseek.com/user/balance",
|
|
headers={"Authorization": f"Bearer {API_KEY}"},
|
|
timeout=10,
|
|
)
|
|
if resp.status_code == 200:
|
|
data = resp.json()
|
|
info = data.get("balance_infos", [{}])[0]
|
|
result = {
|
|
"available": data.get("is_available", False),
|
|
"total_balance": info.get("total_balance", "0.00"),
|
|
"granted_balance": info.get("granted_balance", "0.00"),
|
|
"topped_up_balance": info.get("topped_up_balance", "0.00"),
|
|
"currency": info.get("currency", "CNY"),
|
|
"updated_at": now,
|
|
"status": "ok",
|
|
}
|
|
else:
|
|
result = {
|
|
"status": "error",
|
|
"message": f"HTTP {resp.status_code}",
|
|
"updated_at": now,
|
|
}
|
|
except Exception as e:
|
|
result = {"status": "error", "message": str(e), "updated_at": now}
|
|
|
|
os.makedirs(os.path.dirname(OUTPUT), exist_ok=True)
|
|
with open(OUTPUT, "w") as f:
|
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
|
|
print(f"Written: {result.get('total_balance', 'error')}")
|