DEV Community

loading...
Cover image for Automate Cowin Vaccine slots Availablity using Python

Automate Cowin Vaccine slots Availablity using Python

Ayushi Rawat
A Software Developer and a YouTuber who likes to write Technical Blogs and contribute to Open Source.
Originally published at ayushirawat.com Updated on ・12 min read

Hello Reader!

I hope you all are safe and sound and at home. Do not step out of your house unless it’s absolutely necessary with proper precautions.

Coronavirus cases are increasing day by day. The second wave has hit very badly in many areas. It’s very important to get vaccinated. But, getting a slot seems next to impossible. Even I am not able to get my hands on one. So I was trying to think of a better idea possible and if you know me, you must know by now that I am a python freak, so I tried to automate the availability of the slot.

So in this blog post, I am going to tell you how you can automate the vaccine slots availability and get notified when a slot is available. We will see the implementation in python.

For your ease, you can use the code and it's available at my GitHub repository. Spread this article as much as possible so that everyone can get notified about the availability of slots and we all can get vaccinated.

Check out the Repository for Ultimate Resource in python. Drop a star if you find it useful! Got anything to add? Open a PR on the same!

You can refer to my YouTube video Tutorial to see a working tutorial for better understanding and a step-by-step guide of the same.

What will be covered in this Blog

1. What is Web Scraping?
2. The Web-Scaping Process and its Uses.
3. What is Coronavirus and CoWin?
4. How to Automate vaccine slots availability
Enter fullscreen mode Exit fullscreen mode

Let's get started!

What is Web Scrapping?:

Introduction

If you’ve ever copy and pasted information from a website, you’ve performed the same function as any web scraper, only on a microscopic, manual scale.

image.png
Web scraping, also known as web data extraction, is the process of retrieving or “scraping” data from a website. This information is collected and then exported into a format that is more useful for the user. Be it a spreadsheet or an API.

Two important points to be taken into consideration here:

  1. Always be respectful and try to get permission to scrape, do not bombard a website with scraping requests, otherwise, your IP address may get blocked!
  2. Be aware that websites change often, meaning your code could go from working to totally broken from one day to the next.

The process: three simple steps

  1. Request for a response from the webpage
  2. Parse and extract with the help of Beautiful soup and lxml
  3. Download and export the data with pandas into excel

image.png

Its Uses:

It can serve several purposes, the most popular ones are Investment Decision Making, Competitor Monitoring, News Monitoring, Market Trend Analysis, Appraising Property Value, Estimating Rental Yields, Politics and Campaigns, and many more.

If you wish to know about it further. I am attaching the Wikipedia link here. You can have a look.

What is Coronavirus?

I do not think Coronavirus needs an introduction, but just in case if someone does not know, Coronavirus disease (COVID-19) is an infectious disease caused by a newly discovered coronavirus.

The COVID-19 virus spreads primarily through droplets of saliva or discharge from the nose when an infected person coughs or sneezes

If you wish to know about it further. I am attaching the Wikipedia link here. You can have a look.

What is CoWin?

Under the universal immunization program, for several years, India has been using a vaccine intelligence system called eVIN (electronic vaccine intelligence network), CoWIN is an extension of eVIN. CoWIN stands for Covid Vaccine Intelligence Work.

it is the gateway to vaccination and the backbone of the vaccination drive.

The data source

We will make use of the cowin official website.
image.png

Module Used:

requests Module:

  • Use the requests library to grab the page.
  • This may fail if you have a firewall blocking Python.
  • Sometimes you need to run this twice if it fails the first time.

Now that you are familiar with Coronavirus and CoWin and have acquired basic knowledge of Web Scrapping and requests module, we can move forward to the coding section.

Time to Code!

You can find all the code at my GitHub Repository. Drop a star if you find it useful.

carbon.png

Let's understand this!

In order to access the Python library, you need to install it into your Python environment

pip install requests
Enter fullscreen mode Exit fullscreen mode

Now, we need to import the package into our python script. Use the following command to do so.

import requests

from datetime import datetime, timedelta
import time

import json
Enter fullscreen mode Exit fullscreen mode

Now that we have imported the libraries, let's proceed.

Let's display a welcome message. we will make use of print method for the same.

print("Starting search for Covid vaccine slots!")
Enter fullscreen mode Exit fullscreen mode

Now, let's set some basic parameters,

age = 52
pinCodes = ["462003"]
print_flag = 'Y'
num_days = 2
Enter fullscreen mode Exit fullscreen mode
  • The Slots are available in two categories, Age 45+ and Age 18+. Let's proceed with Age 45+ for now, you can set it according to your need.
  • You can perform the search based on Pincode or District. Since we are looking for the slots available nearby, I am making use of Pin-code. You can also pass multiple pin-codes, separated by commans in the list.
  • We only want to check for slots availability for the next two days for let's givenum_daysas 2 here.
  • Lastly, set the flag value to y.

Now let's make use of datetime method to calculate today's date.

actual = datetime.today()

#output:
2021-05-13 13:35:24.396612
Enter fullscreen mode Exit fullscreen mode

Now, run a loop to convert it into list format. We will make use of timedelta method to convert it into list format

list_format = [actual + timedelta(days=i) for i in range(num_days)]

#Output:
[datetime.datetime(2021, 5, 13, 13, 39, 18, 496330), datetime.datetime(2021, 5, 14, 13, 39, 18, 496330)]
Enter fullscreen mode Exit fullscreen mode

we will again run a loop to fetch the dates from the list and we will make use of strftime method to do so. Note that we are storing date in date-month-year format.

actual_dates = [i.strftime("%d-%m-%Y") for i in list_format]

#Output:
['13-05-2021', '14-05-2021']
Enter fullscreen mode Exit fullscreen mode

Next, we will run a while loop to fetch the details of the slots. Initially let's set the counter to zero.

Let's introduce two more loops here:

  • one in order to fetch details for each pin-code.
  • second in order to fetch details for each date in the given pin-code.

Now in order to get requests, let's define the URL.

URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode={}&date={}".format(pinCode, given_date)
Enter fullscreen mode Exit fullscreen mode

If you observe, I have added parameters in the URL itself using string formatting. We are passing in two parameters here, pincode and date. Every time the inner loop runs, this URL will be called and the respective date and pin-code will be passed as arguments for each case.

Now, in order to get the request, let's define the header.

header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}      
Enter fullscreen mode Exit fullscreen mode

Once done, let's call requests and we will make use of get method.

result = requests.get( URL, headers=header)
Enter fullscreen mode Exit fullscreen mode

Now, our response is stored in the result. Let's try printing it out. We will make use of text method for the same.

print(result.text)

#OUTPUT:

{"centers":[{"center_id":691508,"name":"Navin Girls HSS Tulsi Nagar 18","address":"1250 Tulsi Nagar HOSPITAL","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"b63ce111-23dd-4d4e-9f1c-c97073c57a52","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":570554,"name":"SAHTI Vaishali Kotra 
KNK","address":"Govt. Higher Sec. Kamla Nehru Girls School Teenshed TT Nagar","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"93144388-6dca-4f7c-9aba-ecd5b120fa94","date":"13-05-2021","available_capacity":215,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":592476,"name":"25 Battalion Campus BhadBhada","address":"25th Battalion Bhadbhada Bhopal","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"b857cd10-b46f-404f-a5de-81032b667e46","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":570591,"name":"GHMC- Ward 29 Office 18","address":"Pandit Khushilal Sharma Parisar","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"5bcd92b4-a476-4c47-a203-3e96179e5beb","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":609392,"name":"Govt School Nayabasera 18","address":"NAYABASERA SANJEEVANI","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"60306cc5-15a4-4195-876a-2889676631c0","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":561924,"name":"Saraswati Shisu Mandir Sch Cov","address":"Saraswati Shishu Mandir School Shivaji Nagar Bhopal Madhya Pradesh India","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"e1f4f7a1-42d5-4f58-934b-ed031fc3e7c0","date":"13-05-2021","available_capacity":137,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":591027,"name":"CD RAJ BHAVAN CVX","address":"RAJ BHAVAN BHOPAL","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"52de668d-5526-4e09-8c41-f34c1912302d","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":667429,"name":"CD Vallabh Bhawan CVX","address":"Vallabh Bhawan Bhopal","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"e8202c2d-b545-401c-b266-8bf9102687e2","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":561310,"name":"Govt Kanya School Nehru Nagar","address":"Govt Kanya School Nehru Nagar","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"85a85c27-cabd-4058-b902-7b0bf457cb44","date":"13-05-2021","available_capacity":114,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":636442,"name":"CD VIDHAN SABHA COVISHIELD","address":"VIDHAN SABHA","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"6010b80f-a818-4cf5-ad67-90a0cb49f1d6","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":611640,"name":"Kopal HSS Nehru Nagar18","address":"Kopal HSS Nehru Nagar Bhopal","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"b4a74e1f-cc5b-498a-b92d-25bf5c3bc072","date":"13-05-2021","available_capacity":0,"min_age_limit":18,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":639426,"name":"BANGANG WARD 25 CVX","address":"SANJEEVNI BANGANGA","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"17:00:00","fee_type":"Free","sessions":[{"session_id":"def621bf-2e3e-4514-a605-9debd696d8ef","date":"13-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVAXIN","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-05:00PM"]}]},{"center_id":636455,"name":"JAHANGIRABAD SANJEEVANI","address":"JAHANGIRABAD","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"18:00:00","fee_type":"Free","sessions":[{"session_id":"a58f02b7-a252-4daf-a541-b7ddf9bae21c","date":"19-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-06:00PM"]}]},{"center_id":627202,"name":"CD Sewaniya Gaud","address":"CD Sewaniya Gaud","state_name":"Madhya Pradesh","district_name":"Bhopal","block_name":"PHANDA","pincode":462003,"lat":23,"long":77,"from":"09:00:00","to":"18:00:00","fee_type":"Free","sessions":[{"session_id":"80130fd2-483a-4bb7-876a-9f2130484994","date":"19-05-2021","available_capacity":0,"min_age_limit":45,"vaccine":"COVISHIELD","slots":["09:00AM-11:00AM","11:00AM-01:00PM","01:00PM-03:00PM","03:00PM-06:00PM"]}]}]}
Enter fullscreen mode Exit fullscreen mode

This data is not structured. So, we will make use of json here. Let's cast the data into json file.

if result.ok:
    response_json = result.json()
Enter fullscreen mode Exit fullscreen mode

Now, In order to retrieve the information for each center, we will run a loop for the same. Initially, let's set the flag as false

flag = False
if response_json["centers"]:            
    if(print_flag.lower() =='y'):
        for center in response_json["centers"]:
Enter fullscreen mode Exit fullscreen mode

Now, let's display and verify our data for each center

print(center)

#OUTPUT:

{'center_id': 691508, 'name': 'Navin Girls HSS Tulsi Nagar 18', 'address': '1250 Tulsi Nagar HOSPITAL', 'state_name': 'Madhya Pradesh', 'district_name': 'Bhopal', 'block_name': 'PHANDA', 'pincode': 462003, 'lat': 23, 'long': 77, 'from': '09:00:00', 'to': '17:00:00', 'fee_type': 'Free', 'sessions': [{'session_id': 'b63ce111-23dd-4d4e-9f1c-c97073c57a52', 'date': '13-05-2021', 'available_capacity': 0, 'min_age_limit': 18, 'vaccine': 'COVAXIN', 'slots': ['09:00AM-11:00AM', '11:00AM-01:00PM', '01:00PM-03:00PM', '03:00PM-05:00PM']}]}
Enter fullscreen mode Exit fullscreen mode

And now finally we can retrieve the information for each session. We will apply the check parameters as the arguments we saved earlier.

for session in center["sessions"]:
    if (session["min_age_limit"] <= age and session["available_capacity"] > 0 ):
Enter fullscreen mode Exit fullscreen mode

If it stratifies our conditions, let's display the result.

print('Pincode: ' + pinCode)
print("Available on: {}".format(given_date))
print("\t", center["name"])
print("\t", center["block_name"])
print("\t Price: ", center["fee_type"])
print("\t Availablity : ", session["available_capacity"])

if(session["vaccine"] != ''):
    print("\t Vaccine type: ", session["vaccine"])
    print("\n")
Enter fullscreen mode Exit fullscreen mode

At the end, let's increase the counter by one.

counter += 1
Enter fullscreen mode Exit fullscreen mode

Let's cover the edge case:

if(counter == 0):
    print("No Vaccination slot avaliable!")
else:
    print("Search Completed!")
Enter fullscreen mode Exit fullscreen mode

Lastly, let's sync the data in real-time.

dt = datetime.now() + timedelta(minutes=3)

while datetime.now() < dt:
    time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Let's have a look at the output and let's compare it with the actual website.

output.png

Screenshot_1.png

Screenshot_2.png
And with that, it's a wrap! You can host the script at the server to get notified every time a slot is available near you. You can find all the code at my GitHub Repository. Drop a star if you find it useful.

Simple, isn't it? Hope this tutorial has helped. I would strongly recommend you to Check out the YouTube video of the same and don't forget to subscribe to my Channel.

I write about career, Blogging, programming, and productivity, If this is something that interests you, please share the article with your friends and connections. You can also subscribe to my newsletter to get updates every time I write something!

Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!

I would love to connect with you at Twitter | LinkedIn.

You should definitely check out my other Blogs:

Resources:

See you in my next Blog article, Take care

Discussion (2)

Collapse
grizzlysmit profile image
Francis Grizzly Smit

why not use selenium

to drive a real web browser and achieve the same results
i.e.

import selenium
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.firefox.options import Options
...
....
options = Options()
options.add_argument('-headless') # makes it run without opening 
                                                               # the browser window
driver = webdriver.Firefox(options=options)
driver.get(url)
Enter fullscreen mode Exit fullscreen mode

There is a chromium driver too, but I have only experience with the firefox version

Collapse
dollardhingra profile image
Dollar Dhingra

Great work! The script works as expected. The use of mixer library for notifying is really cool. I didn't know about that.

Also, I too worked on a CoWIN vaccination searching site. You can read about it on dev.to here on how to use it for searching vaccine: dev.to/dollardhingra/contribution-...
Checkout the demo here: dollardhingra.com/cowinquick/