#!/usr/bin/env python3
"""Inspect the live agent Chrome's Curogram cookies (names + domains only,
NOT values) to see if a usable session (incl. XSRF-TOKEN) is present."""
import json, urllib.request
from websocket import create_connection
tabs = json.loads(urllib.request.urlopen("http://localhost:9223/json", timeout=8).read())
page = next(t for t in tabs if t.get("type") == "page" and "curogram.com" in (t.get("url") or "").lower())
print("PAGE_URL", page.get("url"))
ws = create_connection(page["webSocketDebuggerUrl"], timeout=10)
ws.send(json.dumps({"id": 1, "method": "Network.getAllCookies"}))
while True:
    m = json.loads(ws.recv())
    if m.get("id") == 1:
        res = m["result"]; break
ws.close()
cks = [c for c in res.get("cookies", []) if "curogram" in c.get("domain", "")]
for c in cks:
    print("COOKIE", c["name"], "domain=", c["domain"], "len=", len(c.get("value", "")),
          "httpOnly=", c.get("httpOnly"), "secure=", c.get("secure"))
print("total_curogram_cookies", len(cks))
print("has_xsrf", any(c["name"] == "XSRF-TOKEN" for c in cks))
