Last active
December 3, 2023 17:18
-
-
Save st1vms/262eeedf280528e61780d07a69dd314c to your computer and use it in GitHub Desktop.
Semi-Automatic ElevenLabs scraper to auto gather API keys.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
"""ElevenLabs Scraper module""" | |
# REQUIRES: pip install -U selgym onesecmail_api | |
# RUN with: python elevenlabs_auto_key.py | |
from re import findall | |
from time import sleep | |
from random import choice | |
from string import ascii_lowercase, ascii_uppercase, digits | |
from screeninfo import get_monitors | |
from onesecmail_api import gen_random_emails, fetch_mailbox, fetch_msg | |
from selgym import ( | |
get_firefox_options, | |
get_firefox_webdriver, | |
wait_element_by, | |
click_element, | |
cleanup_resources, | |
By, | |
) | |
def gen_creds(pass_len: int = 16) -> tuple[str, str]: | |
"""Generate random 1secmail email and password for registration""" | |
email = gen_random_emails(domain="txcct.com")[0] | |
passwd = "".join( | |
[choice(ascii_lowercase + ascii_uppercase + digits) for _ in range(pass_len)] | |
) + choice("@#_-") | |
return email, passwd | |
def get_verify_link(email: str, timeout: int = 30, retry_rate: int = 5) -> str | None: | |
"""Retrieve elevenlabs verification link from 1secmail email""" | |
pattern = r'<a\s+[^>]*href="(https://beta.elevenlabs.io\?mode=verifyEmail&oobCode=[^"]+)"[^>]*>' | |
box = [] | |
while not box: | |
timeout -= retry_rate | |
box = fetch_mailbox(email) | |
if timeout <= 0: | |
return None | |
sleep(retry_rate) | |
msg = fetch_msg(email, box[0].msg_id) | |
return findall(pattern, msg.body)[0] | |
__BASE_URL = "https://elevenlabs.io" | |
__SIGN_UP_URL = f"{__BASE_URL}/sign-up" | |
__SIGN_UP_EMAIL_INPUT_CSSS = 'input[data-testid="signup-email-input"]' | |
__SIGN_UP_PASSWD_INPUT_CSSS = 'input[data-testid="signup-password-input"]' | |
__SIGN_UP_BUTTON_CSSS = "button.btn:nth-child(1)" | |
__SIGN_IN_BUTTON_CSSS = "span.cursor-pointer > button:nth-child(1)" | |
__SIGN_IN_FORM_CSSS = "#sign-in-form" | |
__SIGN_IN_EMAIL_INPUT_CSSS = "#sign-in-form > div:nth-child(2) > input:nth-child(1)" | |
__SIGN_IN_PASSWD_INPUT_CSSS = "#sign-in-form > div:nth-child(3) > input:nth-child(1)" | |
__SEND_SIGN_IN_BUTTON_CSSS = "button.btn-lg" | |
__PROFILE_PIC_BUTTON_CSSS = 'button[data-testid="user-menu-button"]' | |
__PROFILE_BUTTON_CSSS = 'div[id^="headlessui-menu-item-:r"]:first-of-type' | |
__SHOW_KEY_BUTTON_CSSS = "button.border-l-0:nth-child(2)" | |
__API_KEY_BAR_CSSS = "input.block:nth-child(1)" | |
__MONITOR = get_monitors()[0] | |
def get_elevenlabs_key() -> str | None: | |
"""Method for getting elevenlabs key""" | |
email, passwd = gen_creds() | |
print(f"\nGenereted credentials:\n\nEmail: {email}\n\nPassword: {passwd}\n\n") | |
opts = get_firefox_options(private_mode=True) | |
driver = get_firefox_webdriver(options=opts) | |
driver.set_window_size(__MONITOR.height // 1.25, __MONITOR.width // 1.6) | |
try: | |
driver.get(__SIGN_UP_URL) | |
# TODO Implement captcha solving | |
input("Press Enter after solving captcha") | |
inp = wait_element_by(driver, By.CSS_SELECTOR, __SIGN_UP_EMAIL_INPUT_CSSS) | |
inp.send_keys(email) | |
inp = wait_element_by(driver, By.CSS_SELECTOR, __SIGN_UP_PASSWD_INPUT_CSSS) | |
inp.send_keys(passwd) | |
button = wait_element_by(driver, By.CSS_SELECTOR, __SIGN_UP_BUTTON_CSSS) | |
click_element(driver, button) | |
sleep(6) | |
finally: | |
driver.quit() | |
cleanup_resources() | |
print("\nWaiting for verification email...") | |
link = get_verify_link(email) | |
if not link: | |
print("Verification email was not received in time...\n") | |
if "y" == input("\nWant to try again? (y/N)\n>>").strip().lower(): | |
return get_elevenlabs_key() | |
return None | |
print("\nRetrieved verification link!\n") | |
print("\nPlease wait while I retrieve your API token...\n") | |
opts = get_firefox_options(headless=True, private_mode=True) | |
driver = get_firefox_webdriver(options=opts) | |
driver.maximize_window() | |
try: | |
driver.get(link) | |
driver.implicitly_wait(5) | |
sleep(5) | |
driver.get(__BASE_URL) | |
driver.implicitly_wait(5) | |
sleep(1) | |
button = wait_element_by(driver, By.CSS_SELECTOR, __SIGN_IN_BUTTON_CSSS) | |
click_element(driver, button) | |
form = wait_element_by(driver, By.CSS_SELECTOR, __SIGN_IN_FORM_CSSS) | |
inp = wait_element_by(form, By.CSS_SELECTOR, __SIGN_IN_EMAIL_INPUT_CSSS) | |
inp.send_keys(email) | |
inp = wait_element_by(form, By.CSS_SELECTOR, __SIGN_IN_PASSWD_INPUT_CSSS) | |
inp.send_keys(passwd) | |
button = wait_element_by(form, By.CSS_SELECTOR, __SEND_SIGN_IN_BUTTON_CSSS) | |
click_element(form, button) | |
driver.implicitly_wait(5) | |
sleep(2) | |
button = wait_element_by(driver, By.CSS_SELECTOR, __PROFILE_PIC_BUTTON_CSSS) | |
click_element(driver, button) | |
button = wait_element_by(driver, By.CSS_SELECTOR, __PROFILE_BUTTON_CSSS) | |
click_element(driver, button) | |
button = wait_element_by(driver, By.CSS_SELECTOR, __SHOW_KEY_BUTTON_CSSS) | |
click_element(driver, button) | |
inp = wait_element_by(driver, By.CSS_SELECTOR, __API_KEY_BAR_CSSS) | |
return str(inp.get_attribute("value")) | |
finally: | |
driver.quit() | |
cleanup_resources() | |
if __name__ == "__main__": | |
api_key = get_elevenlabs_key() | |
print(f"\nYour API Key is: {api_key}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment