Skip to content

Instantly share code, notes, and snippets.

@boppreh
Last active July 5, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boppreh/ad7eaafc947e4727ea487605d2eab957 to your computer and use it in GitHub Desktop.
Save boppreh/ad7eaafc947e4727ea487605d2eab957 to your computer and use it in GitHub Desktop.
Visit every StackExchange site
"""
Login and visit each stackexchange site. Schedule it daily to win the Fanatic Badge after 100 days.
"""
# Create account.py module with 'email', 'password', and 'stackexchange_user_id'.
import account
import requests
import re
session = requests.Session()
# Get list of accounts on all stackexchange sites.
accounts_response = session.get(f'https://stackexchange.com/users/{account.stackexchange_user_id}?tab=accounts', allow_redirects=True)
accounts_response.raise_for_status()
user_by_domain = dict(re.findall(r'https://([\w.-]+)/users/(\d+)', accounts_response.text))
# Cookies are domain-bound, and there are a number of different top-level domains.
for domain in ['meta.stackexchange.com', 'stackoverflow.com', 'serverfault.com', 'askubuntu.com', 'stackapps.com', 'mathoverflow.net', 'superuser.com']:
print(f'Logging in to {domain} as {account.email}')
r = session.get(f'https://{domain}/users/login')
r.raise_for_status()
fkey = re.search(r'"fkey":"([^"]+)"', r.text).group(1)
payload = {
'ssrc': 'head',
'email': account.email,
'password': account.password,
'oauth_version': '',
'oauth_server': '',
'fkey': fkey,
}
r = session.post(f'https://{domain}/users/login', allow_redirects=True, data=payload, headers={'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'})
r.raise_for_status()
for domain, user in user_by_domain.items():
# Area51 is frequently bugged, skip it.
if domain == 'area51.stackexchange.com': continue
# No badges in 'stackexchange.com'.
if domain == 'stackexchange.com': continue
print(f'Visiting {domain}')
url = f'https://{domain}/users/saves/{user}'
r = session.get(url, allow_redirects=True)
print('\t', r.status_code, url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment