This commit is contained in:
mwiegand 2022-04-10 20:50:18 +02:00
parent a7b24d4df1
commit 9107e00bb2

46
app.py
View file

@ -12,34 +12,47 @@ from time import sleep
# get previously known message ids from imap server # get previously known message ids from imap server
M = imaplib.IMAP4_SSL(environ['IMAP_HOST']) print('loggin into IMAP Server:', environ['IMAP_HOST'])
M.login(environ['IMAP_USER'], environ['IMAP_PASSWORD'])
M.select() Mailbox = imaplib.IMAP4_SSL(environ['IMAP_HOST'])
typ, data = M.search(None, '(FROM "noreply@steampowered.com" SUBJECT "Your Steam account: Access from new web or mobile device")') Mailbox.login(environ['IMAP_USER'], environ['IMAP_PASSWORD'])
Mailbox.select()
typ, data = Mailbox.search(None, '(FROM "noreply@steampowered.com" SUBJECT "Your Steam account: Access from new web or mobile device")')
old_msg_ids = data[0].split() old_msg_ids = data[0].split()
# perform steam login # perform steam login
print('loggin into STEAM:', environ['STEAM_USERNAME'])
user = wa.WebAuth(environ['STEAM_USERNAME']) user = wa.WebAuth(environ['STEAM_USERNAME'])
try: try:
user.login(environ['STEAM_PASSWORD']) user.login(environ['STEAM_PASSWORD'])
except (wa.LoginIncorrect) as exp: except (wa.LoginIncorrect) as exp:
print('ERROR: loggin incorrect')
raise raise
except (wa.CaptchaRequired) as exp: except (wa.CaptchaRequired) as exp:
print(user.captcha_url) print('ERROR: captcha required:', user.captcha_url)
user.login(password=environ['STEAM_PASSWORD'], captcha=input("Captcha: ")) #user.login(password=environ['STEAM_PASSWORD'], captcha=input("Captcha: "))
raise
except wa.TwoFactorCodeRequired:
print('ERROR: 2FA code required')
#user.login(twofactor_code=input("2FA Code: "))
raise
except wa.EmailCodeRequired: except wa.EmailCodeRequired:
print('getting email code')
while True: while True:
typ, data = M.search(None, '(FROM "noreply@steampowered.com" SUBJECT "Your Steam account: Access from new web or mobile device")') typ, data = Mailbox.search(None, '(FROM "noreply@steampowered.com" SUBJECT "Your Steam account: Access from new web or mobile device")')
newest_msg_id = data[0].split()[-1] newest_msg_id = data[0].split()[-1]
if newest_msg_id in old_msg_ids: if newest_msg_id in old_msg_ids:
print('refreshing messages')
sleep(1) sleep(1)
else: else:
print('messages received')
break break
typ, data = M.fetch(newest_msg_id, '(RFC822)') typ, data = Mailbox.fetch(newest_msg_id, '(RFC822)')
msg = email.message_from_bytes(data[0][1]) msg = email.message_from_bytes(data[0][1])
for payload in msg.get_payload(): for payload in msg.get_payload():
@ -51,9 +64,8 @@ except wa.EmailCodeRequired:
code = plaintext_lines[plaintext_lines.index('Login Code') + 1] code = plaintext_lines[plaintext_lines.index('Login Code') + 1]
break break
print('code found:', code)
user.login(email_code=code) user.login(email_code=code)
except wa.TwoFactorCodeRequired:
user.login(twofactor_code=input("2FA Code: "))
# CRAWL # CRAWL
@ -76,6 +88,7 @@ def parse_trs(trs):
date = datetime.strptime(tds[2].text, '%b %d, %Y @ %I:%M%p PDT').replace(tzinfo=pdt) date = datetime.strptime(tds[2].text, '%b %d, %Y @ %I:%M%p PDT').replace(tzinfo=pdt)
text = tds[3].text text = tds[3].text
print(f'({date}) {from_name} -> {to_name}: {text}') print(f'({date}) {from_name} -> {to_name}: {text}')
# download steamuserimages # download steamuserimages
for url_string in re.findall(r'(https?://\S+)', text): for url_string in re.findall(r'(https?://\S+)', text):
url = urlparse(url_string) url = urlparse(url_string)
@ -86,12 +99,10 @@ def parse_trs(trs):
# get first page # get first page
r = user.session.get('https://help.steampowered.com/en/accountdata/GetFriendMessagesLog') print('parsing friend messages log')
response = user.session.get('https://help.steampowered.com/en/accountdata/GetFriendMessagesLog')
soup = BeautifulSoup(r.text, 'html.parser') soup = BeautifulSoup(response.text, 'html.parser')
continue_value = soup.find(class_='AccountDataLoadMore')['data-continuevalue'] continue_value = soup.find(class_='AccountDataLoadMore')['data-continuevalue']
account_data_table = soup.find(id='AccountDataTable_1') account_data_table = soup.find(id='AccountDataTable_1')
trs = account_data_table.find_all('tr')[1:] trs = account_data_table.find_all('tr')[1:]
@ -100,6 +111,7 @@ parse_trs(trs)
# get further pages # get further pages
while True: while True:
print('getting next page')
r = user.session.get(f'https://help.steampowered.com/en/accountdata/AjaxLoadMoreData/?url=GetFriendMessagesLog&continue={continue_value}') r = user.session.get(f'https://help.steampowered.com/en/accountdata/AjaxLoadMoreData/?url=GetFriendMessagesLog&continue={continue_value}')
continue_value = r.json()['continue'] continue_value = r.json()['continue']
html = r.json()['html'] html = r.json()['html']
@ -108,5 +120,5 @@ while True:
# CLOSE # CLOSE
M.close() Mailbox.close()
M.logout() Mailbox.logout()