Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 3 checks to identify login issues, updated default webdriver to use chrome 117, added requirements.txt and updated readme #35

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ Before you begin, please ensure that you have Chrome installed on your system. T

## Installation

Installing the library:
```python
pip install git+https://github.com/ugorsahin/ChatGPT_Automation
```

Installing required packages (run this command in the same folder as the library files):
```python
pip install -r requirements.txt
```

## Usage

```python
Expand Down
2 changes: 1 addition & 1 deletion chatgpt_automation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .chatgpt_client import ChatGPT_Client
from .talking_heads import TalkingHeads
from .talking_heads import TalkingHeads
47 changes: 43 additions & 4 deletions chatgpt_automation/chatgpt_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'''Class definition for ChatGPT_Client'''

import os
import logging
import time
import os, logging, time
from datetime import datetime
import undetected_chromedriver as uc

Expand Down Expand Up @@ -30,6 +28,10 @@ class ChatGPT_Client:
button_tq = 'button'
done_xq = '//button[//div[text()="Done"]]'

username_error = 'error-element-username'
password_error = 'error-element-password'
account_error = '//div[@data-error-code="user-blocked"]'

menu_xq = '//button[contains(@id, "headlessui-menu-button")]'
custom_xq = '//a[contains(text(), "Custom instructions")]'
enable_xq = '//span[@data-state="unchecked"]'
Expand Down Expand Up @@ -193,14 +195,50 @@ def login(self, username :str, password :str):
time.sleep(1)
logging.info('Clicked continue button')

#Ensure username is in the correct format
usernameIsValid = True
try:
self.browser.find_element(By.ID, self.username_error)
usernameIsValid = False
except Exceptions.NoSuchElementException:
logging.info("Username was validated")
except Exception as exp:
logging.error(f'Something unexpected happened: {exp}')
if not usernameIsValid:
raise RuntimeError("Failed to validate username, please ensure the username/email you have entered is correct")

# Find password textbox, enter password
pass_box = self.sleepy_find_element(By.ID, 'password')
pass_box.send_keys(password)
logging.info('Filled password box')
# Click continue
pass_box.send_keys(Keys.ENTER)
time.sleep(1)
logging.info('Logged in')

#Ensure password is correct
passwordIsValid = True
try:
self.browser.find_element(By.ID, self.password_error)
passwordIsValid = False
except Exceptions.NoSuchElementException:
logging.info("Password was validated")
except Exception as exp:
logging.error(f'Something unexpected happened: {exp}')
if not passwordIsValid:
raise RuntimeError("Failed to validate password, please ensure the password you have entered is correct")

#Ensure account isn't blocked
accountIsValid = True
try:
self.browser.find_element(By.XPATH, self.account_error)
accountIsValid = False
except Exceptions.NoSuchElementException:
logging.info("Account was validated, logging in")
except Exception as exp:
logging.error(f'Something unexpected happened: {exp}')
if not accountIsValid:
raise RuntimeError("Failed to validate account, it appears this account is temporarily blocked by OpenAI")


try:
# Pass introduction
Expand Down Expand Up @@ -402,3 +440,4 @@ def set_custom_instruction(self, mode: str, instruction: str):
chatgpt = ChatGPT_Client(args.username, args.password)
result = chatgpt.interact('Hello, how are you today')
print(result)

10 changes: 5 additions & 5 deletions chatgpt_automation/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def detect_chrome_version(version_num=None):
'''
Detects chrome version, only supports linux and mac machines.
If the command return something else than expected output, it uses the default version 112.
If the command return something else than expected output, it uses the default version 117.
'''

if version_num:
Expand All @@ -15,15 +15,15 @@ def detect_chrome_version(version_num=None):

if platform.system() == 'Windows':
if not version_num:
logging.warning('Windows detected, no version number is provided, default: 112')
return 112
logging.warning('Windows detected, no version number is provided, default: 117')
return 117
return version_num

out = subprocess.check_output(['google-chrome', '--version'])
out = re.search(r'Google\s+Chrome\s+(\d{3})', out.decode())
_v = 112
_v = 117
if not out:
logging.info('Could\'nt locate chrome version, using default value: 112')
logging.info('Could\'nt locate chrome version, using default value: 117')
else:
_v = int(out.group(1))
logging.info(f'The version is {_v}')
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
selenium==4.12.0
undetected_chromedriver==3.5.3