Fetch WIZ_global_data tokens from Gemini page (Push-ID, X-Client-Pctx).
()
| 12 | |
| 13 | |
| 14 | def _get_page_tokens() -> dict: |
| 15 | """Fetch WIZ_global_data tokens from Gemini page (Push-ID, X-Client-Pctx).""" |
| 16 | headers = { |
| 17 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", |
| 18 | } |
| 19 | cookie_str, sapisid = load_cookie() |
| 20 | if cookie_str: |
| 21 | headers["Cookie"] = cookie_str |
| 22 | try: |
| 23 | req = urllib.request.Request("https://gemini.google.com/app", headers=headers) |
| 24 | resp = urllib.request.urlopen(req, context=_get_ssl_ctx(), timeout=30) |
| 25 | html = resp.read().decode() |
| 26 | tokens = {} |
| 27 | for key, pattern in [ |
| 28 | ("push_id", r'"qKIAYe":"([^"]+)"'), |
| 29 | ("pctx", r'"Ylro7b":"([^"]+)"'), |
| 30 | ("at", r'"thykhd":"([^"]+)"'), |
| 31 | ]: |
| 32 | m = re.search(pattern, html) |
| 33 | if m: |
| 34 | tokens[key] = m.group(1) |
| 35 | return tokens |
| 36 | except Exception as e: |
| 37 | log(f"Page token fetch failed: {e}") |
| 38 | return {} |
| 39 | |
| 40 | |
| 41 | _page_tokens_cache = {"tokens": {}, "ts": 0} |
no test coverage detected