Using Python To Get An Early Covid Vaccine

Using Python To Get An Early Covid Vaccine
This post contains affiliate links, which means I earn money from purchases. This never affects the price that you pay but helps with costs to keep the site up and running.

Using Python To Get A Covid Vaccine Early

Based on the title of my article, you might think I am a scumbag. Give me a chance to explain first. Then you can call me whatever you’d like.

I didn’t steal a vaccine from your grandma. No one is going to die because of what I did. You weren’t “entitled” to the vaccine I received. Calm down!

Phoenix is doing their Covid vaccine rollout based mostly on age. I’m not a healthcare worker or first responder so I will need to wait until they allow the 35+ age group.

Recently I found out that they need non-clinical volunteers at the 2 big vaccine sites in the Phoenix area. This means you do things like help people check-in and other tasks that dont require being a medical professional. You aren’t actually giving people the vaccine.

Here is the part of the vaccine volunteer website that really stands out.

Volunteers are able to receive the vaccine the same day, after completing one shift. Upon receiving your vaccine, you will be given a card and will schedule the date and time of your second dose before you leave the site. The vaccine type being dispensed at Phoenix Municipal Stadium is Pfizer.

Awesome! Sign me up.

It turns out that this opportunity is pretty popular. When a volunteer opportunity becomes available, it’s almost instantly taken by someone else in Phoenix.

I’m sure there are hundreds or thousands of people just sitting there refreshing the page over and over until one of these opportunities pops up. I tried that technique for about 3 minutes before I got bored.

Then I thought to myself, “I bet this is how cavemen used to get their Covid vaccine volunteer opportunities…”

I can do better than this.


How I Programmed A Bot To Get Me A Covid Vaccine

I’ll be posting code snippets throughout this post. There will be a link to all of the code at the end if you’d like to use it.

The first thing I needed to do is figure out how the site worked.

As I said before, there are two different vaccination venues that offer these volunteer opportunities. Here are their respective websites.

Phoenix Municipal Stadium Covid Vaccine Site

State Farm Stadium Covid Vaccine Site

If you go to either of those links and scroll down, you’ll almost certainly be greeted with a yellow banner that says The Opportunity date and time you are Looking for is no longer available.

The Opportunity date and time you are Looking for is no longer available

This part is key. That exact line is how I programmatically distinguished between an opening vs no openings. That banner is absent when the volunteer opportunities show up.

I created a Python dictionary with both sites and their URLs

vaccine_sites = {'Phoenix Municipal Stadium': 'https://www.handsonphoenix.org/opportunity/a0N1J00000QGgU1UAL',
                 'State Farm Stadium': 'https://www.handsonphoenix.org/opportunity/a0N1J00000NW4CHUA1'}

Next I needed a way to pull the site down. The Python requests library is perfect for that.

The following function will take whatever URL you give it and grab the entire site.

def get_site(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'}
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, "lxml")
    return soup

You’ll notice a line in that function that contains BeautifulSoup. This is just another Python library I used. My use case for this library is taking the site I just pulled down and converting it to a more searchable format.

Now that I had the site contents, I need to find the The Opportunity date and time you are Looking for is no longer available message from earlier.

Take a look at this code below and then I’ll explain what each line is doing.

while True:
    for site, site_url in vaccine_sites.items():
        vaccine_site_info = get_site(site_url)
        if str(vaccine_site_info).find("no longer available") == -1:
            new_message = 'There is an opening at {0}. Check {1} for more info.'.format(site, site_url)
    time.sleep(30)

while True:

Run the following block of code indefinitely. Unless of course, I tell it to stop doing that.


for site, site_url in vaccine_sites.items():

Look at the items in the vaccine_sites dictionary we created earlier and loop through them from beginning to end.


vaccine_site_info = get_site(site_url)

Use the get_site() function we created to get the entire webpage for the current site in the dictionary. We are storing the entire contents of the webpage in a variable named vaccine_site_info.


if str(vaccine_site_info).find("no longer available") == -1:

Check if the contents of the vaccine_site_info contains less than 1 occurrence of no longer available. Meaning no longer available isn’t found anywhere on the page.


new_message = 'There is an opening at {0}. Check {1} for more info.'.format(site, site_url)

Create a new message with the name of the vaccine site and the URL to the sign up page.


time.sleep(30)

Wait 30 seconds before checking the sites again. This is the polite thing to do so we aren’t hammering their site. It could potentially also help us avoid getting our IP address blocked/banned by the site (if it has those capabilities).


That chunk of code does the majority of the work for us. Now we just need a way to send these messages if we find an opening.

I created an account at Twilio. Twilio is a popular service for programmatically sending text messages or making phone calls.

www.twilio.com/referral/X8ZW9k

This referral link will give you (and me) $10 free to play with if you are interested in attempting a similar project. There is no credit card needed for this free $10.

Twilio outbound SMS messages are $0.0075 (less than 1 cent) each. That’s perfect for our usage.

Now we need to write some code to send an SMS.

# twilio account info
account_sid = '' # provided in twilio dashboard
auth_token = '' # provided in twilio dashboard
target_number = '+15555555555' # your cell phone
source_number = '+15555555555' # your twilio number

def send_twilio_message(body):
    twilioCli = Client(account_sid, auth_token)
    message = twilioCli.messages.create(body=body, from_=source_number, to=target_number)

First we add our Twilio credentials and phone numbers. Then we build a function that accepts a message. Specifically the message that we want to alert us of vaccine volunteer opportunities.

Those code snippets are basically all you need to get started coding with Python.


Run The CovidBot

Shortly after launching the application I started receiving text messages on my phone.

SMS notification

Even though I automated the finding of the opportunity, I still needed to click the link and then the “Sign Up” button.

Successfully booked opportunity

Holy shit, it worked! It took a few notifications but I got an appointment.

I was scheduled to volunteer and after the shift, I would get my first dose of the vaccine.


Getting My Friends Vaccinated Too

What’s the point of being vaccinated if your friends aren’t vaccinated?

I was able to use this same technique to get my wife a vaccine but I had a few friends that wanted to volunteer too.

I could just change the target phone number until each one of my friends gets a volunteer opportunity but that is annoying.

We quickly created a Slack account and I added a new function to send Slack messages.

def send_slack_message(slack_url, message):
    slack = Slack(url=slack_url)
    slack.post(text=message)

3-5 friends gathered in the Slack channel. The bot started finding openings and sending them notifications on their phones with links.

It only took about an hour before all of them had secured a volunteer opportunity. It was so much easier than pressing refresh (over and over and over) in a browser window.

Slack notifications for volunteer opportunities

Summary

The point of this post isn’t to brag about getting a Covid vaccine early. The purpose of this post is to show others how useful it is to learn coding.

I’m not a “developer” by trade but I code almost everyday at work. Coding is so useful for automating tedious tasks that don’t require much thinking. Once you get the hang of it, you can use it for tasks that do require thinking!

I enjoy coding in Python because the language is meant to be very human readable. It was easy to pick up the basics because they made so much more sense to me than other languages.

If you want to try coding, I HIGHLY recommend the book Automate the Boring Stuff with Python.

It is not a “technical book”. It gives you real examples of real scenarios where you can use Python to automate parts of your job and life. This book is amazing for beginners.


Covid Vaccine Bot Code

As promised, the full code with everything cleaned up and commented can be found here on GitHub. If you’d like to use it, feel free!

The full code contains everything you need to send Twilio or Slack messages. Just comment out whichever you don’t use.

In the current state (after adding Twilio or Slack credentials), you should be able to deploy this to Heroku and run it for completely free.

I have a friend that Dockerized the entire thing with Tor Privoxy and some other bells and whistles for running the bot locally. You can check it out at https://charlesmknox.com/tech/software/general/covid-vaccine-bot/. He has a lot of other cool stuff there too.


You can get ahold of me @_tynick on Twitter.