Antonio Cheong
commited on
Commit
·
7b516d5
1
Parent(s):
25a4418
New public endpoint
Browse files- src/EdgeGPT.py +40 -12
src/EdgeGPT.py
CHANGED
@@ -6,7 +6,6 @@ import asyncio
|
|
6 |
import json
|
7 |
import os
|
8 |
import sys
|
9 |
-
import uuid
|
10 |
|
11 |
import requests
|
12 |
import websockets.client as websockets
|
@@ -95,22 +94,51 @@ class Conversation:
|
|
95 |
}
|
96 |
# Create cookies
|
97 |
if os.environ.get("BING_U") is None:
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
else:
|
101 |
cookies = {
|
102 |
"_U": os.environ.get("BING_U"),
|
103 |
}
|
104 |
url = "https://www.bing.com/turing/conversation/create"
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
# Return response
|
114 |
try:
|
115 |
self.struct = response.json()
|
116 |
except json.decoder.JSONDecodeError as exc:
|
|
|
6 |
import json
|
7 |
import os
|
8 |
import sys
|
|
|
9 |
|
10 |
import requests
|
11 |
import websockets.client as websockets
|
|
|
94 |
}
|
95 |
# Create cookies
|
96 |
if os.environ.get("BING_U") is None:
|
97 |
+
home = os.path.expanduser("~")
|
98 |
+
# Check if token exists
|
99 |
+
token_path = f"{home}/.config/bing_token"
|
100 |
+
# Make .config directory if it doesn't exist
|
101 |
+
if not os.path.exists(f"{home}/.config"):
|
102 |
+
os.mkdir(f"{home}/.config")
|
103 |
+
if os.path.exists(token_path):
|
104 |
+
with open(token_path, "r", encoding="utf-8") as file:
|
105 |
+
token = file.read()
|
106 |
+
else:
|
107 |
+
# POST request to get token
|
108 |
+
url = "https://images.duti.tech/allow"
|
109 |
+
response = requests.post(url, timeout=10)
|
110 |
+
if response.status_code != 200:
|
111 |
+
raise Exception("Authentication failed")
|
112 |
+
token = response.json()["token"]
|
113 |
+
# Save token
|
114 |
+
with open(token_path, "w", encoding="utf-8") as file:
|
115 |
+
file.write(token)
|
116 |
+
headers = {
|
117 |
+
"Authorization": token,
|
118 |
+
}
|
119 |
+
url = "https://images.duti.tech/auth"
|
120 |
+
# Send GET request
|
121 |
+
response = requests.get(
|
122 |
+
url,
|
123 |
+
headers=headers,
|
124 |
+
timeout=10,
|
125 |
+
)
|
126 |
+
if response.status_code != 200:
|
127 |
+
raise Exception("Authentication failed")
|
128 |
+
|
129 |
else:
|
130 |
cookies = {
|
131 |
"_U": os.environ.get("BING_U"),
|
132 |
}
|
133 |
url = "https://www.bing.com/turing/conversation/create"
|
134 |
+
# Send GET request
|
135 |
+
response = requests.get(
|
136 |
+
url,
|
137 |
+
cookies=cookies,
|
138 |
+
timeout=30,
|
139 |
+
)
|
140 |
+
if response.status_code != 200:
|
141 |
+
raise Exception("Authentication failed")
|
|
|
142 |
try:
|
143 |
self.struct = response.json()
|
144 |
except json.decoder.JSONDecodeError as exc:
|