diff options
Diffstat (limited to 'g4f/Provider/GptTalkRu.py')
-rw-r--r-- | g4f/Provider/GptTalkRu.py | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/g4f/Provider/GptTalkRu.py b/g4f/Provider/GptTalkRu.py index 16d69f3c..e8c2ffa2 100644 --- a/g4f/Provider/GptTalkRu.py +++ b/g4f/Provider/GptTalkRu.py @@ -1,10 +1,13 @@ from __future__ import annotations -from aiohttp import ClientSession +from aiohttp import ClientSession, BaseConnector from ..typing import AsyncResult, Messages from .base_provider import AsyncGeneratorProvider - +from .helper import get_random_string, get_connector +from ..requests import raise_for_status, get_args_from_browser, WebDriver +from ..webdriver import has_seleniumwire +from ..errors import MissingRequirementsError class GptTalkRu(AsyncGeneratorProvider): url = "https://gpttalk.ru" @@ -17,33 +20,40 @@ class GptTalkRu(AsyncGeneratorProvider): model: str, messages: Messages, proxy: str = None, + connector: BaseConnector = None, + webdriver: WebDriver = None, **kwargs ) -> AsyncResult: if not model: model = "gpt-3.5-turbo" - headers = { - "Accept": "application/json, text/plain, */*", - "Accept-Language": "en-US", - "Connection": "keep-alive", - "Content-Type": "application/json", - "Origin": "https://gpttalk.ru", - "Referer": "https://gpttalk.ru/", - "Sec-Fetch-Dest": "empty", - "Sec-Fetch-Mode": "cors", - "Sec-Fetch-Site": "same-origin", - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36", - "sec-ch-ua": '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"', - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-platform": '"Linux"', - } - async with ClientSession(headers=headers) as session: + if not has_seleniumwire: + raise MissingRequirementsError('Install "selenium-wire" package') + args = get_args_from_browser(f"{cls.url}", webdriver) + args["headers"]["accept"] = "application/json, text/plain, */*" + async with ClientSession(connector=get_connector(connector, proxy), **args) as session: + async with session.get("https://gpttalk.ru/getToken") as response: + await raise_for_status(response) + public_key = (await response.json())["response"]["key"]["publicKey"] + random_string = get_random_string(8) data = { "model": model, "modelType": 1, "prompt": messages, "responseType": "stream", + "security": { + "randomMessage": random_string, + "shifrText": encrypt(public_key, random_string) + } } async with session.post(f"{cls.url}/gpt2", json=data, proxy=proxy) as response: - response.raise_for_status() + await raise_for_status(response) async for chunk in response.content.iter_any(): - yield chunk.decode()
\ No newline at end of file + yield chunk.decode() + +def encrypt(public_key: str, value: str) -> str: + from Crypto.Cipher import PKCS1_v1_5 + from Crypto.PublicKey import RSA + import base64 + rsa_key = RSA.importKey(public_key) + cipher = PKCS1_v1_5.new(rsa_key) + return base64.b64encode(cipher.encrypt(value.encode())).decode()
\ No newline at end of file |