mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-11-15 06:13:01 +00:00
Improve get_cookies helper
This commit is contained in:
parent
fc15181110
commit
3ae90b57ed
@ -14,7 +14,7 @@ from .helper import format_prompt, get_cookies
|
|||||||
class ChatgptFree(AsyncProvider):
|
class ChatgptFree(AsyncProvider):
|
||||||
url = "https://chatgptfree.ai"
|
url = "https://chatgptfree.ai"
|
||||||
supports_gpt_35_turbo = True
|
supports_gpt_35_turbo = True
|
||||||
working = True
|
working = False
|
||||||
_post_id = None
|
_post_id = None
|
||||||
_nonce = None
|
_nonce = None
|
||||||
|
|
||||||
@ -24,6 +24,7 @@ class ChatgptFree(AsyncProvider):
|
|||||||
model: str,
|
model: str,
|
||||||
messages: Messages,
|
messages: Messages,
|
||||||
proxy: str = None,
|
proxy: str = None,
|
||||||
|
timeout: int = 120,
|
||||||
cookies: dict = None,
|
cookies: dict = None,
|
||||||
**kwargs
|
**kwargs
|
||||||
) -> str:
|
) -> str:
|
||||||
@ -45,14 +46,19 @@ class ChatgptFree(AsyncProvider):
|
|||||||
'sec-fetch-dest': 'empty',
|
'sec-fetch-dest': 'empty',
|
||||||
'sec-fetch-mode': 'cors',
|
'sec-fetch-mode': 'cors',
|
||||||
'sec-fetch-site': 'same-origin',
|
'sec-fetch-site': 'same-origin',
|
||||||
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
|
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
|
||||||
}
|
}
|
||||||
|
|
||||||
async with StreamSession(headers=headers,
|
async with StreamSession(
|
||||||
impersonate="chrome107", proxies={"https": proxy}, timeout=10) as session:
|
headers=headers,
|
||||||
|
cookies=cookies,
|
||||||
|
impersonate="chrome107",
|
||||||
|
proxies={"https": proxy},
|
||||||
|
timeout=timeout
|
||||||
|
) as session:
|
||||||
|
|
||||||
if not cls._nonce:
|
if not cls._nonce:
|
||||||
async with session.get(f"{cls.url}/", cookies=cookies) as response:
|
async with session.get(f"{cls.url}/") as response:
|
||||||
|
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
response = await response.text()
|
response = await response.text()
|
||||||
|
@ -3,10 +3,11 @@ from __future__ import annotations
|
|||||||
import sys
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
import webbrowser
|
import webbrowser
|
||||||
import http.cookiejar
|
|
||||||
|
|
||||||
from os import path
|
from os import path
|
||||||
from asyncio import AbstractEventLoop
|
from asyncio import AbstractEventLoop
|
||||||
|
from platformdirs import user_config_dir
|
||||||
|
|
||||||
from ..typing import Dict, Messages
|
from ..typing import Dict, Messages
|
||||||
from browser_cookie3 import chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox, BrowserCookieError
|
from browser_cookie3 import chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox, BrowserCookieError
|
||||||
|
|
||||||
@ -72,16 +73,22 @@ def init_cookies():
|
|||||||
# Load cookies for a domain from all supported browsers.
|
# Load cookies for a domain from all supported browsers.
|
||||||
# Cache the results in the "_cookies" variable.
|
# Cache the results in the "_cookies" variable.
|
||||||
def get_cookies(domain_name=''):
|
def get_cookies(domain_name=''):
|
||||||
cj = http.cookiejar.CookieJar()
|
def g4f(domain_name):
|
||||||
for cookie_fn in [chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]:
|
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 = []
|
||||||
|
for cookie_fn in [g4f, chrome, chromium, opera, opera_gx, brave, edge, vivaldi, firefox]:
|
||||||
try:
|
try:
|
||||||
for cookie in cookie_fn(domain_name=domain_name):
|
cookie_jar = cookie_fn(domain_name=domain_name)
|
||||||
cj.set_cookie(cookie)
|
if len(cookie_jar) > 0:
|
||||||
|
break
|
||||||
except BrowserCookieError:
|
except BrowserCookieError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
_cookies[domain_name] = {cookie.name: cookie.value for cookie in cj}
|
_cookies[domain_name] = {cookie.name: cookie.value for cookie in cookie_jar}
|
||||||
|
|
||||||
return _cookies[domain_name]
|
return _cookies[domain_name]
|
||||||
|
|
||||||
|
|
||||||
@ -100,10 +107,8 @@ def format_prompt(messages: Messages, add_special_tokens=False) -> str:
|
|||||||
|
|
||||||
def get_browser(user_data_dir: str = None):
|
def get_browser(user_data_dir: str = None):
|
||||||
from undetected_chromedriver import Chrome
|
from undetected_chromedriver import Chrome
|
||||||
from platformdirs import user_config_dir
|
|
||||||
|
|
||||||
if not user_data_dir:
|
if not user_data_dir:
|
||||||
user_data_dir = user_config_dir("g4f")
|
user_data_dir = user_config_dir("g4f")
|
||||||
user_data_dir = path.join(user_data_dir, "Default")
|
|
||||||
|
|
||||||
return Chrome(user_data_dir=user_data_dir)
|
return Chrome(user_data_dir=user_data_dir)
|
Loading…
Reference in New Issue
Block a user