DoEvent Antonio Cheong commited on
Commit
867d2ff
·
unverified ·
1 Parent(s): 44edfa0

Update EdgeGPT.py (#29)

Browse files

Co-authored-by: Antonio Cheong <[email protected]>

Files changed (1) hide show
  1. src/EdgeGPT.py +13 -6
src/EdgeGPT.py CHANGED
@@ -12,6 +12,13 @@ import websockets.client as websockets
12
 
13
  DELIMITER = "\x1e"
14
 
 
 
 
 
 
 
 
15
 
16
  class NotAllowedToAccess(Exception):
17
  pass
@@ -96,6 +103,7 @@ class Conversation:
96
  "conversationSignature": None,
97
  "result": {"value": "Success", "message": None},
98
  }
 
99
  # Create cookies
100
  if os.environ.get("BING_U") is None:
101
  home = os.path.expanduser("~")
@@ -108,18 +116,14 @@ class Conversation:
108
  with open(token_path, "r", encoding="utf-8") as file:
109
  token = file.read()
110
  else:
111
- # POST request to get token
112
  url = "https://images.duti.tech/allow"
113
- response = requests.post(url, timeout=10)
114
  if response.status_code != 200:
115
  raise Exception("Authentication failed")
116
  token = response.json()["token"]
117
  # Save token
118
  with open(token_path, "w", encoding="utf-8") as file:
119
  file.write(token)
120
- headers = {
121
- "Authorization": token,
122
- }
123
  url = "https://images.duti.tech/auth"
124
  # Send GET request
125
  response = requests.get(
@@ -140,6 +144,7 @@ class Conversation:
140
  url,
141
  cookies=cookies,
142
  timeout=30,
 
143
  )
144
  if response.status_code != 200:
145
  raise Exception("Authentication failed")
@@ -178,10 +183,12 @@ class ChatHub:
178
  if self.wss.closed:
179
  self.wss = await websockets.connect(
180
  "wss://sydney.bing.com/sydney/ChatHub",
 
 
181
  )
182
  await self.__initial_handshake()
183
  else:
184
- self.wss = await websockets.connect("wss://sydney.bing.com/sydney/ChatHub")
185
  await self.__initial_handshake()
186
  # Construct a ChatHub request
187
  self.request.update(prompt=prompt)
 
12
 
13
  DELIMITER = "\x1e"
14
 
15
+ headers = {
16
+ "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41",
17
+ "origin": "https://www.bing.com",
18
+ "referer": "https://www.bing.com/",
19
+ "sec-ch-ua": '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"',
20
+ "sec-ch-ua-platform": "Windows",
21
+ }
22
 
23
  class NotAllowedToAccess(Exception):
24
  pass
 
103
  "conversationSignature": None,
104
  "result": {"value": "Success", "message": None},
105
  }
106
+ # POST request to get token
107
  # Create cookies
108
  if os.environ.get("BING_U") is None:
109
  home = os.path.expanduser("~")
 
116
  with open(token_path, "r", encoding="utf-8") as file:
117
  token = file.read()
118
  else:
 
119
  url = "https://images.duti.tech/allow"
120
+ response = requests.post(url, timeout=10, headers=headers,)
121
  if response.status_code != 200:
122
  raise Exception("Authentication failed")
123
  token = response.json()["token"]
124
  # Save token
125
  with open(token_path, "w", encoding="utf-8") as file:
126
  file.write(token)
 
 
 
127
  url = "https://images.duti.tech/auth"
128
  # Send GET request
129
  response = requests.get(
 
144
  url,
145
  cookies=cookies,
146
  timeout=30,
147
+ headers=headers,
148
  )
149
  if response.status_code != 200:
150
  raise Exception("Authentication failed")
 
183
  if self.wss.closed:
184
  self.wss = await websockets.connect(
185
  "wss://sydney.bing.com/sydney/ChatHub",
186
+ extra_headers=headers,
187
+ max_size=None,
188
  )
189
  await self.__initial_handshake()
190
  else:
191
+ self.wss = await websockets.connect("wss://sydney.bing.com/sydney/ChatHub", extra_headers=headers, max_size=None)
192
  await self.__initial_handshake()
193
  # Construct a ChatHub request
194
  self.request.update(prompt=prompt)