-
Notifications
You must be signed in to change notification settings - Fork 6
/
chatGPT.py
63 lines (52 loc) · 2.08 KB
/
chatGPT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from playwright.sync_api import sync_playwright, Page
import os
import json
import time
'''
This class is used to interact with chatGPT on the website
'''
class chatGPT:
'''
Constructor for the chatGPT class
'''
def __init__(self, p, browser):
self.p = p
self.browser = browser
self.start_chat()
def start_chat(self):
self.chat_page: Page = self.browser.new_page()
self.first_prompt = True
# Attempt to load cookies from cookies.json into page
if os.path.exists("cookies.json"):
with open("cookies.json", "r") as f:
cookies = json.load(f)
self.chat_page.context.add_cookies(cookies['cookies'])
self.chat_page.goto("https://chat.openai.com/chat")
# If the url is not the chat page, then the user is not logged in
if self.chat_page.url == "https://chat.openai.com/auth/login":
input("Please log in to chatGPT and press enter to continue")
# Save the cookies to a file to use later
cookies = self.chat_page.context.cookies()
with open("cookies.json", "w+") as f:
# Dump json to the file
json.dump({"cookies": cookies}, f)
# Click past the introduction
for i in range(2):
# Click button that says next in innerText
self.chat_page.click("text=Next")
self.chat_page.click("text=Done")
# Wait for the chat to load
time.sleep(1)
def send_prompts(self, prompts: list):
# Send each prompt to the chatGPT chat
for prompt in prompts:
# Type into a textarea element and hit the enter key to submit
self.chat_page.fill("textarea", prompt)
# If a prompt is still processing we wait
while self.chat_page.query_selector(".text-2xl"):
pass
self.chat_page.press("textarea", "Enter")
self.first_prompt = False
def wait_for_chat_close(self):
input("Hit enter to close the browser")
self.chat_page.close()