Skip to content

Commit

Permalink
Add user-agent header to fix issue with requests getting blocked
Browse files Browse the repository at this point in the history
  • Loading branch information
Moozilla committed Dec 6, 2022
1 parent 0d4c77b commit de86da1
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/revChatGPT/revChatGPT.py
Expand Up @@ -15,12 +15,13 @@ def __init__(self, config, conversation_id=None):
self.config = config
self.conversation_id = conversation_id
self.parent_id = self.generate_uuid()
self.user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
self.refresh_headers()

def reset_chat(self):
self.conversation_id = None
self.parent_id = self.generate_uuid()

def refresh_headers(self):
if 'Authorization' not in self.config:
self.config['Authorization'] = ''
Expand All @@ -29,7 +30,8 @@ def refresh_headers(self):
self.headers = {
"Accept": "application/json",
"Authorization": "Bearer " + self.config['Authorization'],
"Content-Type": "application/json"
"Content-Type": "application/json",
"User-Agent": self.user_agent
}

def generate_uuid(self):
Expand Down Expand Up @@ -68,7 +70,7 @@ def get_chat_text(self, data):
self.conversation_id = response["conversation_id"]
message = response["message"]["content"]["parts"][0]
return {'message':message, 'conversation_id':self.conversation_id, 'parent_id':self.parent_id}

def get_chat_response(self, prompt, output="text"):
data = {
"action":"next",
Expand Down Expand Up @@ -96,13 +98,15 @@ def refresh_session(self):
# Set cookies
s.cookies.set("__Secure-next-auth.session-token", self.config['session_token'])
# s.cookies.set("__Secure-next-auth.csrf-token", self.config['csrf_token'])
response = s.get("https://chat.openai.com/api/auth/session")
response = s.get("https://chat.openai.com/api/auth/session", headers={
"User-Agent": self.user_agent
})
try:
self.config['session_token'] = response.cookies.get("__Secure-next-auth.session-token")
self.config['Authorization'] = response.json()["accessToken"]
self.refresh_headers()
except Exception as e:
print("Error refreshing session")
print("Error refreshing session")
print(response.text)
elif 'email' in self.config and 'password' in self.config:
try:
Expand All @@ -112,7 +116,7 @@ def refresh_session(self):
return e
else:
raise ValueError("No tokens provided")

def login(self, email, password):
print("Logging in...")
auth = OpenAIAuth(email, password)
Expand Down Expand Up @@ -389,4 +393,4 @@ def part_nine(self):
if is_200:
# Get the session token
# return response.json()
pass
pass

0 comments on commit de86da1

Please sign in to comment.