diff options
author | Heiner Lohaus <heiner@lohaus.eu> | 2023-10-22 23:53:18 +0200 |
---|---|---|
committer | Heiner Lohaus <heiner@lohaus.eu> | 2023-10-22 23:53:18 +0200 |
commit | 598255fa26e39d57e51fb41957a381a9241cb032 (patch) | |
tree | 041477072837e13f5ccd313e3b67fce0a0b334b4 /g4f/Provider/helper.py | |
parent | Improve get_cookies helper (diff) | |
download | gpt4free-598255fa26e39d57e51fb41957a381a9241cb032.tar gpt4free-598255fa26e39d57e51fb41957a381a9241cb032.tar.gz gpt4free-598255fa26e39d57e51fb41957a381a9241cb032.tar.bz2 gpt4free-598255fa26e39d57e51fb41957a381a9241cb032.tar.lz gpt4free-598255fa26e39d57e51fb41957a381a9241cb032.tar.xz gpt4free-598255fa26e39d57e51fb41957a381a9241cb032.tar.zst gpt4free-598255fa26e39d57e51fb41957a381a9241cb032.zip |
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/helper.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 20500777..1b00ace5 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -10,7 +10,7 @@ from platformdirs import user_config_dir from ..typing import Dict, Messages from browser_cookie3 import chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox, BrowserCookieError - +from .. import debug # Change event loop policy on windows if sys.platform == 'win32': @@ -45,7 +45,6 @@ def get_event_loop() -> AbstractEventLoop: ) def init_cookies(): - urls = [ 'https://chat-gpt.org', 'https://www.aitianhu.com', @@ -73,22 +72,26 @@ def init_cookies(): # Load cookies for a domain from all supported browsers. # Cache the results in the "_cookies" variable. def get_cookies(domain_name=''): + if domain_name in _cookies: + return _cookies[domain_name] def g4f(domain_name): user_data_dir = user_config_dir("g4f") cookie_file = path.join(user_data_dir, "Default", "Cookies") if not path.exists(cookie_file): return [] return chrome(cookie_file, domain_name) - cookie_jar = [] + cookies = {} for cookie_fn in [g4f, chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]: try: cookie_jar = cookie_fn(domain_name=domain_name) - if len(cookie_jar) > 0: - break - except BrowserCookieError: + if len(cookie_jar) and debug.logging: + print(f"Read cookies from {cookie_fn.__name__} for {domain_name}") + for cookie in cookie_jar: + if cookie.name not in cookies: + cookies[cookie.name] = cookie.value + except BrowserCookieError as e: pass - - _cookies[domain_name] = {cookie.name: cookie.value for cookie in cookie_jar} + _cookies[domain_name] = cookies return _cookies[domain_name] |