diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-04-22 01:35:07 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-22 01:35:07 +0200 |
commit | 4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7 (patch) | |
tree | 6ed0cfc6cd53a3ab32565d6199a929ac1ea6ad80 /g4f/cookies.py | |
parent | Merge pull request #1869 from hlohaus/carst (diff) | |
parent | Add vision models to readme (diff) | |
download | gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.gz gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.bz2 gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.lz gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.xz gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.tar.zst gpt4free-4b4d1f08b5c75c8c8932b5edfbb0d020f8e029a7.zip |
Diffstat (limited to 'g4f/cookies.py')
-rw-r--r-- | g4f/cookies.py | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/g4f/cookies.py b/g4f/cookies.py index 578be8db..efd6e969 100644 --- a/g4f/cookies.py +++ b/g4f/cookies.py @@ -2,6 +2,7 @@ from __future__ import annotations import os import time +import json try: from platformdirs import user_config_dir @@ -38,6 +39,7 @@ def get_cookies(domain_name: str = '', raise_requirements_error: bool = True, si Returns: Dict[str, str]: A dictionary of cookie names and values. """ + global _cookies if domain_name in _cookies: return _cookies[domain_name] @@ -46,6 +48,7 @@ def get_cookies(domain_name: str = '', raise_requirements_error: bool = True, si return cookies def set_cookies(domain_name: str, cookies: Cookies = None) -> None: + global _cookies if cookies: _cookies[domain_name] = cookies elif domain_name in _cookies: @@ -84,6 +87,61 @@ def load_cookies_from_browsers(domain_name: str, raise_requirements_error: bool print(f"Error reading cookies from {cookie_fn.__name__} for {domain_name}: {e}") return cookies +def read_cookie_files(dirPath: str = "./har_and_cookies"): + global _cookies + harFiles = [] + cookieFiles = [] + for root, dirs, files in os.walk(dirPath): + for file in files: + if file.endswith(".har"): + harFiles.append(os.path.join(root, file)) + elif file.endswith(".json"): + cookieFiles.append(os.path.join(root, file)) + _cookies = {} + for path in harFiles: + with open(path, 'rb') as file: + try: + harFile = json.load(file) + except json.JSONDecodeError: + # Error: not a HAR file! + continue + if debug.logging: + print("Read .har file:", path) + new_cookies = {} + for v in harFile['log']['entries']: + v_cookies = {} + for c in v['request']['cookies']: + if c['domain'] not in v_cookies: + v_cookies[c['domain']] = {} + v_cookies[c['domain']][c['name']] = c['value'] + for domain, c in v_cookies.items(): + _cookies[domain] = c + new_cookies[domain] = len(c) + if debug.logging: + for domain, new_values in new_cookies.items(): + print(f"Cookies added: {new_values} from {domain}") + for path in cookieFiles: + with open(path, 'rb') as file: + try: + cookieFile = json.load(file) + except json.JSONDecodeError: + # Error: not a json file! + continue + if not isinstance(cookieFile, list): + continue + if debug.logging: + print("Read cookie file:", path) + new_cookies = {} + for c in cookieFile: + if isinstance(c, dict) and "domain" in c: + if c["domain"] not in new_cookies: + new_cookies[c["domain"]] = {} + new_cookies[c["domain"]][c["name"]] = c["value"] + for domain, new_values in new_cookies.items(): + if debug.logging: + print(f"Cookies added: {len(new_values)} from {domain}") + _cookies[domain] = new_values + def _g4f(domain_name: str) -> list: """ Load cookies from the 'g4f' browser (if exists). |