From f6db1426e489e7d79c52edc57133abb6525fe993 Mon Sep 17 00:00:00 2001 From: kondo Date: Wed, 3 May 2023 02:12:37 +0900 Subject: Add function to save Forefront cookies --- gpt4free/forefront/__init__.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/gpt4free/forefront/__init__.py b/gpt4free/forefront/__init__.py index 4d679a14..23978501 100644 --- a/gpt4free/forefront/__init__.py +++ b/gpt4free/forefront/__init__.py @@ -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 -- cgit v1.2.3