bing-chat-api / conversations /conversation_creator.py
Hansimov's picture
:zap: [Enhance] Use requests to replace httpx for cleaner headers
f05e49d
raw
history blame
1.6 kB
import requests
from pprint import pprint
from utils.enver import enver
from networks import ConversationCreateHeadersConstructor
class ConversationCreator:
conversation_create_url = "https://www.bing.com/turing/conversation/create"
def __init__(self, cookies: dict = {}):
self.cookies = cookies
def construct_cookies(self):
pass
def construct_headers(self):
# New Bing 封锁原理探讨 #78
# https://github.com/weaigc/bingo/issues/78
self.request_headers = ConversationCreateHeadersConstructor().request_headers
def create(self):
self.construct_cookies()
self.construct_headers()
enver.set_envs(proxies=True)
self.response = requests.get(
self.conversation_create_url,
headers=self.request_headers,
proxies=enver.requests_proxies,
)
try:
self.response_data = self.response.json()
except:
print(self.response.text)
raise Exception(
f"x Failed to create conversation: {self.response.status_code}"
)
self.response_headers = self.response.headers
pprint(self.response_data)
# These info would be used in ConversationConnector
self.sec_access_token = self.response_headers[
"x-sydney-encryptedconversationsignature"
]
self.client_id = self.response_data["clientId"]
self.conversation_id = self.response_data["conversationId"]
if __name__ == "__main__":
creator = ConversationCreator()
creator.create()