Skip to content

Commit a4a03a9

Browse files
committed
fix: handle the registration form better, fixes #39
1 parent 7185c49 commit a4a03a9

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
save_credentials,
2020
)
2121
from utilities.web import (
22+
finish_form,
2223
generate_mail,
2324
type_name,
2425
type_password,
@@ -154,7 +155,7 @@ async def register(credentials: Credentials, executable_path: str, config: Confi
154155
"userDataDir": f"{os.getcwd()}/tmp",
155156
"args": args,
156157
"executablePath": executable_path,
157-
"autoClose": False,
158+
"autoClose": False, # We run into runtime errors if we use autoClose
158159
"ignoreDefaultArgs": ["--enable-automation", "--disable-extensions"],
159160
}
160161
)
@@ -164,6 +165,7 @@ async def register(credentials: Credentials, executable_path: str, config: Confi
164165

165166
await type_name(page, credentials)
166167
await type_password(page, credentials)
168+
await finish_form(page, credentials)
167169
mail = await mail_login(credentials)
168170

169171
await asyncio.sleep(1.5)

utilities/etc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from utilities.types import Colours, Credentials
1212

13-
VERSION = "v1.5.1"
13+
VERSION = "v1.5.2"
1414
mega = Mega()
1515

1616

@@ -80,7 +80,7 @@ def kill_process(matches: list):
8080
if any(x in _.path for x in matches):
8181
p_print(f"Killing process {process.name()}...", Colours.WARNING)
8282
process.kill()
83-
except psutil.AccessDenied:
83+
except (psutil.AccessDenied, psutil.NoSuchProcess):
8484
continue
8585

8686
p_print("Killed previous instances successfully!", Colours.OKGREEN)

utilities/web.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
import re
55
import string
66
import random
7-
import sys
87
import pymailtm
98
from faker import Faker
109

1110
from pymailtm.pymailtm import CouldNotGetAccountException, CouldNotGetMessagesException
11+
import pyppeteer
12+
import pyppeteer.page
1213

1314
from utilities.etc import Credentials, p_print, Colours
1415

@@ -63,10 +64,6 @@ async def get_mail(mail):
6364
attempts = 0
6465

6566
while True:
66-
if attempts >= 10:
67-
p_print("Failed to find mail... exiting.", Colours.FAIL)
68-
sys.exit(1)
69-
7067
try:
7168
message = mail.get_messages()[0]
7269
p_print("Found mail!", Colours.OKGREEN)
@@ -79,41 +76,48 @@ async def get_mail(mail):
7976
await asyncio.sleep(1.5)
8077

8178

82-
async def type_name(page, credentials: Credentials):
79+
async def type_name(page: pyppeteer.page.Page, credentials: Credentials):
8380
"""Types name into the name fields."""
8481
name = str(fake.name()).split(" ", 2)
8582
firstname = name[0]
8683
lastname = name[1]
8784
await page.goto("https://mega.nz/register")
8885
await page.waitForSelector("#register_form")
89-
await page.type("#register-firstname-registerpage2", firstname)
90-
await page.type("#register-lastname-registerpage2", lastname)
91-
await page.type("#register-email-registerpage2", credentials.email)
86+
await page.evaluate(
87+
f"const textInputs = document.querySelectorAll('input[type=text]'); textInputs[2].value = '{firstname}'; textInputs[3].value = '{lastname}'; textInputs[4].value = '{credentials.email}';"
88+
)
9289

9390

94-
async def type_password(page, credentials: Credentials):
95-
"""Types passwords into the password fields."""
96-
await page.click("#register-password-registerpage2")
97-
await page.type("#register-password-registerpage2", credentials.password)
98-
await page.click("#register-password-registerpage3")
99-
await page.type("#register-password-registerpage3", credentials.password)
91+
async def finish_form(page: pyppeteer.page.Page, credentials: Credentials):
92+
"""Checks the boxes for the register page."""
10093
await page.click("#register-check-registerpage2")
94+
await page.click(".register-button")
95+
96+
97+
async def type_password(page: pyppeteer.page.Page, credentials: Credentials):
98+
"""Types passwords into the password fields."""
99+
await page.evaluate(
100+
f"const passwordInputs = document.querySelectorAll('input[type=password]'); passwordInputs[0].value = '{credentials.password}'; passwordInputs[1].value = '{credentials.password}';"
101+
)
101102
await page.querySelectorAllEval(
102103
".understand-check", "(elements) => {elements[0].click();}"
103104
)
104-
await page.click(".register-button")
105105
p_print("Registered account successfully!", Colours.OKGREEN)
106106

107107

108108
async def generate_mail() -> Credentials:
109109
"""Generate mail.tm account and return account credentials."""
110110
mail = pymailtm.MailTm()
111+
try_count = 0
112+
111113
while True:
112114
try:
113115
account = mail.get_account()
114116
break
115117
except CouldNotGetAccountException:
116118
p_print("Retrying mail.tm account generation...", Colours.WARNING)
119+
try_count += 1
120+
await asyncio.sleep(try_count)
117121

118122
credentials = Credentials()
119123
credentials.email = account.address

0 commit comments

Comments
 (0)