Earn the Fanatic Badge on Stack Overflow

A step-by-step guide to schedule automatic logins with Heroku

Alexandru Somai
codersDoRead 👨🏻‍💻📚
6 min readFeb 4, 2018

--

In the following post I’ll show you how to deploy a Python script to Heroku that performs automatic logins on stackoverflow.com on a regular basis, so that you can earn the Fanatic badge, effortlessly.

If you’re an avid programmer, and a stack overflow user, as well, most probably you know about the Fanatic badge. To earn it, you must:

Visit the site each day for 100 consecutive days. (Days are counted in UTC.)

Have you ever struggled to earn it? Did you had a login streak of 67 days, and on the 68th you went on a trip and forgot to login? Then you had to start all over again.

Instead of this, let’s do what every lazy programmers does. Automate. Automate everything.

We can achieve this in three easy steps:

  1. Write the script that logs into stack overflow
  2. Schedule the script
  3. Deploy everything to Heroku

“But wait, I don’t want to follow your boring steps, just show me the code!”

Sure, the complete source code is available here.

If you still want to go through the steps, let’s kick off!

Step 1. Write the script

The easy part — writing the script that logs into stack overflow.

Before writing and running the script, we need some prerequisites:

  • Python 3.6+ (along with pip) installed
  • a browser of choice installed (we’ll use Chrome for this example)
  • the selenium driver for the used browser (in this case, ChromeDriver). You can download the latest from here https://sites.google.com/a/chromium.org/chromedriver/downloads. After you unzip it, for convenience, on unix, you should place the chromedriver file under /usr/local/bin/
  • and because we’ll use Selenium, install the Python dependencies: pip install selenium

Firstly, we’ll create a project directory called so-fanatic-badge-your-name (for later on, when we deploy it to Heroku, I suggest you to have a unique name for the directory).

In our project directory, let’s create a file called stack_overflow_page.py in which we’ll define a method for logging on stack overflow.

Logs into stackoverflow using selenium

The above code should be quite easy to follow. After it instantiates the driver (line 9), it accesses the stackoverflow.com page, follows the ‘Log in’ link, fills in the email and password that are taken from environment variables and submits the form. After successfully logging in, it goes to ‘My Profile’ page and checks if the display name matches with the given environment variable.

Before running the script, we firstly need to set our local environment variables. Will be useful later, when we run the script on the Heroku platform.

$ export STACK_OVERFLOW_EMAIL='your_stackoverflow_email'
$ export STACK_OVERFLOW_PASSWORD='your_stackoverflow_password'
$ export STACK_OVERFLOW_DISPLAY_NAME='Your Display Name'

In the same terminal window, run the script:

$ python3 stack_overflow_page.py

You should see the Chrome browser opening and performing the login action.

Step 2. Schedule the script

Scheduling the easy part - run the script on a regular basis.

After we’ve managed to login into stack overflow page using the script from step one, we’ll schedule it to run on a regular basis.

For scheduling it, we’re going to use a lightweight library, APScheduler. Firstly, install it with pip:

$ pip install apscheduler

In our project that we’ve created at step one, let’s create a new file, clock.py, in which we define our schedule:

A basic scheduler that performs the login action on a regular basis

We’ve configured the APScheduler to invoke the login method defined in our stack_overflow_page.py file every hour. The scheduling interval (line 8) may be customized, as shown in the APScheduler documentation, but for simplicity (and because selenium may fail from time to time) we’ll run it hourly.

To run the scheduler locally, simply run:

$ python3 clock.py

In our example, the scheduler will access, hourly, the stack overflow site.

Step 3. Deploy to Heroku

The tricky part — deploying everything to Heroku.

For the final step, we need a few more dependencies installed:

After you’ve installed the Heroku CLI, perform the login heroku login and provide your Heroku credentials.

Into our project directory created at step one, that is so-fanatic-badge-your-name, we have to initialize an empty git repository and to create the heroku application, with the built-in buildpack, python.

$ git init 
$ heroku create so-fanatic-badge-your-name --buildpack heroku/python

To be able to run the application on the Heroku stack, we have to install Chrome and chromedriver, as well. For this, we’ll define two buildpacks within our application:

$ heroku buildpacks:add https://github.com/heroku/heroku-buildpack-google-chrome
$ heroku buildpacks:add https://github.com/heroku/heroku-buildpack-chromedriver.git

We also have to make a minor change to the webdriver initialization. In stack_overflow_page.py file, when we initialize the webdriver, we have to install the chrome driver manager. You might also need to install the webdriver_manager dependency:

$ pip install webdriver_manager

The changed lines of code are like this:

Install chrome driver manager when instantiating webdriver

Note: At the time of writing, the applications were created using heroku-16 stack. For different heroku stacks, the buildpacks and configs may be slightly different.

If you remember from the first step, before actually running the login script, we had to define locally a few environment variables. We have to define them also in the Heroku environment:

$ heroku config:set STACK_OVERFLOW_EMAIL='your_stackoverflow_email'
$ heroku config:set STACK_OVERFLOW_PASSWORD='your_stackoverflow_pass'
$ heroku config:set STACK_OVERFLOW_DISPLAY_NAME='Your Display Name'

We’re close to deploying our application to Heroku, a couple more files to be added and we’re done.

We have to create a new file called requirements.txt. This file will be used by Heroku to install our python dependencies. The file contains the following lines:

APScheduler==3.6.3
selenium==3.8.1
webdriver_manager==3.1.0

Last, but not least, we must define the Procfile(the name must be exactly like this). Procfile it’s a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform:

clock: python3 clock.py

In the Procfile, we’ve defined a process called clock, that will run our scheduler.

The final touch consists in pushing our application code to Heroku repository:

$ git add .
$ git commit -m "Deploy to Heroku"
$ git push heroku master

You should see many console output logs until the required dependencies are installed on the Heroku platform. After completion, we need to scale our dyno formation:

$ heroku ps:scale clock=1

You can view the Heroku logs by simply typing heroku logs --tail. They should print that you’ve successfully logged into stack overflow site.

And that’s it! After 100 days, you’ll earn the Fanatic badge without doing anything else.

I hope that this blog post has helped you to easily achieve the Fanatic badge on stack overflow.

I’m planning to write a second part, in which we’ll use the Stack Exchange API to retrieve the last time we’ve accessed the stack overflow site. If the date is below a specific threshold (for example twelve hours), we’ll be notified via email, therefore we know that something went wrong with the login script.

Update: I’ve written a second part, in which we are notified by email in case we don’t login into Stack Overflow for more than twelve hours.

If you’ve found this guide useful, or if you have any questions or suggestions, let me know in the comments below. Any improvements to the source code are welcomed here.

--

--