Skip to content

Instantly share code, notes, and snippets.

@bostrot
Last active September 24, 2021 12:00
Show Gist options
  • Save bostrot/af2499f9418cf2e900082f329dfb30c0 to your computer and use it in GitHub Desktop.
Save bostrot/af2499f9418cf2e900082f329dfb30c0 to your computer and use it in GitHub Desktop.
Stackoverflow Fanatics badge automator with daily Telegram notifications
# Run this daily with cron
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import telegram_send
import time
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=chrome_options)
# go to qis site
def openSite():
driver.get('https://stackoverflow.com/users/login')
# authenticate
def login():
driver.find_element_by_id('email').send_keys('USERNAME')
driver.find_element_by_id('password').send_keys('PASSWORD')
driver.find_element_by_id('submit-button').click()
time.sleep(3)
# navigate to profile
def navToProfile():
driver.find_element_by_css_selector('.my-profile').click()
def sendMsg(msg):
try:
telegram_send.send(messages=[msg])
except Exception as e:
print('Unable to send', e)
def getBadgeProgress():
try:
badge = driver.find_element_by_xpath('//*[contains(text(), \'Next badge\')]')
badge = badge.find_element_by_xpath('..') # parent
badge = badge.find_element_by_css_selector('span')
badgeName = driver.find_element_by_css_selector('.s-badge--label')
sendMsg('Current progress: ' + badge.text + ' on ' + badgeName.text + ' Badge')
except Exception as e:
print('Error:' + e)
sendMsg('Error: ' + e)
# check for specific modules
def checkLoggedIn():
try:
navToProfile()
except Exception as e:
print('Not logged in.')
login()
# save screenshot
def createScreenshot():
driver.get_screenshot_as_file('screen.png')
# accept cookie
def acceptCookie():
driver.find_element_by_class_name('js-accept-cookies').click()
time.sleep(3)
# main
try:
openSite()
acceptCookie()
checkLoggedIn()
createScreenshot()
navToProfile()
getBadgeProgress()
driver.quit()
except Exception as e:
sendMsg('Error: ' + e)
finally:
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment