2023-01-11 15:59:46 +00:00
|
|
|
# type: ignore
|
|
|
|
# For some reason package not working so just deal with it and dont ask questions
|
2023-01-12 05:50:18 +00:00
|
|
|
# Copied and updated from https://github.com/acheong08/ChatGPT/blob/main/src/revChatGPT/ChatGPT.py
|
2023-01-18 07:35:30 +00:00
|
|
|
# credits to https://github.com/acheong08/ChatGPT
|
2023-01-11 15:59:46 +00:00
|
|
|
|
2023-01-18 14:38:51 +00:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import uuid
|
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import tls_client
|
2023-01-11 15:59:46 +00:00
|
|
|
import undetected_chromedriver as uc
|
2023-01-18 07:35:30 +00:00
|
|
|
from selenium.webdriver.common.by import By
|
2023-01-18 14:38:51 +00:00
|
|
|
from selenium.webdriver.support import expected_conditions as EC
|
|
|
|
from selenium.webdriver.support.ui import WebDriverWait
|
2023-01-11 15:59:46 +00:00
|
|
|
from twocaptcha import TwoCaptcha
|
2023-01-18 14:38:51 +00:00
|
|
|
|
2023-01-11 15:59:46 +00:00
|
|
|
# Disable all logging
|
|
|
|
logging.basicConfig(level=logging.ERROR)
|
|
|
|
|
|
|
|
BASE_URL = "https://chat.openai.com/"
|
|
|
|
|
|
|
|
|
|
|
|
class Chrome(uc.Chrome):
|
|
|
|
def __del__(self):
|
|
|
|
self.quit()
|
|
|
|
|
|
|
|
|
|
|
|
class Chatbot:
|
2023-01-18 14:38:51 +00:00
|
|
|
def __init__(
|
|
|
|
self, config, conversation_id=None, parent_id=None, no_refresh=False
|
|
|
|
) -> None:
|
2023-01-11 15:59:46 +00:00
|
|
|
self.config = config
|
2023-01-18 14:38:51 +00:00
|
|
|
self.session = tls_client.Session(client_identifier="chrome_108")
|
2023-01-11 15:59:46 +00:00
|
|
|
if "proxy" in config:
|
|
|
|
if type(config["proxy"]) != str:
|
|
|
|
raise Exception("Proxy must be a string!")
|
|
|
|
proxies = {
|
|
|
|
"http": config["proxy"],
|
|
|
|
"https": config["proxy"],
|
|
|
|
}
|
|
|
|
self.session.proxies.update(proxies)
|
|
|
|
if "verbose" in config:
|
|
|
|
if type(config["verbose"]) != bool:
|
|
|
|
raise Exception("Verbose must be a boolean!")
|
|
|
|
self.verbose = config["verbose"]
|
|
|
|
else:
|
|
|
|
self.verbose = False
|
|
|
|
self.conversation_id = conversation_id
|
|
|
|
self.parent_id = parent_id
|
|
|
|
self.conversation_mapping = {}
|
|
|
|
self.conversation_id_prev_queue = []
|
|
|
|
self.parent_id_prev_queue = []
|
|
|
|
self.isMicrosoftLogin = False
|
|
|
|
self.twocaptcha_key = None
|
|
|
|
# stdout colors
|
2023-01-18 14:38:51 +00:00
|
|
|
self.GREEN = "\033[92m"
|
|
|
|
self.WARNING = "\033[93m"
|
|
|
|
self.ENDCOLOR = "\033[0m"
|
2023-01-11 15:59:46 +00:00
|
|
|
if "email" in config and "password" in config:
|
|
|
|
if type(config["email"]) != str:
|
|
|
|
raise Exception("Email must be a string!")
|
|
|
|
if type(config["password"]) != str:
|
|
|
|
raise Exception("Password must be a string!")
|
|
|
|
self.email = config["email"]
|
|
|
|
self.password = config["password"]
|
|
|
|
if "isMicrosoftLogin" in config and config["isMicrosoftLogin"] == True:
|
|
|
|
self.isMicrosoftLogin = True
|
|
|
|
self.microsoft_login()
|
|
|
|
elif "captcha" in config:
|
|
|
|
if type(config["captcha"]) != str:
|
|
|
|
raise Exception("2Captcha API Key must be a string!")
|
|
|
|
self.twocaptcha_key = config["captcha"]
|
|
|
|
self.email_login(self.solve_captcha())
|
|
|
|
else:
|
|
|
|
raise Exception("Invalid config!")
|
|
|
|
elif "session_token" in config:
|
|
|
|
if no_refresh:
|
|
|
|
self.get_cf_cookies()
|
|
|
|
return
|
|
|
|
if type(config["session_token"]) != str:
|
|
|
|
raise Exception("Session token must be a string!")
|
|
|
|
self.session_token = config["session_token"]
|
|
|
|
self.session.cookies.set(
|
2023-01-18 14:38:51 +00:00
|
|
|
"__Secure-next-auth.session-token", config["session_token"]
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
self.get_cf_cookies()
|
|
|
|
else:
|
|
|
|
raise Exception("Invalid config!")
|
|
|
|
self.retry_refresh()
|
|
|
|
|
|
|
|
def retry_refresh(self):
|
|
|
|
retries = 5
|
|
|
|
refresh = True
|
|
|
|
while refresh:
|
|
|
|
try:
|
|
|
|
self.refresh_session()
|
|
|
|
refresh = False
|
2023-01-18 07:35:30 +00:00
|
|
|
except Exception as exc:
|
2023-01-11 15:59:46 +00:00
|
|
|
if retries == 0:
|
2023-01-18 07:35:30 +00:00
|
|
|
raise exc
|
2023-01-11 15:59:46 +00:00
|
|
|
retries -= 1
|
|
|
|
|
2023-01-18 14:38:51 +00:00
|
|
|
def ask(
|
|
|
|
self,
|
|
|
|
prompt,
|
|
|
|
conversation_id=None,
|
|
|
|
parent_id=None,
|
|
|
|
gen_title=False,
|
|
|
|
session_token=None,
|
|
|
|
):
|
2023-01-11 15:59:46 +00:00
|
|
|
if session_token:
|
2023-01-18 14:38:51 +00:00
|
|
|
self.session.cookies.set("__Secure-next-auth.session-token", session_token)
|
2023-01-11 15:59:46 +00:00
|
|
|
self.session_token = session_token
|
|
|
|
self.config["session_token"] = session_token
|
|
|
|
self.retry_refresh()
|
|
|
|
self.map_conversations()
|
|
|
|
if conversation_id == None:
|
|
|
|
conversation_id = self.conversation_id
|
|
|
|
if parent_id == None:
|
2023-01-18 14:38:51 +00:00
|
|
|
parent_id = (
|
|
|
|
self.parent_id
|
|
|
|
if conversation_id == self.conversation_id
|
|
|
|
else self.conversation_mapping[conversation_id]
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
data = {
|
|
|
|
"action": "next",
|
|
|
|
"messages": [
|
|
|
|
{
|
|
|
|
"id": str(uuid.uuid4()),
|
|
|
|
"role": "user",
|
|
|
|
"content": {"content_type": "text", "parts": [prompt]},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
"conversation_id": conversation_id,
|
|
|
|
"parent_message_id": parent_id or str(uuid.uuid4()),
|
|
|
|
"model": "text-davinci-002-render",
|
|
|
|
}
|
|
|
|
new_conv = data["conversation_id"] is None
|
2023-01-18 14:38:51 +00:00
|
|
|
self.conversation_id_prev_queue.append(data["conversation_id"]) # for rollback
|
2023-01-11 15:59:46 +00:00
|
|
|
self.parent_id_prev_queue.append(data["parent_message_id"])
|
|
|
|
response = self.session.post(
|
|
|
|
url=BASE_URL + "backend-api/conversation",
|
|
|
|
data=json.dumps(data),
|
2023-01-18 14:38:51 +00:00
|
|
|
timeout_seconds=180,
|
2023-01-11 15:59:46 +00:00
|
|
|
)
|
|
|
|
if response.status_code != 200:
|
|
|
|
print(response.text)
|
|
|
|
self.refresh_session()
|
|
|
|
raise Exception("Wrong response code! Refreshing session...")
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
response = response.text.splitlines()[-4]
|
|
|
|
response = response[6:]
|
|
|
|
except Exception as exc:
|
|
|
|
print("Incorrect response from OpenAI API")
|
|
|
|
raise Exception("Incorrect response from OpenAI API") from exc
|
|
|
|
# Check if it is JSON
|
|
|
|
if response.startswith("{"):
|
|
|
|
response = json.loads(response)
|
|
|
|
self.parent_id = response["message"]["id"]
|
|
|
|
self.conversation_id = response["conversation_id"]
|
|
|
|
message = response["message"]["content"]["parts"][0]
|
|
|
|
res = {
|
|
|
|
"message": message,
|
|
|
|
"conversation_id": self.conversation_id,
|
|
|
|
"parent_id": self.parent_id,
|
|
|
|
}
|
|
|
|
if gen_title and new_conv:
|
|
|
|
try:
|
2023-01-18 14:38:51 +00:00
|
|
|
title = self.gen_title(self.conversation_id, self.parent_id)[
|
|
|
|
"title"
|
|
|
|
]
|
2023-01-11 15:59:46 +00:00
|
|
|
except Exception as exc:
|
|
|
|
split = prompt.split(" ")
|
2023-01-18 14:38:51 +00:00
|
|
|
title = " ".join(split[:3]) + ("..." if len(split) > 3 else "")
|
2023-01-11 15:59:46 +00:00
|
|
|
res["title"] = title
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def check_response(self, response):
|
|
|
|
if response.status_code != 200:
|
|
|
|
print(response.text)
|
|
|
|
raise Exception("Response code error: ", response.status_code)
|
|
|
|
|
|
|
|
def get_conversations(self, offset=0, limit=20):
|
2023-01-18 14:38:51 +00:00
|
|
|
url = BASE_URL + f"backend-api/conversations?offset={offset}&limit={limit}"
|
2023-01-11 15:59:46 +00:00
|
|
|
response = self.session.get(url)
|
|
|
|
self.check_response(response)
|
|
|
|
data = json.loads(response.text)
|
|
|
|
return data["items"]
|
|
|
|
|
|
|
|
def get_msg_history(self, id):
|
|
|
|
url = BASE_URL + f"backend-api/conversation/{id}"
|
|
|
|
response = self.session.get(url)
|
|
|
|
self.check_response(response)
|
|
|
|
data = json.loads(response.text)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def gen_title(self, id, message_id):
|
|
|
|
url = BASE_URL + f"backend-api/conversation/gen_title/{id}"
|
2023-01-18 14:38:51 +00:00
|
|
|
response = self.session.post(
|
|
|
|
url,
|
|
|
|
data=json.dumps(
|
|
|
|
{"message_id": message_id, "model": "text-davinci-002-render"}
|
|
|
|
),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
self.check_response(response)
|
|
|
|
data = json.loads(response.text)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def change_title(self, id, title):
|
|
|
|
url = BASE_URL + f"backend-api/conversation/{id}"
|
2023-01-18 07:35:30 +00:00
|
|
|
response = self.session.patch(url, data=f'{{"title": "{title}"}}')
|
2023-01-11 15:59:46 +00:00
|
|
|
self.check_response(response)
|
|
|
|
|
|
|
|
def delete_conversation(self, id):
|
|
|
|
url = BASE_URL + f"backend-api/conversation/{id}"
|
|
|
|
response = self.session.patch(url, data='{"is_visible": false}')
|
|
|
|
self.check_response(response)
|
|
|
|
|
|
|
|
def map_conversations(self):
|
|
|
|
conversations = self.get_conversations()
|
|
|
|
histories = [self.get_msg_history(x["id"]) for x in conversations]
|
|
|
|
for x, y in zip(conversations, histories):
|
|
|
|
self.conversation_mapping[x["id"]] = y["current_node"]
|
|
|
|
|
|
|
|
def refresh_session(self, session_token=None):
|
|
|
|
if session_token:
|
2023-01-18 14:38:51 +00:00
|
|
|
self.session.cookies.set("__Secure-next-auth.session-token", session_token)
|
2023-01-11 15:59:46 +00:00
|
|
|
self.session_token = session_token
|
|
|
|
self.config["session_token"] = session_token
|
|
|
|
url = BASE_URL + "api/auth/session"
|
|
|
|
response = self.session.get(url, timeout_seconds=180)
|
|
|
|
if response.status_code == 403:
|
|
|
|
self.get_cf_cookies()
|
|
|
|
raise Exception("Clearance refreshing...")
|
|
|
|
try:
|
|
|
|
if "error" in response.json():
|
|
|
|
raise Exception(
|
2023-01-18 14:38:51 +00:00
|
|
|
f"Failed to refresh session! Error: {response.json()['error']}"
|
|
|
|
)
|
|
|
|
elif (
|
|
|
|
response.status_code != 200
|
|
|
|
or response.json() == {}
|
|
|
|
or "accessToken" not in response.json()
|
|
|
|
):
|
|
|
|
raise Exception(
|
|
|
|
f"Response code: {response.status_code} \n Response: {response.text}"
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
else:
|
2023-01-18 14:38:51 +00:00
|
|
|
self.session.headers.update(
|
|
|
|
{"Authorization": "Bearer " + response.json()["accessToken"]}
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
self.session_token = self.session.cookies._find(
|
2023-01-18 14:38:51 +00:00
|
|
|
"__Secure-next-auth.session-token",
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
except Exception as exc:
|
|
|
|
print("Failed to refresh session!")
|
|
|
|
if self.isMicrosoftLogin:
|
|
|
|
print("Attempting to re-authenticate...")
|
|
|
|
self.microsoft_login()
|
|
|
|
elif self.twocaptcha_key:
|
|
|
|
self.email_login(self.solve_captcha())
|
|
|
|
else:
|
|
|
|
raise Exception("Failed to refresh session!") from exc
|
|
|
|
|
|
|
|
def reset_chat(self) -> None:
|
|
|
|
"""
|
|
|
|
Reset the conversation ID and parent ID.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
self.conversation_id = None
|
|
|
|
self.parent_id = str(uuid.uuid4())
|
|
|
|
|
|
|
|
def microsoft_login(self) -> None:
|
|
|
|
"""
|
|
|
|
Login to OpenAI via Microsoft Login Authentication.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Open the browser
|
|
|
|
self.cf_cookie_found = False
|
|
|
|
self.session_cookie_found = False
|
|
|
|
self.agent_found = False
|
|
|
|
self.cf_clearance = None
|
|
|
|
self.user_agent = None
|
|
|
|
options = self.__get_ChromeOptions()
|
|
|
|
print("Spawning browser...")
|
|
|
|
driver = uc.Chrome(
|
2023-01-18 14:38:51 +00:00
|
|
|
enable_cdp_events=True,
|
|
|
|
options=options,
|
2023-01-11 15:59:46 +00:00
|
|
|
driver_executable_path=self.config.get("driver_exec_path"),
|
2023-01-18 14:38:51 +00:00
|
|
|
browser_executable_path=self.config.get("browser_exec_path"),
|
2023-01-11 15:59:46 +00:00
|
|
|
)
|
|
|
|
print("Browser spawned.")
|
|
|
|
driver.add_cdp_listener(
|
2023-01-18 14:38:51 +00:00
|
|
|
"Network.responseReceivedExtraInfo",
|
|
|
|
lambda msg: self.detect_cookies(msg),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
driver.add_cdp_listener(
|
2023-01-18 14:38:51 +00:00
|
|
|
"Network.requestWillBeSentExtraInfo",
|
|
|
|
lambda msg: self.detect_user_agent(msg),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
driver.get(BASE_URL)
|
|
|
|
while not self.agent_found or not self.cf_cookie_found:
|
|
|
|
sleep(5)
|
2023-01-18 14:38:51 +00:00
|
|
|
self.refresh_headers(
|
|
|
|
cf_clearance=self.cf_clearance, user_agent=self.user_agent
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the login button to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 120).until(
|
|
|
|
EC.element_to_be_clickable(
|
|
|
|
(By.XPATH, "//button[contains(text(), 'Log in')]")
|
|
|
|
)
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Click the login button
|
|
|
|
driver.find_element(
|
2023-01-18 14:38:51 +00:00
|
|
|
by=By.XPATH, value="//button[contains(text(), 'Log in')]"
|
|
|
|
).click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the Login with Microsoft button to be clickable
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.element_to_be_clickable(
|
|
|
|
(By.XPATH, "//button[@data-provider='windowslive']")
|
|
|
|
)
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Click the Login with Microsoft button
|
|
|
|
driver.find_element(
|
2023-01-18 14:38:51 +00:00
|
|
|
by=By.XPATH, value="//button[@data-provider='windowslive']"
|
|
|
|
).click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the email input field to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.visibility_of_element_located((By.XPATH, "//input[@type='email']"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Enter the email
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.find_element(by=By.XPATH, value="//input[@type='email']").send_keys(
|
|
|
|
self.config["email"]
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the Next button to be clickable
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.element_to_be_clickable((By.XPATH, "//input[@type='submit']"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Click the Next button
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.find_element(by=By.XPATH, value="//input[@type='submit']").click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the password input field to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.visibility_of_element_located(
|
|
|
|
(By.XPATH, "//input[@type='password']")
|
|
|
|
)
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Enter the password
|
|
|
|
driver.find_element(
|
2023-01-18 14:38:51 +00:00
|
|
|
by=By.XPATH, value="//input[@type='password']"
|
|
|
|
).send_keys(self.config["password"])
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the Sign in button to be clickable
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.element_to_be_clickable((By.XPATH, "//input[@type='submit']"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Click the Sign in button
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.find_element(by=By.XPATH, value="//input[@type='submit']").click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the Allow button to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.element_to_be_clickable((By.XPATH, "//input[@type='submit']"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# click Yes button
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.find_element(by=By.XPATH, value="//input[@type='submit']").click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# wait for input box to appear (to make sure we're signed in)
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.visibility_of_element_located((By.XPATH, "//textarea"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
while not self.session_cookie_found:
|
|
|
|
sleep(5)
|
|
|
|
print(self.GREEN + "Login successful." + self.ENDCOLOR)
|
|
|
|
finally:
|
|
|
|
# Close the browser
|
|
|
|
driver.quit()
|
|
|
|
del driver
|
|
|
|
|
|
|
|
def solve_captcha(self) -> str:
|
|
|
|
"""
|
|
|
|
Solve the 2Captcha captcha.
|
|
|
|
|
|
|
|
:return: str
|
|
|
|
"""
|
|
|
|
twocaptcha_key = self.twocaptcha_key
|
|
|
|
twocaptcha_solver_config = {
|
2023-01-18 14:38:51 +00:00
|
|
|
"apiKey": twocaptcha_key,
|
|
|
|
"defaultTimeout": 120,
|
|
|
|
"recaptchaTimeout": 600,
|
|
|
|
"pollingInterval": 10,
|
2023-01-11 15:59:46 +00:00
|
|
|
}
|
|
|
|
twocaptcha_solver = TwoCaptcha(**twocaptcha_solver_config)
|
2023-01-18 14:38:51 +00:00
|
|
|
print("Waiting for captcha to be solved...")
|
2023-01-11 15:59:46 +00:00
|
|
|
solved_captcha = twocaptcha_solver.recaptcha(
|
2023-01-18 14:38:51 +00:00
|
|
|
sitekey="6Lc-wnQjAAAAADa5SPd68d0O3xmj0030uaVzpnXP",
|
|
|
|
url="https://auth0.openai.com/u/login/identifier",
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
if "code" in solved_captcha:
|
|
|
|
print(self.GREEN + "Captcha solved successfully!" + self.ENDCOLOR)
|
|
|
|
if self.verbose:
|
2023-01-18 14:38:51 +00:00
|
|
|
print(
|
|
|
|
self.GREEN
|
|
|
|
+ "Captcha token: "
|
|
|
|
+ self.ENDCOLOR
|
|
|
|
+ solved_captcha["code"]
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
return solved_captcha
|
|
|
|
|
|
|
|
def email_login(self, solved_captcha) -> None:
|
|
|
|
"""
|
|
|
|
Login to OpenAI via Email/Password Authentication and 2Captcha.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
# Open the browser
|
|
|
|
try:
|
|
|
|
self.cf_cookie_found = False
|
|
|
|
self.session_cookie_found = False
|
|
|
|
self.agent_found = False
|
|
|
|
self.cf_clearance = None
|
|
|
|
self.user_agent = None
|
|
|
|
options = self.__get_ChromeOptions()
|
|
|
|
print("Spawning browser...")
|
|
|
|
driver = uc.Chrome(
|
2023-01-18 14:38:51 +00:00
|
|
|
enable_cdp_events=True,
|
|
|
|
options=options,
|
2023-01-11 15:59:46 +00:00
|
|
|
driver_executable_path=self.config.get("driver_exec_path"),
|
2023-01-18 14:38:51 +00:00
|
|
|
browser_executable_path=self.config.get("browser_exec_path"),
|
2023-01-11 15:59:46 +00:00
|
|
|
)
|
|
|
|
print("Browser spawned.")
|
|
|
|
driver.add_cdp_listener(
|
2023-01-18 14:38:51 +00:00
|
|
|
"Network.responseReceivedExtraInfo",
|
|
|
|
lambda msg: self.detect_cookies(msg),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
driver.add_cdp_listener(
|
2023-01-18 14:38:51 +00:00
|
|
|
"Network.requestWillBeSentExtraInfo",
|
|
|
|
lambda msg: self.detect_user_agent(msg),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
driver.get(BASE_URL)
|
|
|
|
while not self.agent_found or not self.cf_cookie_found:
|
|
|
|
sleep(5)
|
2023-01-18 14:38:51 +00:00
|
|
|
self.refresh_headers(
|
|
|
|
cf_clearance=self.cf_clearance, user_agent=self.user_agent
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the login button to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 120).until(
|
|
|
|
EC.element_to_be_clickable(
|
|
|
|
(By.XPATH, "//button[contains(text(), 'Log in')]")
|
|
|
|
)
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Click the login button
|
|
|
|
driver.find_element(
|
2023-01-18 14:38:51 +00:00
|
|
|
by=By.XPATH, value="//button[contains(text(), 'Log in')]"
|
|
|
|
).click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the email input field to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.visibility_of_element_located((By.ID, "username"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Enter the email
|
|
|
|
driver.find_element(by=By.ID, value="username").send_keys(
|
2023-01-18 14:38:51 +00:00
|
|
|
self.config["email"]
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for Recaptcha to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.presence_of_element_located(
|
|
|
|
(By.CSS_SELECTOR, "*[name*='g-recaptcha-response']")
|
|
|
|
)
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Find Recaptcha
|
|
|
|
google_captcha_response_input = driver.find_element(
|
2023-01-18 14:38:51 +00:00
|
|
|
By.CSS_SELECTOR, "*[name*='g-recaptcha-response']"
|
|
|
|
)
|
|
|
|
captcha_input = driver.find_element(By.NAME, "captcha")
|
2023-01-11 15:59:46 +00:00
|
|
|
# Make input visible
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.execute_script(
|
|
|
|
"arguments[0].setAttribute('style','type: text; visibility:visible;');",
|
|
|
|
google_captcha_response_input,
|
|
|
|
)
|
|
|
|
driver.execute_script(
|
|
|
|
"arguments[0].setAttribute('style','type: text; visibility:visible;');",
|
|
|
|
captcha_input,
|
|
|
|
)
|
|
|
|
driver.execute_script(
|
|
|
|
"""
|
2023-01-11 15:59:46 +00:00
|
|
|
document.getElementById("g-recaptcha-response").innerHTML = arguments[0]
|
2023-01-18 14:38:51 +00:00
|
|
|
""",
|
|
|
|
solved_captcha.get("code"),
|
|
|
|
)
|
|
|
|
driver.execute_script(
|
|
|
|
"""
|
2023-01-11 15:59:46 +00:00
|
|
|
document.querySelector("input[name='captcha']").value = arguments[0]
|
2023-01-18 14:38:51 +00:00
|
|
|
""",
|
|
|
|
solved_captcha.get("code"),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Hide the captcha input
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.execute_script(
|
|
|
|
"arguments[0].setAttribute('style', 'display:none;');",
|
|
|
|
google_captcha_response_input,
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the Continue button to be clickable
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Click the Continue button
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.find_element(by=By.XPATH, value="//button[@type='submit']").click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the password input field to appear
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.visibility_of_element_located((By.ID, "password"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Enter the password
|
|
|
|
driver.find_element(by=By.ID, value="password").send_keys(
|
2023-01-18 14:38:51 +00:00
|
|
|
self.config["password"]
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Wait for the Sign in button to be clickable
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
# Click the Sign in button
|
2023-01-18 14:38:51 +00:00
|
|
|
driver.find_element(by=By.XPATH, value="//button[@type='submit']").click()
|
2023-01-11 15:59:46 +00:00
|
|
|
# wait for input box to appear (to make sure we're signed in)
|
2023-01-18 14:38:51 +00:00
|
|
|
WebDriverWait(driver, 60).until(
|
|
|
|
EC.visibility_of_element_located((By.XPATH, "//textarea"))
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
while not self.session_cookie_found:
|
|
|
|
sleep(5)
|
|
|
|
print(self.GREEN + "Login successful." + self.ENDCOLOR)
|
|
|
|
finally:
|
|
|
|
# Close the browser
|
|
|
|
driver.quit()
|
|
|
|
del driver
|
|
|
|
|
|
|
|
def __get_ChromeOptions(self):
|
|
|
|
options = uc.ChromeOptions()
|
2023-01-18 14:38:51 +00:00
|
|
|
options.add_argument("--start_maximized")
|
2023-01-11 15:59:46 +00:00
|
|
|
options.add_argument("--disable-extensions")
|
2023-01-18 14:38:51 +00:00
|
|
|
options.add_argument("--disable-application-cache")
|
|
|
|
options.add_argument("--disable-gpu")
|
2023-01-11 15:59:46 +00:00
|
|
|
options.add_argument("--no-sandbox")
|
|
|
|
options.add_argument("--disable-setuid-sandbox")
|
|
|
|
options.add_argument("--disable-dev-shm-usage")
|
|
|
|
if self.config.get("proxy", "") != "":
|
|
|
|
options.add_argument("--proxy-server=" + self.config["proxy"])
|
|
|
|
return options
|
|
|
|
|
|
|
|
def get_cf_cookies(self) -> None:
|
|
|
|
"""
|
|
|
|
Get cloudflare cookies.
|
|
|
|
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.cf_cookie_found = False
|
|
|
|
self.agent_found = False
|
|
|
|
self.cf_clearance = None
|
|
|
|
self.user_agent = None
|
|
|
|
options = self.__get_ChromeOptions()
|
|
|
|
print("Spawning browser...")
|
|
|
|
driver = uc.Chrome(
|
2023-01-18 14:38:51 +00:00
|
|
|
enable_cdp_events=True,
|
|
|
|
options=options,
|
2023-01-11 15:59:46 +00:00
|
|
|
driver_executable_path=self.config.get("driver_exec_path"),
|
2023-01-18 14:38:51 +00:00
|
|
|
browser_executable_path=self.config.get("browser_exec_path"),
|
2023-01-11 15:59:46 +00:00
|
|
|
)
|
|
|
|
print("Browser spawned.")
|
|
|
|
driver.add_cdp_listener(
|
2023-01-18 14:38:51 +00:00
|
|
|
"Network.responseReceivedExtraInfo",
|
|
|
|
lambda msg: self.detect_cookies(msg),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
driver.add_cdp_listener(
|
2023-01-18 14:38:51 +00:00
|
|
|
"Network.requestWillBeSentExtraInfo",
|
|
|
|
lambda msg: self.detect_user_agent(msg),
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
driver.get("https://chat.openai.com/chat")
|
|
|
|
while not self.agent_found or not self.cf_cookie_found:
|
|
|
|
sleep(5)
|
|
|
|
finally:
|
|
|
|
# Close the browser
|
|
|
|
driver.quit()
|
|
|
|
del driver
|
2023-01-18 14:38:51 +00:00
|
|
|
self.refresh_headers(
|
|
|
|
cf_clearance=self.cf_clearance, user_agent=self.user_agent
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
|
|
|
|
def detect_cookies(self, message):
|
2023-01-18 14:38:51 +00:00
|
|
|
if "params" in message:
|
|
|
|
if "headers" in message["params"]:
|
|
|
|
if "set-cookie" in message["params"]["headers"]:
|
2023-01-11 15:59:46 +00:00
|
|
|
# Use regex to get the cookie for cf_clearance=*;
|
|
|
|
cf_clearance_cookie = re.search(
|
2023-01-18 14:38:51 +00:00
|
|
|
"cf_clearance=.*?;", message["params"]["headers"]["set-cookie"]
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
session_cookie = re.search(
|
2023-01-18 14:38:51 +00:00
|
|
|
"__Secure-next-auth.session-token=.*?;",
|
|
|
|
message["params"]["headers"]["set-cookie"],
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
if cf_clearance_cookie and not self.cf_cookie_found:
|
|
|
|
print("Found Cloudflare Cookie!")
|
|
|
|
# remove the semicolon and 'cf_clearance=' from the string
|
|
|
|
raw_cf_cookie = cf_clearance_cookie.group(0)
|
|
|
|
self.cf_clearance = raw_cf_cookie.split("=")[1][:-1]
|
|
|
|
if self.verbose:
|
|
|
|
print(
|
2023-01-18 14:38:51 +00:00
|
|
|
self.GREEN
|
|
|
|
+ "Cloudflare Cookie: "
|
|
|
|
+ self.ENDCOLOR
|
|
|
|
+ self.cf_clearance
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
self.cf_cookie_found = True
|
|
|
|
if session_cookie and not self.session_cookie_found:
|
|
|
|
print("Found Session Token!")
|
|
|
|
# remove the semicolon and '__Secure-next-auth.session-token=' from the string
|
|
|
|
raw_session_cookie = session_cookie.group(0)
|
2023-01-18 14:38:51 +00:00
|
|
|
self.session_token = raw_session_cookie.split("=")[1][:-1]
|
2023-01-11 15:59:46 +00:00
|
|
|
self.session.cookies.set(
|
2023-01-18 14:38:51 +00:00
|
|
|
"__Secure-next-auth.session-token", self.session_token
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
if self.verbose:
|
|
|
|
print(
|
2023-01-18 14:38:51 +00:00
|
|
|
self.GREEN
|
|
|
|
+ "Session Token: "
|
|
|
|
+ self.ENDCOLOR
|
|
|
|
+ self.session_token
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
self.session_cookie_found = True
|
|
|
|
|
|
|
|
def detect_user_agent(self, message):
|
2023-01-18 14:38:51 +00:00
|
|
|
if "params" in message:
|
|
|
|
if "headers" in message["params"]:
|
|
|
|
if "user-agent" in message["params"]["headers"]:
|
2023-01-11 15:59:46 +00:00
|
|
|
# Use regex to get the cookie for cf_clearance=*;
|
2023-01-18 14:38:51 +00:00
|
|
|
user_agent = message["params"]["headers"]["user-agent"]
|
2023-01-11 15:59:46 +00:00
|
|
|
self.user_agent = user_agent
|
|
|
|
self.agent_found = True
|
2023-01-18 14:38:51 +00:00
|
|
|
self.refresh_headers(cf_clearance=self.cf_clearance, user_agent=self.user_agent)
|
2023-01-11 15:59:46 +00:00
|
|
|
|
|
|
|
def refresh_headers(self, cf_clearance, user_agent):
|
|
|
|
del self.session.cookies["cf_clearance"]
|
|
|
|
self.session.headers.clear()
|
|
|
|
self.session.cookies.set("cf_clearance", cf_clearance)
|
2023-01-18 14:38:51 +00:00
|
|
|
self.session.headers.update(
|
|
|
|
{
|
|
|
|
"Accept": "text/event-stream",
|
|
|
|
"Authorization": "Bearer ",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"User-Agent": user_agent,
|
|
|
|
"X-Openai-Assistant-App-Id": "",
|
|
|
|
"Connection": "close",
|
|
|
|
"Accept-Language": "en-US,en;q=0.9",
|
|
|
|
"Referer": "https://chat.openai.com/chat",
|
|
|
|
}
|
|
|
|
)
|
2023-01-11 15:59:46 +00:00
|
|
|
|
|
|
|
def rollback_conversation(self, num=1) -> None:
|
|
|
|
"""
|
|
|
|
Rollback the conversation.
|
|
|
|
:param num: The number of messages to rollback
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
for i in range(num):
|
|
|
|
self.conversation_id = self.conversation_id_prev_queue.pop()
|
|
|
|
self.parent_id = self.parent_id_prev_queue.pop()
|