summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/helper.py
diff options
context:
space:
mode:
authorrazrab <razrab@PimoLin>2023-10-23 07:25:46 +0200
committerrazrab <razrab@PimoLin>2023-10-23 07:25:46 +0200
commit2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c (patch)
tree7de51129865bd786bdf7b21fa658725a474a8cd8 /g4f/Provider/helper.py
parentAdded code to ignore certain providers in the API (diff)
parent~ | g4f `v-0.1.7.5` (diff)
downloadgpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.gz
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.bz2
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.lz
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.xz
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.tar.zst
gpt4free-2b3bc749b39b0c6f3e8921b9e3b2fc52263c568c.zip
Diffstat (limited to 'g4f/Provider/helper.py')
-rw-r--r--g4f/Provider/helper.py34
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