Python program to check the Corona Cases and Notify

Python program to check the Corona Cases and Notify

In this article, we will make a corona case notifier. At the current time the cases are still pretty high so to track them without going online every time we will make the program do this and get us notified about the number of total cases, totally recovered, and deaths in custom time gaps. For this, we will need some modules

  • plyer
  • requests
  • bs4

You can download and install these modules by opening command prompt and writing ''pip install plyer requests bs4'' and hit enter it will automatically install these modules. Then we will start by importing them.

from plyer import notification
import requests
from bs4 import BeautifulSoup as bs
import os
import time

The os and time modules are inbuilt so you don't have to download them. First, we will make a function notifyMe() which will take two arguments title of the notification and message to be displayed.

def notifyMe(title,message):
        notification.notify(
        title = title,
        message = message,
        app_icon = os.getcwd()+"\covid.ico",    '''you can use a icon for your notification       as well, but make sure to put it in the same directory and it should be an .ico file not png or jpg'''
        timeout = 10 # its the timeout for which the notification will be seen
)

Another function will be to get and fetch the required data. We will use https://virusncov.com/ for the data.

def getdata():
r = requests.get("https://virusncov.com/")
    data = r.content        # to get it in content form
    soup = bs(data,'lxml')  # we have called BeautifulSoup as bs in the beginning #as it will be easy to use
    getdata.total_case = "Total Corona cases: %s " %(soup.h2.text.split(" ")[-1])+"\n" #we used a webscrapping to access the required fields in which the #number of total cases was showing
    getdata.total_death = "Total Deaths: %s " %(soup.find("span",{"class":"red-text"}).text+"\n")
    getdata.total_rec = "Total Recovered: %s " %(soup.find("span",{"class":"green-text"}).text) #.text is used to get the text only from a given field excluding all #other ascii characters and other things.

print('Welcome to the program.\n') # A simple greeting to the program
time_gap = int(input("\nEnter the amount of interval in minutes which the program should pop up: "))
amount = time_gap * 60    #convert the minute into seconds for the time function

Finally, a while loop to run it endlessly

while True:
        getdata()
        notifyMe("Corona stats",getdata.total_case+getdata.total_death+getdata.total_rec)
        print("updating..")
        time.sleep(amount)

Putting the code together

from plyer import notification
import requests
from bs4 import BeautifulSoup as bs
import os
import time

def notifyMe(title,message):
        notification.notify(
        title = title,
        message = message,
        app_icon = os.getcwd()+"\covid.ico",    '''you can use a icon for your notification       as well, but make sure to put it in the same directory and it should be an .ico file not png or jpg'''
        timeout = 10 # its the timeout for which the notification will be seen
)
def getdata():
r = requests.get("https://virusncov.com/")
    data = r.content        # to get it in content form
    soup = bs(data,'lxml')  # we have called BeautifulSoup as bs in the beginning #as it will be easy to use
    getdata.total_case = "Total Corona cases: %s " %(soup.h2.text.split(" ")[-1])+"\n" #we used a webscrapping to access the required fields in which the #number of total cases was showing
    getdata.total_death = "Total Deaths: %s " %(soup.find("span",{"class":"red-text"}).text+"\n")
    getdata.total_rec = "Total Recovered: %s " %(soup.find("span",{"class":"green-text"}).text) #.text is used to get the text only from a given field excluding all #other ascii characters and other things.

print('Welcome to the program.\n') # A simple greeting to the program
time_gap = int(input("\nEnter the amount of interval in minutes which the program should pop up: "))
amount = time_gap * 60    #convert the minute into seconds for the time function

while True:
        getdata()
        notifyMe("Corona stats",getdata.total_case+getdata.total_death+getdata.total_rec)
        print("updating..")
        time.sleep(amount)