feat: add EditThisCookie JSON format support for cookie import

This commit is contained in:
Ben
2026-01-06 03:49:18 +00:00
parent 9d60f1a2fb
commit e9479e442a

View File

@@ -1,5 +1,6 @@
"""Authentication utilities for EVE Forums (Discourse)."""
import json
import re
from pathlib import Path
@@ -16,6 +17,52 @@ class AuthError(Exception):
pass
def parse_cookies_json(content: str) -> dict[str, str]:
"""
Parse cookies from EditThisCookie JSON export format.
EditThisCookie exports an array of cookie objects like:
[
{
"name": "_forum_session",
"value": "abc123...",
"domain": ".eveonline.com",
...
},
...
]
Args:
content: The raw JSON content
Returns:
Dictionary of cookie name -> value
"""
try:
data = json.loads(content)
# Handle array of cookie objects
if isinstance(data, list):
cookies = {}
for cookie in data:
if isinstance(cookie, dict) and "name" in cookie and "value" in cookie:
cookies[cookie["name"]] = cookie["value"]
return cookies
# Handle single object with name/value
if isinstance(data, dict) and "name" in data and "value" in data:
return {data["name"]: data["value"]}
# Handle dict of name -> value directly
if isinstance(data, dict):
return {k: v for k, v in data.items() if isinstance(v, str)}
except json.JSONDecodeError:
pass
return {}
def parse_cookies_env(content: str) -> dict[str, str]:
"""
Parse cookies from a cookies.env file.
@@ -97,6 +144,18 @@ def load_cookies(cookie_path: Path | str) -> dict[str, str]:
)
content = path.read_text()
# Try JSON format first (EditThisCookie export)
if content.strip().startswith("[") or content.strip().startswith("{"):
cookies = parse_cookies_json(content)
if cookies:
# Check for required cookies
missing = [name for name in REQUIRED_COOKIES if name not in cookies]
if not missing:
return cookies
# Fall through to try other formats if JSON didn't have required cookies
# Try key=value and Netscape formats
cookies = parse_cookies_env(content)
# Check for required cookies