91 lines
3.3 KiB
Python
91 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Speka pool status: login -> usage_current_month -> cost_usd, rebuild live pool."""
|
|
import os, sys, json, time, threading, argparse
|
|
import requests
|
|
|
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
SUPA = "https://supabase-speka.srv1729373.hstgr.cloud"
|
|
ANON = open(os.path.join(HERE, "anon.txt")).read().strip()
|
|
BANK = os.path.join(HERE, "bank.jsonl")
|
|
LIVE = os.path.join(HERE, "pool_live.jsonl")
|
|
STATUS = os.path.join(HERE, "pool_status.jsonl")
|
|
LIMIT = 1.0
|
|
_lock = threading.Lock()
|
|
|
|
|
|
def log(*a):
|
|
print(" ".join(str(x) for x in a), flush=True)
|
|
|
|
|
|
def one(rec):
|
|
email, pw = rec["email"], rec["password"]
|
|
try:
|
|
r = requests.post(SUPA + "/auth/v1/token?grant_type=password",
|
|
headers={"apikey": ANON, "content-type": "application/json"},
|
|
json={"email": email, "password": pw}, timeout=40)
|
|
if r.status_code != 200:
|
|
return {"email": email, "ok": False, "err": "login_%d" % r.status_code, "cost": None}
|
|
at = r.json()["access_token"]
|
|
u = requests.get(SUPA + "/rest/v1/usage_current_month?select=cost_usd,requests,tokens",
|
|
headers={"apikey": ANON, "Authorization": "Bearer " + at}, timeout=40)
|
|
cost = 0.0
|
|
reqs = 0
|
|
if u.status_code == 200 and u.json():
|
|
d = u.json()[0]
|
|
cost = float(d.get("cost_usd") or 0)
|
|
reqs = int(d.get("requests") or 0)
|
|
return {"email": email, "ok": True, "cost": cost, "requests": reqs,
|
|
"left": round(LIMIT - cost, 4), "key": rec.get("key"), "err": None}
|
|
except Exception as e:
|
|
return {"email": email, "ok": False, "err": "exc:" + repr(e)[:80], "cost": None}
|
|
|
|
|
|
def worker(rows, out):
|
|
for rec in rows:
|
|
r = one(rec)
|
|
r["ts"] = int(time.time())
|
|
with _lock:
|
|
out.append(r)
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("-w", "--workers", type=int, default=6)
|
|
ap.add_argument("--limit", type=float, default=LIMIT)
|
|
a = ap.parse_args()
|
|
recs = {}
|
|
for line in open(BANK, encoding="utf-8"):
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
try:
|
|
d = json.loads(line)
|
|
except Exception:
|
|
continue
|
|
if d.get("key"):
|
|
recs[d["email"]] = d
|
|
rows = list(recs.values())
|
|
log("checking %d accounts" % len(rows))
|
|
out = []
|
|
chunks = [rows[i::a.workers] for i in range(a.workers)]
|
|
ths = [threading.Thread(target=worker, args=(c, out)) for c in chunks if c]
|
|
t0 = time.time()
|
|
for t in ths:
|
|
t.start()
|
|
for t in ths:
|
|
t.join()
|
|
with open(STATUS, "w", encoding="utf-8") as f:
|
|
for r in out:
|
|
f.write(json.dumps(r, ensure_ascii=False) + "\n")
|
|
live = [r for r in out if r.get("ok") and (r.get("cost") or 0) < a.limit]
|
|
with open(LIVE, "w", encoding="utf-8") as f:
|
|
for r in live:
|
|
f.write(json.dumps({"email": r["email"], "key": r["key"], "ok": True,
|
|
"cost": r["cost"], "left": r["left"]}, ensure_ascii=False) + "\n")
|
|
total_left = sum(r["left"] for r in live if r.get("left"))
|
|
log("=== live=%d/%d spent_to_now=$%.4f left=$%.2f in %.0fs ===" %
|
|
(len(live), len(out), sum((r.get("cost") or 0) for r in out), total_left, time.time() - t0))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |