File size: 1,809 Bytes
c05a7fe
 
21f117b
dd37946
855fab6
c05a7fe
 
 
 
 
21f117b
c05a7fe
 
 
 
 
 
 
a1801ab
 
 
855fab6
a1801ab
dd37946
21f117b
a1801ab
1f691c1
c05a7fe
 
a1801ab
dcbfe03
c05a7fe
 
3eecc54
 
 
 
 
 
 
c05a7fe
21f117b
 
 
 
 
 
 
 
c05a7fe
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import httpx
import json
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):
        self.httpx_cookies = httpx.Cookies()
        for key, val in self.cookies.items():
            self.httpx_cookies.set(key, val)

    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 = httpx.get(
            self.conversation_create_url,
            headers=self.request_headers,
            proxies=enver.proxy,
            cookies=self.httpx_cookies,
        )
        try:
            self.response_content = json.loads(self.response.content.decode("utf-8"))
        except:
            print(self.response.content)
            raise Exception(
                f"x Failed to create conversation: {self.response.status_code}"
            )
        self.response_headers = dict(self.response.headers)
        pprint(self.response_content)

        # These info would be used in ConversationConnector
        self.sec_access_token = self.response_headers[
            "x-sydney-encryptedconversationsignature"
        ]
        self.client_id = self.response_content["clientId"]
        self.conversation_id = self.response_content["conversationId"]


if __name__ == "__main__":
    creator = ConversationCreator()
    creator.create()