Skip to content

Commit

Permalink
Add a login mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dzianisv committed Jan 11, 2025
1 parent 1ec9a12 commit 65c6d62
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ bot.close()
## Env variables

- POE_HEADLESS=true
- POE_EMAIL=<your email>

- POE_COOKIES=<base64 encoded cookies> otherwise cookies are read from "~/.cache/poept.cookies.json"
## Extra

- status of client
Expand Down
63 changes: 31 additions & 32 deletions poept/poept.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import json
import base64
from urllib.parse import urlparse
import signal
import sys

class BotNotFound(Exception):
pass
Expand All @@ -32,7 +34,6 @@ class EmptyResponse(Exception):
default_bot = "Assistant"
website = "https://poe.com/"
letters = ["ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz"]
email_area='input[type="email"]'
code_area="input[class*=CodeInput"
go_key='//button[text()="Go"]'
log_key=f"//button[contains(translate(., '{letters[0]}', '{letters[-1]}' ), 'log')]"
Expand All @@ -54,11 +55,11 @@ class PoePT:

def __init__(self,
cookies: Optional[str] = os.environ.get("POE_COOKIES"),
email: Optional[str] = os.environ.get("POE_EMAIL"),
headless: bool = os.environ.get("POE_HEADLESS", "true") == "true",
login_mode: bool = False
):
chrome_options = Options()
if headless:
if headless or login_mode:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-gpu")
Expand All @@ -75,6 +76,16 @@ def __init__(self,
self.driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
self.stat = "false"

if login_mode:
logger.info("Login mode enabled. Waiting for login completion...")
self.driver.get(website)
WebDriverWait(self.driver, 120).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "textarea[class*='GrowingTextArea_textArea']"))
)
logger.info("Login detected. Saving cookies...")
self.save_cookies_on_exit()
return

self.cookies = None

if cookies is not None:
Expand All @@ -84,19 +95,11 @@ def __init__(self,
if self.cookies is None:
self.cookies = self.read_cookies()

if self.cookies is None:
if email is None:
raise AuthenticationFailure("no cookies and email are set")
else:
self.login(email)
for _ in range(3):
if self.apply_cookies():
break
else:
# i noticed that sometimes cookies are not applied from first run :facepalm:
# workaround
for _ in range(3):
if self.apply_cookies():
break
else:
raise AuthenticationFailure("cookies wasn't applied after a few attempts")
raise AuthenticationFailure("cookies wasn't applied after a few attempts")

def read_cookies(self):
for cookies_file in [self.cookies_file_path, self.alternative_cookies_file_path]:
Expand Down Expand Up @@ -205,22 +208,6 @@ def get_message(self):
def clearchat(self):
click(self.driver, By.CSS_SELECTOR, clear_key)

def login(self, email: str):
self.driver.get(website)
self.driver.execute_script('window.scrollBy(0, 5);')

enter(self.driver, By.CSS_SELECTOR, email_area, email)
click(self.driver, By.XPATH, go_key)

code = input("Enter code: ")
enter(self.driver, By.CSS_SELECTOR, code_area, code)
click(self.driver, By.XPATH, log_key)

self.cookies = self.driver.get_cookies()
with open(self.cookies_file_path, "wb") as f:
json.dump(self.cookies, f)


def _typein(self, element, text):
js_code = """
const element = arguments[0];
Expand Down Expand Up @@ -328,4 +315,16 @@ def get_chat_id(self) -> Optional[str]:
chat_part = parsed_url.path.split('chat/')[1]
return f'chat/{chat_part}'
else:
return None
return None

def save_cookies_on_exit(self):
logger.info("Saving cookies...")
cookies = self.driver.get_cookies()
with open(self.cookies_file_path, 'w') as f:
json.dump(cookies, f)
print(json.dumps(cookies))
self.close()
sys.exit(0)

if __name__ == "__main__":
PoePT(login_mode=True)

0 comments on commit 65c6d62

Please sign in to comment.