Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
New public endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonio Cheong committed Feb 11, 2023
1 parent 25a4418 commit 7b516d5
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/EdgeGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import json
import os
import sys
import uuid

import requests
import websockets.client as websockets
Expand Down Expand Up @@ -95,22 +94,51 @@ def __init__(self) -> None:
}
# Create cookies
if os.environ.get("BING_U") is None:
url = "https://bing.kpham.workers.dev/"
cookies = {}
home = os.path.expanduser("~")
# Check if token exists
token_path = f"{home}/.config/bing_token"
# Make .config directory if it doesn't exist
if not os.path.exists(f"{home}/.config"):
os.mkdir(f"{home}/.config")
if os.path.exists(token_path):
with open(token_path, "r", encoding="utf-8") as file:
token = file.read()
else:
# POST request to get token
url = "https://images.duti.tech/allow"
response = requests.post(url, timeout=10)
if response.status_code != 200:
raise Exception("Authentication failed")
token = response.json()["token"]
# Save token
with open(token_path, "w", encoding="utf-8") as file:
file.write(token)
headers = {
"Authorization": token,
}
url = "https://images.duti.tech/auth"
# Send GET request
response = requests.get(
url,
headers=headers,
timeout=10,
)
if response.status_code != 200:
raise Exception("Authentication failed")

else:
cookies = {
"_U": os.environ.get("BING_U"),
}
url = "https://www.bing.com/turing/conversation/create"
# Send GET request
response = requests.get(
url,
cookies=cookies,
timeout=30,
)
if response.status_code != 200:
raise Exception("Authentication failed")
# Return response
# Send GET request
response = requests.get(
url,
cookies=cookies,
timeout=30,
)
if response.status_code != 200:
raise Exception("Authentication failed")
try:
self.struct = response.json()
except json.decoder.JSONDecodeError as exc:
Expand Down

0 comments on commit 7b516d5

Please sign in to comment.