Hansimov commited on
Commit
f05e49d
1 Parent(s): dd0dbb2

:zap: [Enhance] Use requests to replace httpx for cleaner headers

Browse files
conversations/conversation_creator.py CHANGED
@@ -1,5 +1,4 @@
1
- import httpx
2
- import json
3
  from pprint import pprint
4
  from utils.enver import enver
5
  from networks import ConversationCreateHeadersConstructor
@@ -12,9 +11,7 @@ class ConversationCreator:
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 construct_headers(self):
20
  # New Bing 封锁原理探讨 #78
@@ -25,28 +22,27 @@ class ConversationCreator:
25
  self.construct_cookies()
26
  self.construct_headers()
27
  enver.set_envs(proxies=True)
28
- self.response = httpx.get(
29
  self.conversation_create_url,
30
  headers=self.request_headers,
31
- proxies=enver.proxy,
32
- cookies=self.httpx_cookies,
33
  )
34
  try:
35
- self.response_content = json.loads(self.response.content.decode("utf-8"))
36
  except:
37
- print(self.response.content)
38
  raise Exception(
39
  f"x Failed to create conversation: {self.response.status_code}"
40
  )
41
- self.response_headers = dict(self.response.headers)
42
- pprint(self.response_content)
43
 
44
  # These info would be used in ConversationConnector
45
  self.sec_access_token = self.response_headers[
46
  "x-sydney-encryptedconversationsignature"
47
  ]
48
- self.client_id = self.response_content["clientId"]
49
- self.conversation_id = self.response_content["conversationId"]
50
 
51
 
52
  if __name__ == "__main__":
 
1
+ import requests
 
2
  from pprint import pprint
3
  from utils.enver import enver
4
  from networks import ConversationCreateHeadersConstructor
 
11
  self.cookies = cookies
12
 
13
  def construct_cookies(self):
14
+ pass
 
 
15
 
16
  def construct_headers(self):
17
  # New Bing 封锁原理探讨 #78
 
22
  self.construct_cookies()
23
  self.construct_headers()
24
  enver.set_envs(proxies=True)
25
+ self.response = requests.get(
26
  self.conversation_create_url,
27
  headers=self.request_headers,
28
+ proxies=enver.requests_proxies,
 
29
  )
30
  try:
31
+ self.response_data = self.response.json()
32
  except:
33
+ print(self.response.text)
34
  raise Exception(
35
  f"x Failed to create conversation: {self.response.status_code}"
36
  )
37
+ self.response_headers = self.response.headers
38
+ pprint(self.response_data)
39
 
40
  # These info would be used in ConversationConnector
41
  self.sec_access_token = self.response_headers[
42
  "x-sydney-encryptedconversationsignature"
43
  ]
44
+ self.client_id = self.response_data["clientId"]
45
+ self.conversation_id = self.response_data["conversationId"]
46
 
47
 
48
  if __name__ == "__main__":
networks/conversation_create_headers_constructor.py CHANGED
@@ -4,5 +4,7 @@ class ConversationCreateHeadersConstructor:
4
 
5
  def construct(self):
6
  self.request_headers = {
7
- "X-Forwarded-For": "65.49.22.66",
 
 
8
  }
 
4
 
5
  def construct(self):
6
  self.request_headers = {
7
+ "Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&setlang=en&cc=us",
8
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
9
+ "X-Ms-Useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.12.0 OS/Windows",
10
  }
requirements.txt CHANGED
@@ -3,6 +3,7 @@ fastapi
3
  httpx
4
  openai
5
  pydantic
 
6
  sse_starlette
7
  termcolor
8
  uvicorn
 
3
  httpx
4
  openai
5
  pydantic
6
+ requests
7
  sse_starlette
8
  termcolor
9
  uvicorn
utils/enver.py CHANGED
@@ -50,6 +50,10 @@ class OSEnver:
50
  or self.envs.get("https_proxy")
51
  or None
52
  )
 
 
 
 
53
 
54
 
55
  enver = OSEnver()
 
50
  or self.envs.get("https_proxy")
51
  or None
52
  )
53
+ self.requests_proxies = {
54
+ "http": self.proxy,
55
+ "https": self.proxy,
56
+ }
57
 
58
 
59
  enver = OSEnver()