返回提交历史
Modified
gpt4free/forefront/__init__.py
+33
-1
XFEstudio/gpt4free
Add function to save Forefront cookies
f6db1426
代码差异
1 个文件
+33
-1
@@ -1,3 +1,5 @@
1
import os
2
import pickle
1
3
from json import loads
2
4
from re import findall
3
5
from time import time, sleep
@@ -13,8 +15,29 @@ from .typing import ForeFrontResponse
13
15
14
16
15
17
class Account:
18
COOKIES_FILE_NAME = 'cookies.pickle'
19
16
20
@staticmethod
17
def create(proxy: Optional[str] = None, logging: bool = False):
21
def login(proxy: Optional[str] = None, logging: bool = False) -> str:
22
if not os.path.isfile(Account.COOKIES_FILE_NAME):
23
return Account.create(proxy, logging)
24
25
with open(Account.COOKIES_FILE_NAME, 'rb') as f:
26
cookies = pickle.load(f)
27
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
28
29
client = Session(client_identifier='chrome110')
30
client.proxies = proxies
31
client.cookies.update(cookies)
32
33
if Account.is_cookie_enabled(client):
34
response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4')
35
return response.json()['response']['sessions'][0]['last_active_token']['jwt']
36
else:
37
return Account.create(proxy, logging)
38
39
@staticmethod
40
def create(proxy: Optional[str] = None, logging: bool = False, save_cookies: bool = False) -> str:
18
41
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
19
42
20
43
start = time()
@@ -72,6 +95,10 @@ class Account:
72
95
73
96
token = response.json()['response']['sessions'][0]['last_active_token']['jwt']
74
97
98
if save_cookies:
99
with open(Account.COOKIES_FILE_NAME, 'wb') as f:
100
pickle.dump(client.cookies, f)
101
75
102
with open('accounts.txt', 'a') as f:
76
103
f.write(f'{mail_address}:{token}\n')
77
104
@@ -80,6 +107,11 @@ class Account:
80
107
81
108
return token
82
109
110
@staticmethod
111
def is_cookie_enabled(client: Session) -> bool:
112
response = client.get('https://chat.forefront.ai/')
113
return 'window.startClerk' in response.text
114
83
115
84
116
class StreamingCompletion:
85
117
@staticmethod