Vaccinater | vaccine notification
Tags: python, javascript, webdev
Published: 2021-12-30 09:35:37

{% youtube wydgbawcdJg %}


To beat covid-19, Vaccination is going all around the world. In India vaccination is being carried at a great pace with coWIN.gov.in .But unfortunately, getting a slot for your jab is tough, as they quickly fill out.
So I thought of a hack that can notify users whenever vaccine slots is available in their location.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5eyb24xefmw2u0hhj47.png)

**$ git init**

While searching api for covid data for my covid dashboard, I came across [api.setu.gov.in](https://apisetu.gov.in/public/marketplace/api/cowin), API endpoints for CoWin app. I started looking into these api response. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/227bqgn0up075gt5wrpf.png)
As you can see it returns an array with centers data like like available dose, vaccine name , address etc. API takes two parameters, district id & current date.

```
url = 'https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id='+str(district)+'&date='+today
response = requests.get(url).json()
```
 requesting url with python Requests library and then .json() converts JSON response to python dictionary, Now we can easily access response data.
Now with data at our hands, we want to request this API for each users, so we need to get each users data, so let’s build a form. Since I’m using python so I will be using Flask and instead of using WTForms I have used POST method to save user responses.
after getting user details, we also need to add it to a database. since all user’s data are unrelated so I’m simply using NoSQL, like MongoDB with pymongo.

{% gist https://gist.github.com/mayukhpankaj/3ee06db6cb5cc18c3f43310a89f06b49.js %}

**Notification.py **

Now with data stored in our DB, we want to iterate it over and make request to CoWin API for each user, make a message out of it & send it to users email.
sorting the whole database & using previous response can save us a lot of time. Like if 100 users are from Delhi, Instead of calling the API 100 times for Delhi, We can use the first response for rest 99 users.
Sort Query on MongoDB

`sorted_doc = collection.find().sort('district',pymongo.ASCENDING)
users = list(sorted_doc)`

> Remember collection.find() gives a cursor object which is not exactly a list, so we can use list() to get a list & can iterate it easily.

**Sending pretty HTML emails**

we can setup a basic SMTP client to send emails & use MIMEText to send html in email. HTML ? yes, now we can design our email just like any webpage.

{% gist https://gist.github.com/mayukhpankaj/376a0933c9d4e923d768eda34880433d.js %}

**Scheduling**

since we want to carry out this operation & send email everyday at specific time so we need schedule this job. we can use scheduler or APScheduler in python, but it requires the cloud instances to be running, So I have used GCP cloud function to schedule this job.

```
import schedule
import time
def notify():
 #todo
schedule.every().day.at("09:00").do(notify)
while True:
 schedule.run_pending()
 time.sleep(1)
```
**
Thank you for reading !**