Last active
April 22, 2023 11:40
-
-
Save vincentclaes/48aeb663e5b69a5c61ff252289c67cd6 to your computer and use it in GitHub Desktop.
Schedule an AWS lambda with python 3.8 to run each day and logs in to stackoverflow. Put your email ("EMAIL") and password ("PASS") in the environment variables. lf you visit https://stackoverflow.com/ for 30 consecutive days, you can earn Enthusiast badge. And 100 days, Fanatic badge.
This file contains 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
import os | |
import re | |
import datetime | |
import json | |
import urllib3 | |
from urllib.parse import urlencode | |
def lambda_handler(event, context): | |
stack_overflow = StackOverflow() | |
stack_overflow.login() | |
return { | |
'statusCode': 200, | |
'body': json.dumps('yeeey!') | |
} | |
http = urllib3.PoolManager() | |
class StackOverflow: | |
base_url = 'https://stackoverflow.com/users/login' | |
fkey = '' | |
headers = { | |
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36", | |
} | |
params = { | |
"ssrc": "head", | |
"returnurl": "https://stackoverflow.com/", | |
} | |
def login(self): | |
print('Start login ...') | |
account = Account() | |
print('Email : {}\nPassword : {}'.format(account.email, "*********")) | |
payload = self.get_payload(account) | |
encoded_data = urlencode(self.params) | |
url = self.base_url + f"?{encoded_data}" | |
response = http.request('POST', url, fields=payload, headers=self.headers) | |
if response.status == 200: | |
print("Logged in:{} with fkey: {}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M"), | |
payload['fkey'])) | |
else: | |
print("Please check your email and password.") | |
def get_fkey(self): | |
print('Retrieving fkey information...') | |
response = http.request('GET', self.base_url, fields=self.params, headers=self.headers) | |
print(response.data) | |
return re.search(r'"fkey":"([^"]+)"', str(response.data)).group(1) | |
def get_payload(self, account): | |
self.fkey = self.get_fkey() | |
return { | |
'openid_identifier': '', | |
'password': account.password, | |
'fkey': self.fkey, | |
'email': account.email, | |
'oauth_server': '', | |
'oauth_version': '', | |
'openid_username': '', | |
'ssrc': 'head', | |
} | |
def get_profile_url(self, session): | |
response = session.get("https://stackoverflow.com/") | |
html = response.text | |
return "https://stackoverflow.com" + re.search(r'<a href="(.+)" class="my-profile', html).group(1) | |
class Account: | |
email = '' | |
password = '' | |
def __init__(self): | |
self.email = os.environ['EMAIL'] | |
self.password = os.environ['PASS'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment