import asyncio, json, subprocess, time, requests, websockets
key=subprocess.check_output(["security","find-generic-password","-s","getxapi-key","-w"]).decode().strip()
ws_url=requests.get("http://127.0.0.1:9555/json/version").json()["webSocketDebuggerUrl"]
async def _c():
    async with websockets.connect(ws_url, max_size=50_000_000) as ws:
        await ws.send(json.dumps({"id":1,"method":"Storage.getCookies"}))
        while True:
            m=json.loads(await ws.recv())
            if m.get("id")==1: return m["result"]["cookies"]
ck={c["name"]:c["value"] for c in asyncio.get_event_loop().run_until_complete(_c())
    if c.get("domain","").endswith("x.com") and c["name"] in ("auth_token","ct0","twid")}
H={"Authorization":f"Bearer {key}","Content-Type":"application/json"}
base="https://api.getxapi.com/twitter/tweet"; body={"auth_token":ck["auth_token"],"ct0":ck.get("ct0"),"twid":ck.get("twid")}
def post(payload,tries=4):
    for i in range(tries):
        r=requests.post(f"{base}/create",headers=H,json={**body,**payload},timeout=60)
        if r.status_code!=429: return r
        print(f"  429, backoff {2**i}s"); time.sleep(2**i)
    return r
r=post({"text":"transport reply-lane check (auto-delete)"})
print("CREATE",r.status_code,r.text[:120]); rid=r.json()["data"]["id"]
time.sleep(6)
r2=post({"text":"this is the reply/comment lane (auto-delete)","reply_to_tweet_id":rid})
print("REPLY ",r2.status_code,r2.text[:160]); cid=(r2.json().get("data") or {}).get("id")
time.sleep(3)
for tid,l in [(cid,"reply"),(rid,"root")]:
    if tid:
        d=requests.post(f"{base}/delete",headers=H,json={**body,"tweet_id":tid},timeout=60)
        print("DELETE",l,d.status_code)
