Coder's GroundPython

Fetch real-time COVID-19 data with python EASILY!

thumbnail image

The novel coronavirus (COVID-19) has taken over the world. It feels like some crazy Hollywood movie has come true. And we programmers are like those Violin players from Titanic who kept on playing until the ship sank.

So, we are gonna code till the end of this world. Today, let’s make a simple python program to fetch data of novel coronavirus cases, both across the world and in cities of India.

Fetching COVID-19 Coronavirus’ data using python

  • Fetching state wise corona cases in india or district wise coronavirus data

for this, there are lot’s of APIs available on the internet. One of the most popular and reliable source is https://api.covid19india.org/ .

The link has lots of APIs depending upon the specific data you want to fetch.

For our purpose we are gonna use National/State/District Level : Latest cumulative/daily counts , which is listed in the website above. Let’s start step-wise. I request you to emphasize more on how data is being fetched, rather than just copying the codes.

  • Create a python file. Import JSON module.

    import json
  •  Now to access any URL with python, we need to import  a module named urllib3 

    import urllib3

     

  • Opening the URL

    While there are a ton of great APIs on the Internet, most of them require you to sign up in order to get an API key. But, this is not the case with this specific API which we are going to use. No sign up required in this case.

    We need to start by creating a PoolManager() object using urllib3. It’s that object that you can use to make requests to a website.

    http = urllib3.PoolManager()
  • Use this HTTP object to make a GET request

    Now we need to make a request with GET method, using the HTTP object which we’ve just created to the covid19india api. This is the link to JSON – https://api.covid19india.org/v3/data.json

    covid = http.request('GET', 'https://api.covid19india.org/v3/data.json')
  • Parsing the Json

    Now, I want you to try printing the variable ‘covid’ which we’ve created. You’ll find a request object as the output. It isn’t what you need? Now try printing covid.data.

    print(covid.data)

    You’ll see that we are kind of close, but we haven’t yet reached to the exact data that we need, because your output is not completely readable. Now , we will be trying to obviously do that.

    We need to decode the data into UTF-8.

    print(covid.data.decode('UTF-8'))

    Now,please be patient! we are almost there. The data you’ve printed is  JSON. You can parse that with the JSON module we had imported while starting this code.

    covid_dict = json.loads(covid.data.decode('UTF-8'))

    There you go! we have all the data in form of dictionaries within dictionaries. I want you to print covid_dict and understand the format of the output.

  • Get desired data

the syntax of getting desired data out of the dictionary depends on what you want to fetch. If you want all the details of a particular state, you can do that by writing its short form,e.g. ‘MP’ for Madhya Pradesh.

print(covid_dict['MP'])

This will give you a total of all cases,tests and recoveries along with the data of each districts in the state.

But if you need data of a specific city, You can further add it as the key to the inner dictionaries . For example:

print(covid_dict['MP']['districts]['Indore'])

Note: I’ve noticed that few of the states’ short form is not correct . for example for chhattisgarh, ‘CT’ has been used instead of ‘CG’. So before trying to show your friends about how cool you are, make sure you print the whole dictionary to double check the short form of your state.

  • data of cases Across the world

This is an easy task to perform.

Among many python packages in the Python Package Index, is a package named covid 2.2.11 . It uses John Hopkins University API

 

Requirements: python >=3.6

How to install:

pip install covid

Dependencies:

pydantic
requests

 

the documentation of package is quite easy to understand, you can get it here

click here for a link to the package  .

There you go, fellow programmer. Stay safe, and keep playing your violin and don’t worry about the ship 😉

Also read:Top free online python courses on Internet for beginners , How to send free SMS with python, A Simple python code to get real time stock market details

You may also like

Leave a reply

Your email address will not be published. Required fields are marked *