diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-10-22 23:55:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-22 23:55:32 +0200 |
commit | 955fb4bbaa3c31787bbe699aa913f22ec3a4a639 (patch) | |
tree | c298e3feb1110b047a74cdb224024b412712323a /g4f/Provider/helper.py | |
parent | Merge pull request #1122 from Luneye/patch-2 (diff) | |
parent | Debug logging support (diff) | |
download | gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.gz gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.bz2 gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.lz gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.xz gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.zst gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.zip |
Diffstat (limited to 'g4f/Provider/helper.py')
-rw-r--r-- | g4f/Provider/helper.py | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/g4f/Provider/helper.py b/g4f/Provider/helper.py index 096293b2..1b00ace5 100644 --- a/g4f/Provider/helper.py +++ b/g4f/Provider/helper.py @@ -3,13 +3,14 @@ from __future__ import annotations import sys import asyncio import webbrowser -import http.cookiejar from os import path from asyncio import AbstractEventLoop +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': @@ -44,7 +45,6 @@ def get_event_loop() -> AbstractEventLoop: ) def init_cookies(): - urls = [ 'https://chat-gpt.org', 'https://www.aitianhu.com', @@ -72,16 +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=''): - cj = http.cookiejar.CookieJar() - for cookie_fn in [chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]: + 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) + cookies = {} + for cookie_fn in [g4f, chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]: try: - for cookie in cookie_fn(domain_name=domain_name): - cj.set_cookie(cookie) - except BrowserCookieError: + cookie_jar = cookie_fn(domain_name=domain_name) + 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 cj} - + _cookies[domain_name] = cookies return _cookies[domain_name] @@ -100,10 +110,8 @@ def format_prompt(messages: Messages, add_special_tokens=False) -> str: def get_browser(user_data_dir: str = None): from undetected_chromedriver import Chrome - from platformdirs import user_config_dir if not user_data_dir: user_data_dir = user_config_dir("g4f") - user_data_dir = path.join(user_data_dir, "Default") return Chrome(user_data_dir=user_data_dir)
\ No newline at end of file |