#!/usr/bin/env python3 """Snapshot current DeepSeek vault balances vs the Stage 3 final record.""" import json, pathlib from concurrent.futures import ThreadPoolExecutor, as_completed import urllib.request HERE = pathlib.Path(__file__).resolve().parent VAULT = HERE / "usable_keys/deepseek/keys.json" OUT = HERE / "results/deepseek_v2/balance_change.json" def fetch(key): try: req = urllib.request.Request( "https://api.deepseek.com/user/balance", headers={"Authorization": f"Bearer {key}"}) with urllib.request.urlopen(req, timeout=20) as r: data = json.loads(r.read()) infos = data.get("balance_infos") or [] cur = infos[0]["currency"] if infos else "CNY" bal = float(infos[0].get("total_balance") or 0.0) if infos else 0.0 return key, cur, bal, "ok" except Exception as e: return key, None, None, f"{e}" def main(): d = json.loads(pathlib.Path(VAULT).read_text()) print(f"snapshot {len(d)} keys ...", flush=True) rows = {} with ThreadPoolExecutor(max_workers=16) as ex: futs = {ex.submit(fetch, e["key"]): e for e in d} done = 0 for f in as_completed(futs): k, cur, bal, st = f.result() e = futs[f] if st != "ok": rows[k] = dict(prev_cur=e.get("currency"), prev_bal=e.get("balance"), cur=None, bal=None, delta=None, status=st) print(f" ERR {k[-6:]} {st}", flush=True) else: delta = round(bal - e.get("balance", 0.0), 2) rows[k] = dict(prev_cur=e.get("currency"), prev_bal=e.get("balance"), cur=cur, bal=bal, delta=delta, status="ok") done += 1 if done % 10 == 0: print(f" {done}/{len(d)}", flush=True) pathlib.Path(OUT).write_text(json.dumps(rows, indent=2, ensure_ascii=False)) cny_now = sum(v["bal"] for v in rows.values() if v.get("cur") == "CNY") usd_now = sum(v["bal"] for v in rows.values() if v.get("cur") == "USD") cny_prev = sum(v["prev_bal"] for v in rows.values() if v.get("prev_cur") == "CNY") usd_prev = sum(v["prev_bal"] for v in rows.values() if v.get("prev_cur") == "USD") print(f" prev: CNY {cny_prev:.2f} + USD {usd_prev:.2f}") print(f" now : CNY {cny_now:.2f} + USD {usd_now:.2f}") print("written", OUT) if __name__ == "__main__": main()