Trending Articles       10 Flutter Listview Examples       Flutter Custom Button Example       Flutter Audio Recording       Flutter 2.0 Autocomplete TextField       Current Location Google Maps

Python Script to Integrate Cowin API to check Cowin vaccine slot availability

Published June 17, 2021

In this python programming example we will write a script to check availability of Cowin vaccine slot availability. For this we will call a Cowin API and display notification of availability status on desktop with playing a sound.

In this example script we will cover

  • Making an API call
  • How to display Notifications on desktop
  • How to play music

 

We will use Cowin public API to get the slots availability status

 

Python Cowin API integration

 

 

Let's get started

Step 1: Create a Python project

Step 2: Check require python modules installed or not

For this script we will use the below modules

requests : To call API request
win10toast : To show notifications on desktop
pygame : To play music

 

 

Step 3: Reading API

Now import request to make an API call for Cowin. Read API integration in python also

import requests

 

Call API by using below function

def CallCowinAPI():
    today = date.today()
    fromDate = today.strftime("%d-%m-%Y");

    print ("Calling Cowin request")

    print ("Checking vaccine availablity for pincode " + str(PIN_CODE) + " on date " + fromDate)
    API_URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=" + str(
        PIN_CODE) + "&date=" + fromDate
    resp = requests.get(API_URL)
    return resp.content

 

 

Show Desktop Notification with Python

To show notification we are using the ToastNotifier module

from win10toast import ToastNotifier

 

 

def showNotification(message):
    '''
    utility method to show desktop notification 
    '''
    toaster.show_toast("Cowin Notification",
                       message,
                       icon_path="custom.ico",
                       duration=30)

 

 

How to play music with python

To play music in the python script we used module mixer

from pygame import mixer

here is the code to play music

def playSong():
    '''
    utility function to play song
    '''
    mixer.init()
    mixer.music.load("song.mp3")
    mixer.music.set_volume(10)
    mixer.music.play()

 

Here the complete python script for check availability of Cowin vaccine slots

import requests
import json
from win10toast import ToastNotifier
import datetime
from datetime import date
from pygame import mixer
import time

toaster = ToastNotifier()
PIN_CODE = 500050


def CallCowinAPI():
    today = date.today()
    fromDate = today.strftime("%d-%m-%Y");

    print ("Calling Cowin request")

    print ("Checking vaccine availablity for pincode " + str(PIN_CODE) + " on date " + fromDate)
    API_URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByPin?pincode=" + str(
        PIN_CODE) + "&date=" + fromDate
    resp = requests.get(API_URL)
    return resp.content


def playSong():
    '''
    utility function to play song
    '''
    mixer.init()
    mixer.music.load("song.mp3")
    mixer.music.set_volume(10)
    mixer.music.play()


def parseJSON(res):
    '''
    utility function to parse API JSON response
    '''
    root = json.loads(res);
    centres = root['centers']
    if len(centres) == 0:
        return "No centres found !!"
    else:
        return checkVaccineAvailabilty(centres)


def checkVaccineAvailabilty(centres):
    '''
    utility function to check for vaccine availablity
    '''
    for center in centres:
        block = center['block_name']
        for session in center['sessions']:
            if session['min_age_limit'] == 18 and session['available_capacity'] > 0:
                print
                currentDateTime() + "FOUND ONE"
                return str(session['available_capacity']) + ' doses of ' + session['vaccine'] + ' available @ ' + block

    print
    currentDateTime() + ' :: None found in ' + block + '. Checking in a minute ...'
    print
    "#############################################"
    print
    "---------------------------------------------"
    return 'None'


def currentDateTime():
    '''
    utility function to get current date and time
    '''
    return datetime.datetime.now().strftime("%d-%m-%Y, %H:%M:%S")


def showNotification(message):
    '''
    utility method to show desktop notification 
    '''
    toaster.show_toast("Cowin Notification",
                       message,
                       icon_path="custom.ico",
                       duration=30)


def main():
    '''
    the main function 
    '''
    response = CallCowinAPI()
    vaccine_available = parseJSON(response)
    if vaccine_available != 'None':
        playSong()
        showNotification(vaccine_available)

    time.sleep(40)


while True:
    main()

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

186 Views

Flutter Questions

Android Questions

© 2021 RRTutors . All Rights Reserved.