Hansimov commited on
Commit
21f117b
1 Parent(s): 1bbf682

:recycle: [Refactor] Format ports among classes

Browse files
conversation_connector.py CHANGED
@@ -36,24 +36,19 @@ class ConversationConnectRequestHeadersConstructor:
36
  class ConversationConnector:
37
  def __init__(
38
  self,
39
- conversation_style="precise",
40
- sec_access_token=None,
41
- client_id=None,
42
- conversation_id=None,
43
- invocation_id=0,
44
  cookies={},
45
  ):
46
  self.conversation_style = conversation_style
47
  self.sec_access_token = sec_access_token
48
- self.quotelized_sec_access_token = urllib.parse.quote(self.sec_access_token)
49
  self.client_id = client_id
50
  self.conversation_id = conversation_id
51
  self.invocation_id = invocation_id
52
  self.cookies = cookies
53
- self.ws_url = (
54
- f"wss://sydney.bing.com/sydney/ChatHub"
55
- f"?sec_access_token={self.quotelized_sec_access_token}"
56
- )
57
 
58
  async def wss_send(self, message):
59
  serialized_websocket_message = json.dumps(message, ensure_ascii=False) + "\x1e"
@@ -65,6 +60,11 @@ class ConversationConnector:
65
  await self.wss_send({"type": 6})
66
 
67
  async def init_wss_connection(self):
 
 
 
 
 
68
  self.aiohttp_session = aiohttp.ClientSession(cookies=self.cookies)
69
  request_headers_constructor = ConversationConnectRequestHeadersConstructor()
70
  self.wss = await self.aiohttp_session.ws_connect(
 
36
  class ConversationConnector:
37
  def __init__(
38
  self,
39
+ conversation_style: str = "precise",
40
+ sec_access_token: str = "",
41
+ client_id: str = "",
42
+ conversation_id: str = "",
43
+ invocation_id: int = 0,
44
  cookies={},
45
  ):
46
  self.conversation_style = conversation_style
47
  self.sec_access_token = sec_access_token
 
48
  self.client_id = client_id
49
  self.conversation_id = conversation_id
50
  self.invocation_id = invocation_id
51
  self.cookies = cookies
 
 
 
 
52
 
53
  async def wss_send(self, message):
54
  serialized_websocket_message = json.dumps(message, ensure_ascii=False) + "\x1e"
 
60
  await self.wss_send({"type": 6})
61
 
62
  async def init_wss_connection(self):
63
+ self.quotelized_sec_access_token = urllib.parse.quote(self.sec_access_token)
64
+ self.ws_url = (
65
+ f"wss://sydney.bing.com/sydney/ChatHub"
66
+ f"?sec_access_token={self.quotelized_sec_access_token}"
67
+ )
68
  self.aiohttp_session = aiohttp.ClientSession(cookies=self.cookies)
69
  request_headers_constructor = ConversationConnectRequestHeadersConstructor()
70
  self.wss = await self.aiohttp_session.ws_connect(
conversation_creator.py CHANGED
@@ -1,6 +1,6 @@
1
  import httpx
2
  import json
3
- import pprint
4
 
5
  http_proxy = "http://localhost:11111" # Replace with yours
6
 
@@ -8,16 +8,16 @@ http_proxy = "http://localhost:11111" # Replace with yours
8
  class ConversationCreator:
9
  conversation_create_url = "https://www.bing.com/turing/conversation/create"
10
 
11
- def __init__(self, cookies={}):
12
  self.cookies = cookies
13
- self.construct_cookies()
14
 
15
  def construct_cookies(self):
16
  self.httpx_cookies = httpx.Cookies()
17
  for key, val in self.cookies.items():
18
  self.httpx_cookies.set(key, val)
19
 
20
- def create(self, proxy=None):
 
21
  self.response = httpx.get(
22
  self.conversation_create_url,
23
  proxies=http_proxy if proxy is None else proxy,
@@ -25,8 +25,14 @@ class ConversationCreator:
25
  )
26
  self.response_content = json.loads(self.response.content.decode("utf-8"))
27
  self.response_headers = dict(self.response.headers)
28
- pprint.pprint(self.response_content)
29
- # pprint.pprint(self.response_headers)
 
 
 
 
 
 
30
 
31
 
32
  if __name__ == "__main__":
 
1
  import httpx
2
  import json
3
+ from pprint import pprint
4
 
5
  http_proxy = "http://localhost:11111" # Replace with yours
6
 
 
8
  class ConversationCreator:
9
  conversation_create_url = "https://www.bing.com/turing/conversation/create"
10
 
11
+ def __init__(self, cookies: dict = {}):
12
  self.cookies = cookies
 
13
 
14
  def construct_cookies(self):
15
  self.httpx_cookies = httpx.Cookies()
16
  for key, val in self.cookies.items():
17
  self.httpx_cookies.set(key, val)
18
 
19
+ def create(self, proxy: str = None):
20
+ self.construct_cookies()
21
  self.response = httpx.get(
22
  self.conversation_create_url,
23
  proxies=http_proxy if proxy is None else proxy,
 
25
  )
26
  self.response_content = json.loads(self.response.content.decode("utf-8"))
27
  self.response_headers = dict(self.response.headers)
28
+ pprint(self.response_content)
29
+
30
+ # These info would be used in ConversationConnector
31
+ self.sec_access_token = self.response_headers[
32
+ "x-sydney-encryptedconversationsignature"
33
+ ]
34
+ self.client_id = self.response_content["clientId"]
35
+ self.conversation_id = self.response_content["conversationId"]
36
 
37
 
38
  if __name__ == "__main__":
conversation_session.py CHANGED
@@ -5,7 +5,7 @@ from logger.logger import logger
5
 
6
 
7
  class ConversationSession:
8
- def __init__(self, conversation_style="precise"):
9
  self.conversation_style = conversation_style
10
 
11
  def __enter__(self):
@@ -22,11 +22,9 @@ class ConversationSession:
22
  def connect(self):
23
  self.connector = ConversationConnector(
24
  conversation_style=self.conversation_style,
25
- sec_access_token=self.creator.response_headers[
26
- "x-sydney-encryptedconversationsignature"
27
- ],
28
- client_id=self.creator.response_content["clientId"],
29
- conversation_id=self.creator.response_content["conversationId"],
30
  )
31
 
32
  def open(self):
 
5
 
6
 
7
  class ConversationSession:
8
+ def __init__(self, conversation_style: str = "precise"):
9
  self.conversation_style = conversation_style
10
 
11
  def __enter__(self):
 
22
  def connect(self):
23
  self.connector = ConversationConnector(
24
  conversation_style=self.conversation_style,
25
+ sec_access_token=self.creator.sec_access_token,
26
+ client_id=self.creator.client_id,
27
+ conversation_id=self.creator.conversation_id,
 
 
28
  )
29
 
30
  def open(self):