Vaccine availability checker

Raghuram Bharathan
3 min readJun 25, 2021

For the background behind this, read this.

Co-WIN has published a number of public (readOnly) APIs. I wanted to use these to book myself a vaccine slot.

The general approach was this

  • Call the API and get the response
  • Examine the response and filter it for relevant data
  • Send data

Choice of technology

There was no dearth of options — from shell script to full-fledged programming language like java. For each, there were also associated frameworks. After a short consideration, I chose to go with javascript.

To run javascript outside browser, I used nodejs.

The primary reason for this choice was that the API returned json (javascript object notation). So, javascript was the language of choice, needing no additional library or code to parse.

The application was developed iteratively. This meant, features were added progressively rather than thought out upfront. This of course meant, re-work but the first version was out in two commits and each subsequent commit had a better version!

Which API?

There was findByPin — vaccine availabilty by pincode. This was quite restrictive since it reduced the number of results drastically

findByDistrict was better as it gave availability data for the whole district. Yes, it would give results of locations far from my place, but at least I would have options

findByLatLong was a needless API since I doubt if vaccine centres updated their GPS coordinates

I was inclined to go with findByDistrict till I found calenderByDistrict, an API that returned vaccine availability details for 7 days. This gave maximum options and flexibility.

calenderByPin is a good option to go with, if you are particular about location and flexible about dates.

JSON response formats

Interestingly, the response format is different for findByDistrict and calenderByDistrict — so they can’t be interchangeably used.

The Code

I went with the default https library to call the API.

const https = require("https");https.get("https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByDistrict?district_id=307&date=25-06-2021",(resp) => {
let data = "";
resp.on("data", (chunk) => {
data += chunk;
});
resp.on("end", () => {
parseData(data);
})
;})
.on("error", (err) => {
console.log("Error: " + err.message);
});

Now to parse the data

const parseData = (rawData) => {
const data = JSON.parse(rawData);
const covishield45 = data.sessions.filter((item) =>
item.vaccine === "COVISHIELD" && item.min_age_limit === 45);
let output = "No | Name | Address | PINCODE | Capacity (Dose 2\n";
covishield45.forEach((item, index) => {
output += `${index + 1} ${item.name} | ${item.address} | ${item.pincode} | ${item.available_capacity_dose2} \n`
});
sendMail(output)};
};

Ok, now to send the mail. To do this, I used the popular nodemailer package.

const sendMail = (data) => {
const transporter = tranodemailer.createTransport({
pool: true,
host: "localhost",
port: 25
});
var mailOptions = {
from: 'raghu@...',
to: 'raghu@...,
subject: 'Covid Vaccine availability Info',
text: data
};
transporter.sendMail(mailOptions, function(err, data) {
if (err) {
console.log("Error " + err);
} else {
console.log(data);
}
});
}

Nodemailer is quite powerful and allows different configurations for email including gmail.

That is it! My first version of the vaccine checker was ready!

Now I worked on enhancements and there were plenty of them

  • Configurable values instead of hard-coding them
  • Email in HTML format so that it is easy to read
  • Support calendarByDistrict API and allow user to choose between day or week data

Doing this helped me reach my first milestone, which I deployed to our office staging server. Now I needed to run this script at specific intervals.

I wanted it to run

  • once a minute from 7 AM to 10 PM
  • once in 15 minutes from 10 PM to midnight
  • once in an hour from midnight to 7 AM

cron was the way to go. I added the following configuration for this

* 7-21 * * * cd /home/raghu/vaccine-check && /home/raghu/.nvm/versions/node/v14.17.0/bin/node /home/raghu/vaccine-check/vaccine.js >> /tmp/vaccine.err 2>&1*/15 22-23 * * * cd /home/raghu/vaccine-check && /home/raghu/.nvm/versions/node/v14.17.0/bin/node /home/raghu/vaccine-check/vaccine.js >> /tmp/vaccine.err 2>&10 0-6 * * * cd /home/raghu/vaccine-check && 
/home/raghu/.nvm/versions/node/v14.17.0/bin/node /home/raghu/vaccine-check/vaccine.js >> /tmp/vaccine.err 2>&1

This can be run from your own machine so long as you keep it running all the time and configure gmail (or any mail provider) to send mails

Visit GitHub repo for the full and update code.

--

--

Raghuram Bharathan

Software developer, blogger, passionate about software engineering and technology