53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
import json
|
|
from pathlib import Path
|
|
from typing import Dict
|
|
|
|
|
|
def cookies_dict_to_netscape(cookies: Dict[str, str], domain: str = ".x.com") -> str:
|
|
netscape_lines = []
|
|
|
|
for name, value in cookies.items():
|
|
path = "/"
|
|
secure = "TRUE"
|
|
http_only = "FALSE"
|
|
expiry = "1735689600"
|
|
|
|
line = f"{domain}\tTRUE\t{path}\t{secure}\t{expiry}\t{name}\t{value}"
|
|
netscape_lines.append(line)
|
|
|
|
return '\n'.join(netscape_lines)
|
|
|
|
|
|
def cookies_dict_to_json(cookies: Dict[str, str], domain: str = ".x.com") -> str:
|
|
json_cookies = []
|
|
|
|
for name, value in cookies.items():
|
|
cookie_obj = {
|
|
"domain": domain,
|
|
"expirationDate": 1735689600,
|
|
"hostOnly": False,
|
|
"httpOnly": False,
|
|
"name": name,
|
|
"path": "/",
|
|
"sameSite": "no_restriction",
|
|
"secure": True,
|
|
"session": False,
|
|
"storeId": None,
|
|
"value": value
|
|
}
|
|
json_cookies.append(cookie_obj)
|
|
|
|
return json.dumps(json_cookies, indent=2)
|
|
|
|
|
|
def extract_auth_token_from_cookies(cookies: Dict[str, str]) -> str:
|
|
return cookies.get('auth_token', '')
|
|
|
|
|
|
def parse_cookie_string(cookie_string: str) -> Dict[str, str]:
|
|
cookies = {}
|
|
for pair in cookie_string.split('; '):
|
|
if '=' in pair:
|
|
name, value = pair.split('=', 1)
|
|
cookies[name] = value
|
|
return cookies
|