Files
withruntime-autoreg/wr_key2.py
T

56 lines
2.3 KiB
Python

# -*- coding: utf-8 -*-
"""wr_key2.py [label] — в профиле wrprof2 (Google-сессия) создать ключ и проверить usage."""
import json, sys, time, urllib.request
from playwright.sync_api import sync_playwright
PROF = r"C:\Users\User\tmp\wrprof2"
STATE = r"C:\Users\User\tmp\wr_state2.json"
BASE = "https://withruntime.com"
label = sys.argv[1] if len(sys.argv) > 1 else "farm"
def usage(key):
try:
req = urllib.request.Request("https://api.withruntime.com/v1/usage")
req.add_header("Authorization", "Bearer " + key)
d = json.loads(urllib.request.urlopen(req, timeout=40).read())
return {a["pool"]: {"q": a["quantity"], "left": a["left"]} for a in (d.get("allowances") or [])}
except Exception as e:
return {"err": str(e)[:100]}
with sync_playwright() as p:
ctx = p.chromium.launch_persistent_context(
PROF, channel="chrome", headless=False,
args=["--no-first-run", "--no-default-browser-check", "--disable-blink-features=AutomationControlled"],
viewport={"width": 1280, "height": 900})
pg = ctx.pages[0] if ctx.pages else ctx.new_page()
pg.goto(BASE + "/account/keys", wait_until="domcontentloaded")
time.sleep(3)
print("URL", pg.url)
js = """async (label) => {
const r = await fetch('/api/account/keys', {method:'POST', headers:{'Content-Type':'application/json'},
credentials:'include', body: JSON.stringify({action:'create', label:label, access:'full'})});
return {s: r.status, t: await r.text()};
}"""
res = pg.evaluate(js, label)
print("status", res.get("s"))
key = None
try:
d = json.loads(res["t"])
cred = d.get("credential") or {}
key = cred.get("plaintext") or cred.get("key") or d.get("key")
if not key:
print("RESP", json.dumps(d)[:400])
except Exception:
print("RAW", str(res.get("t"))[:300])
if key:
print("KEY", key[:26] + "...")
us = usage(key)
print("USAGE", json.dumps(us))
rec = {"alias": "google:flk8ka4g4op6@reforrm.me", "key": key, "ts": int(time.time()), "usage": us}
with open(r"C:\Users\User\tmp\wr_accounts.jsonl", "a", encoding="utf-8") as f:
f.write(json.dumps(rec) + "\n")
print("SAVED")
ctx.close()