From 598255fa26e39d57e51fb41957a381a9241cb032 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sun, 22 Oct 2023 23:53:18 +0200 Subject: Debug logging support Async browse access token --- g4f/Provider/helper.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'g4f/Provider/helper.py') 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] -- cgit v1.2.3