You are currently viewing Real-time notification for COVID-19 cases using Python

Real-time notification for COVID-19 cases using Python

Loading

In this tutorial, you will learn about how to get real-time notifications for COVID-19 cases using python.

Like the vast majority on the planet at this moment, I’m truly worried about COVID-19. I wind up continually examining my own well being and thinking about whether/when I will contract it.

This is a very tough time for all of us. We should stand together and fight for this epidemic corona that is the only way to defeat this thing.

Instead of sitting idly by and letting whatever is ailing me keep me down, I decided to do my best and keep getting updated on the COVID-19 patients. So I decided to make a real-time notification on my system that will help me to keep track of these cases.

Inside today’s tutorial, you will learn how to

– get live updates on COVID-19 cases every single hour.

The libraries that I am gonna use now is BeautifulSoup for scrapping web pages and we use plyer to get notifications.

To install these libraries we can use the pip command

– pip install plyer

– pip install beautifulsoup4


Now let us move to the coding part

from plyer import notification
import requests
from bs4 import BeautifulSoup
import time


def notifyMe(title,message):
	notification.notify(
		title=title,
		message=message,
		app_icon="Your icon location",
		timeout=15
	)


def getData(url):
	r=requests.get(url)
	return r.txt

Step 1– To Load the libraries.

Step 2– Now we define a function notifyMe() and passing the parameter title and message. After that www call the notify function and setting the title, message, an app icon and we set a notification timer for 15 seconds.

Note– Your icon must be in the form of .ico format.

Step 3– Now we define getData function and we pass the URL. In our case, it is India so we pass the URL of Indian medical organization. and return the text.

Now we call the main function

if __name__ == '__main__':
	while True:

		notifyMe("Covid-19 Updates","Warning")
		myHtmlData=getData('https://www.mohfw.gov.in/')

		#print(myHtmlData)
		soup=BeautifulSoup(myHtmlData,'html.parser')
		#print(soup.preetify())

		myDataStr=""

		for tr in soup.find_all('tbody')[1].find_all('tr'):
			#print(tr.get_text())
			myDataStr+= tr.get_text()
		myDataStr=myDataStr[1:]
		itemList= myDataStr.split("\n\n")


		states=['Chandigarh', 'Chandigarh', 'Punjab', 'Uttar Pradesh', 'Kerala', 'Delhi']

		for item in itemList[0:21]:
			dataList=item.split('\n')
			if dataList[1] in states:
				print(dataList)
				nTitle='Cases of COVID-19'
				nText=f"STATE {dataList[1]} Indian:{dataList[2]}  Foreign: {dataList[3]}\n Deaths: {dataList[4]}\n Cured: {dataList[5]}"
				notifyMe(nTitle,nText)
				time.sleep(2)

		time.sleep(3600)

Now we add a while loop and set equal True

Line-5 we pass the URL that contains the tables of COVID-19 cases. It will display the resulting State wise.

The tables consist of following columns like

– Name of State/ Union territory

– Total confirmed cases (Indian national)

– Total confirmed cases (Foreign national)

– Number of patients that are cured or discharged or migrated

– Total no of Deaths

Line-8 Now we use BeautifulSoup function to parse the Html content

Line-9 After that we use prettify function to make the Html code looks nicer

Line-13 Now we loop over the Html codes and find the table’s body. If you know a little bit about Html we are finding the texts that are in the

tag.

Line-16 Now we remove the spaces by using the slice operator

Line-20 Now we make the notification bell for the particular states like I am choosing some of the states like Mumbai, Chandigarh, Punjab, Uttar Pradesh, Kerala, and Delhi.

Line-22 Now we loop over through all the states

Line-24 After that we pass the condition If data in states and call the notify

function.

Line-27 after that we display the texts like Indian, foreigners, Cured and no of deaths for a particular states

Last Line we put the timer to sleep for 1 hrs by this we get a notification after every hour and that helps us to keep the track of the patients.

Summary

In this tutorial, we learned about how to make real-time notifications for COVID-19 cases. And I am requesting to all of us to stay home and follow the government rules. I hope it will pass soon.

I hope you enjoyed the blog post. Please like it if you like this blog and feel free to comment if you have any problem regarding implementation.

And the code is available on Github as usual.

Some of the References are

-https://www.mohfw.gov.in To get the live track of COVID-19 cases in India

https://experience.arcgis.com/experience/685d0ace521648f8a5beeeee1b9125cd

To get the live track of COVID-19 cases in the whole world