Add function to save Forefront cookies

This commit is contained in:
kondo 2023-05-03 02:12:37 +09:00
parent 43e5e21bbb
commit f6db1426e4
1 changed files with 33 additions and 1 deletions

View File

@ -1,3 +1,5 @@
import os
import pickle
from json import loads
from re import findall
from time import time, sleep
@ -13,8 +15,29 @@ from .typing import ForeFrontResponse
class Account:
COOKIES_FILE_NAME = 'cookies.pickle'
@staticmethod
def create(proxy: Optional[str] = None, logging: bool = False):
def login(proxy: Optional[str] = None, logging: bool = False) -> str:
if not os.path.isfile(Account.COOKIES_FILE_NAME):
return Account.create(proxy, logging)
with open(Account.COOKIES_FILE_NAME, 'rb') as f:
cookies = pickle.load(f)
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
client = Session(client_identifier='chrome110')
client.proxies = proxies
client.cookies.update(cookies)
if Account.is_cookie_enabled(client):
response = client.get('https://clerk.forefront.ai/v1/client?_clerk_js_version=4.38.4')
return response.json()['response']['sessions'][0]['last_active_token']['jwt']
else:
return Account.create(proxy, logging)
@staticmethod
def create(proxy: Optional[str] = None, logging: bool = False, save_cookies: bool = False) -> str:
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else False
start = time()
@ -72,6 +95,10 @@ class Account:
token = response.json()['response']['sessions'][0]['last_active_token']['jwt']
if save_cookies:
with open(Account.COOKIES_FILE_NAME, 'wb') as f:
pickle.dump(client.cookies, f)
with open('accounts.txt', 'a') as f:
f.write(f'{mail_address}:{token}\n')
@ -80,6 +107,11 @@ class Account:
return token
@staticmethod
def is_cookie_enabled(client: Session) -> bool:
response = client.get('https://chat.forefront.ai/')
return 'window.startClerk' in response.text
class StreamingCompletion:
@staticmethod