Parse Google cookies from JSON export or name=value text.
(raw: str)
| 17 | |
| 18 | |
| 19 | def _parse_google_cookies(raw: str) -> Dict[str, str]: |
| 20 | """Parse Google cookies from JSON export or name=value text.""" |
| 21 | text = (raw or "").strip() |
| 22 | if not text: |
| 23 | return {} |
| 24 | |
| 25 | try: |
| 26 | data = json.loads(text) |
| 27 | except (json.JSONDecodeError, ValueError): |
| 28 | data = None |
| 29 | |
| 30 | if isinstance(data, list): |
| 31 | result: Dict[str, str] = {} |
| 32 | for item in data: |
| 33 | if not isinstance(item, dict): |
| 34 | continue |
| 35 | name = str(item.get("name") or "").strip() |
| 36 | value = str(item.get("value") or "").strip() |
| 37 | if name and value: |
| 38 | result[name] = value |
| 39 | if result: |
| 40 | return result |
| 41 | |
| 42 | if isinstance(data, dict): |
| 43 | cookies_list = data.get("cookies") |
| 44 | if isinstance(cookies_list, list): |
| 45 | result: Dict[str, str] = {} |
| 46 | for item in cookies_list: |
| 47 | if not isinstance(item, dict): |
| 48 | continue |
| 49 | name = str(item.get("name") or "").strip() |
| 50 | value = str(item.get("value") or "").strip() |
| 51 | if name and value: |
| 52 | result[name] = value |
| 53 | if result: |
| 54 | return result |
| 55 | |
| 56 | result = { |
| 57 | str(key).strip(): str(value).strip() |
| 58 | for key, value in data.items() |
| 59 | if isinstance(value, str) and str(key).strip() and value.strip() |
| 60 | } |
| 61 | if result: |
| 62 | return result |
| 63 | |
| 64 | result: Dict[str, str] = {} |
| 65 | for part in text.split(";"): |
| 66 | part = part.strip() |
| 67 | if not part or "=" not in part: |
| 68 | continue |
| 69 | name, _, value = part.partition("=") |
| 70 | name = name.strip() |
| 71 | value = value.strip() |
| 72 | if name and value: |
| 73 | result[name] = value |
| 74 | return result |
| 75 | |
| 76 |