Cowin Vaccine | how to create your own cowin availability notification and checker | cowin availability checker | How to Use Rest API in Linux #linuxtopic

Linuxtopic
0
how to use rest api in linux, cowin vaccine slot booking, cowin vaccine alert, cowin api, cowin api github, cowin api update, cowin api how to use, cowin api call, cowin, cowin availability script, cowin availability notification, cowin availability checker, cowin availability api

how to use rest api in linux, cowin vaccine slot booking, cowin vaccine alert, cowin api, cowin api github, cowin api update, cowin api how to use, cowin api call, cowin, cowin availability script, cowin availability notification, cowin availability checker, cowin availability api

In this tutorial we will learn, how to use REST API in Linux box, for this topic we have Cowin API and we will set alerts using this cowin API once available.

Prerequisite :

1 - Linux Box - 
2 - Internet 
2 - Mail configuration 
3 - Cowin API
4 - Curl
Linux Box - 

I'm using a CentOS 7 for this tutorial and it is hosted in my laptop and connected to Internet for API Calling and sending a email.

We can also use cloud box like AWS / Azure / Google cloud. who can run 24/7 for notification.

cat /etc/redhat-release

CentOS Linux release 7.6.1810 (Core)

Cowin API

https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin

In the above api we provide below input
  • pincode 
  • date
  • age
curl "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=455115&date=29-05-2021"


We got the all covid centers list of Dewas, you can check according to your pincode and date

In the next we will filter more like center name 
curl "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=455001&date=29-05-2021&min_age_limit=18" 2>/dev/null | grep -o '"name":"[^"]*"'

Read more.. about how to use regular expression part 2


Filter available_capacity

Read more.. about how to use regular expression part 1

curl "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=455001&date=29-05-2021&min_age_limit=18" 2>/dev/null | grep -o "available_capacity\":[0-9]\+"



Now we have center name and available capacity so we will create a script to call this cowin api using
vi cowin-alert.sh

#!/bin/bash
pincode=455001
date=`date -d '0 day' '+%d-%m-%Y'`

center=`curl "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=$pincode&date=$date&min_age_limit=18" 2>/dev/null | grep -oe '"name":"[^"]*"' -e "\"available_capacity\":[0-9]\+"`

echo $center


chmod +x cowin-alert.sh
Points :

1 - In the above script we create  pincode variable  and it is a editable you can edit as per the your area code

2 - Second variable we are print current date you can also edit for next day just increases the number like 0 day to 1 day ( tomorrow ) or  2 day ( day after tomorrow ) .

date=`date -d '0 day' '+%d-%m-%Y'`  - Today
date=`date -d '1 day' '+%d-%m-%Y'`  - Tomorrow 
date=`date -d '2 day' '+%d-%m-%Y'`  - Day after tomorrow

3 - In the third variable we used curl command and  grep value, after the execute, below response from cowin api, center name and available capacity


4 - Now we will  create 3 another variable below
c_name=$(echo $center | grep -oe '"name":"[^"]*"' | sed -e 's/"name"://g')
capacity=$(echo $center | grep -o "available_capacity\":[0-9]\+" | sed -e 's/available_capacity"//g')
c_available=`paste <(echo "$c_name") <(echo "$capacity") | sed '/:0/d'`
c_name is for  Center Name, we are removing a "name" from exact value.

we create a capacity variable for Available Capacity of vaccine and remove available_capacity" using sed command.

In c_available variable we are merging the both output c_namec_name + c_name


To check outputs we use below
echo $c_available

In the above screenshot, we printed  center with available capacity of  Bhopal ( updated pin in variable  ) 

In the Next step we will use condition 
echo $c_available | grep -E :[0-9]\+ 2>&1 >/dev/null
if [ $? == 0 ]; then
        echo "Available Center and Capacity of Vaccine"
        paste <(echo "$c_name") <(echo "$capacity") | sed '/:0/d'
else
        echo "All Center is Booked"
fi

Explanations of condition :

1 - First we echo $c_available variable and grep capacity like :[0-9]\+ using regular expression. also we send the command output in dev null using 2>&1 >/dev/null  

2 - We used if condition using the above command execution status code line 0 or 1 using $? == 0, in this condition if command founded any value then it will print "Available Center and Capacitiy of Vaccine" else it will echo "All Center is Booked"

Output of script:




We can  send this output to our email address as notification, for this we require active email server for send mail. if you don't have follow below links 

Email configuration  

How to Configure Sendmail -- Click Here...

How to configure  Sendmail using Gmail SMTP relay -- Click Here...

How to configure  Postfix using Gmail SMTP relay -- Click Here...

Note: Follow any one if you do not have a active email server to send email.

Notification Sent in Email 

We have to update below in the script
paste <(echo "$c_name") <(echo "$capacity") | sed '/:0/d' | mail -s "Cowin Vaccine Alerts" lokesh@linuxtopic.com

After adding mail command, Update your email to received cowin vaccine alerts and execute the script



At the final steps we can set the cronjob for receiving alerts, here we will set for every 2 min. once i will scheduled i will disable the cronjob. 
crontab -e 

*/2    *     *     *     *    /root/cowin_alert.sh
Check in logs:


Email Received:



Note: You can make it better then me, try own yourself and update below script and send me, 
Best of luck...

Full Script :

#!/bin/bash

#pincode=455115
#pincode=455001
#pincode=403504
#pincode=452010
#pincode=450331
pincode=462042

date=`date -d '1 day' '+%d-%m-%Y'`

center=`curl "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/findByPin?pincode=$pincode&date=$date&min_age_limit=18" 2>/dev/null | grep -o -e '"name":"[^"]*"' -e "\"available_capacity\":[0-9]\+" `

c_name=$(echo $center | grep -oe '"name":"[^"]*"' | sed -e 's/"name"://g')
capacity=$(echo $center | grep -o "available_capacity\":[0-9]\+" | sed -e 's/available_capacity"//g')

c_available=`paste <(echo "$c_name") <(echo "$capacity") | sed '/:0/d'`

echo $c_available | grep -E :[0-9]\+ 2>&1 >/dev/null
if [ $? == 0 ]; then
        echo "Available Center and Capacity of Vaccine"
        paste <(echo "$c_name") <(echo "$capacity") | sed '/:0/d' | mail -s "Cowin Vaccine Alerts" lokesh@linuxtopic.com
else
        echo "All Center is Booked"
fi


Thanks you !!

I hope this topic gave you all the information you needed. If you have any further questions or would like more detailed directions feel free to contact us using any of the following sources.We look forward to talking to you.

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!