mirror of
https://github.com/xtekky/gpt4free.git
synced 2024-12-19 08:41:47 +03:00
Automatically obtain restNonce
This commit is contained in:
parent
86142cddd1
commit
b0dee1385d
@ -5,20 +5,26 @@
|
|||||||
@File :__init__.py.py
|
@File :__init__.py.py
|
||||||
@IDE :PyCharm
|
@IDE :PyCharm
|
||||||
"""
|
"""
|
||||||
|
import re
|
||||||
import json
|
import json
|
||||||
import requests
|
import base64
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
|
import requests
|
||||||
|
from fake_useragent import UserAgent
|
||||||
|
|
||||||
|
|
||||||
class ChatCompletion:
|
class ChatCompletion:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create(
|
def create(
|
||||||
messages: list,
|
messages: list,
|
||||||
context: str = "Converse as if you were an AI assistant. Be friendly, creative.",
|
context: str = "Converse as if you were an AI assistant. Be friendly, creative.",
|
||||||
restNonce:str="9d6d743bd3",
|
restNonce: str = None,
|
||||||
proxy: str = None
|
proxy: str = None
|
||||||
):
|
):
|
||||||
url = "https://chatgptlogin.ac/wp-json/ai-chatbot/v1/chat"
|
url = "https://chatgptlogin.ac/wp-json/ai-chatbot/v1/chat"
|
||||||
|
if not restNonce:
|
||||||
|
restNonce = ChatCompletion.get_restNonce(proxy)
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Wp-Nonce": restNonce
|
"X-Wp-Nonce": restNonce
|
||||||
@ -48,7 +54,6 @@ class ChatCompletion:
|
|||||||
return res.json()
|
return res.json()
|
||||||
return res.text
|
return res.text
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def randomStr():
|
def randomStr():
|
||||||
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=34))[:11]
|
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=34))[:11]
|
||||||
@ -66,7 +71,21 @@ class ChatCompletion:
|
|||||||
prompt += '\n' + "AI: "
|
prompt += '\n' + "AI: "
|
||||||
return prompt
|
return prompt
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_restNonce(cls, proxy: str = None):
|
||||||
|
url = "https://chatgptlogin.ac/"
|
||||||
|
headers = {
|
||||||
|
"Referer": "https://chatgptlogin.ac/",
|
||||||
|
"User-Agent": UserAgent().random
|
||||||
|
}
|
||||||
|
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
|
||||||
|
res = requests.get(url, headers=headers, proxies=proxies)
|
||||||
|
src = re.search(
|
||||||
|
'class="mwai-chat mwai-chatgpt">.*<span>Send</span></button></div></div></div> <script defer src="(.*?)">',
|
||||||
|
res.text).group(1)
|
||||||
|
decoded_string = base64.b64decode(src.split(",")[-1]).decode('utf-8')
|
||||||
|
restNonce = re.search(r"let restNonce = '(.*?)';", decoded_string).group(1)
|
||||||
|
return restNonce
|
||||||
|
|
||||||
|
|
||||||
class Completion:
|
class Completion:
|
||||||
|
Loading…
Reference in New Issue
Block a user