Skip to content

Instantly share code, notes, and snippets.

@dipu-bd
Last active November 28, 2018 11:05
Show Gist options
  • Save dipu-bd/555183e2362a82ef534c343ffdc56f94 to your computer and use it in GitHub Desktop.
Save dipu-bd/555183e2362a82ef534c343ffdc56f94 to your computer and use it in GitHub Desktop.
Visit stackoverflow everyday to get the Fanatic badge
import time
from os import path
from datetime import datetime
from splinter import Browser
email = "<EMAIL>"
password = "<PASSWORD>"
user_name = "<USER_NAME>"
dir_path = path.dirname(path.realpath(__file__))
driver_path = path.join(dir_path, 'lib', 'chromedriver')
def run_task():
browser = Browser(
'chrome',
headless=True,
incognito=True,
executable_path=driver_path
)
login = "https://stackoverflow.com/users/login"
logout = "https://stackoverflow.com/users/logout"
profile = "https://stackoverflow.com/users/1583052/" + user_name
print(datetime.now(), 'Visiting login...')
browser.visit(login)
browser.fill('email', email')
browser.fill('password', password)
time.sleep(1)
script = '$("#submit-button").click();'
browser.execute_script(script)
print(datetime.now(), 'Awaiting login...')
seconds = 0
while seconds < 60 and len(browser.find_by_css('.my-profile')) == 0:
time.sleep(1)
seconds += 1
time.sleep(1)
print(datetime.now(), 'Visiting profile...')
browser.visit(profile)
elem = browser.find_by_css(
'#top-cards .js-highlight-box-badges span.grid--cell.fs-caption')
if len(elem) > 0:
elem = elem.first
print(datetime.now(), elem.text)
else:
print(datetime.now(), '<!> Badge count not present.')
print(datetime.now(), 'Wait a little...')
time.sleep(20)
print(datetime.now(), 'Visiting logout...')
browser.visit(logout)
time.sleep(1)
script = '$("#logout-form #controls input")[0].click();'
browser.execute_script(script)
print(datetime.now(), 'Awaiting logout...')
seconds = 0
while seconds < 60 and len(browser.find_by_css('.login-link')) == 0:
time.sleep(1)
seconds += 1
time.sleep(1)
print(datetime.now(), 'Exit')
browser.quit()
if __name__ == '__main__':
print('$ python3 stackoverflow.py')
print('-----------------------------------')
run_task()
print('-----------------------------------')
print('')
@dipu-bd
Copy link
Author

dipu-bd commented Nov 28, 2018

Setup it as a cron in a server:

  • First run in terminal:
$ crontab -e
  • Then add the following line in the file:
0 7 * * * python3 /home/dipu/stackoverflow.py >> /home/dipu/run.txt 2>&1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment