Debug logging support

Async browse access token
This commit is contained in:
Heiner Lohaus 2023-10-22 23:53:18 +02:00
parent 3ae90b57ed
commit 598255fa26
6 changed files with 47 additions and 38 deletions

View file

@ -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]