Create Covid-19 data list using Python in 5 minute


getcode

The getcode() method return a value which indicate, we are succeeded or not. The function return 200 when we have no issue reaching the server.

Output json data

The second part is that getting the desired content from the JSON object. Iterate through JSON object and get our data and print them to the screen, that our printData function going to do

def printData(data):
    jsonData = json.loads(data)
    print('Printing Json Data....')
    for d in jsonData:
        s = str(d["date"]) 
        dt=datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))             
        print("Date :" + str(dt))
        print("State :" + d["state"])
        print("Positive :" +  str(d["positive"]))
        print("Negetive :" +  str(d["negative"]))
        print("Pending :" +  str(d["pending"]))
        print("Hospitalized [Today] :" +  str(d["hospitalizedCurrently"]))
        print("Hospitalized [All] :" +  str(d["hospitalizedCumulative"]))
        print("Died :" +  str(d["death"]))     
        print("Recoverd :" +  str(d["recovered"]))
        print('---------------------------------------')

Integer date to Python date object

Our json object contain date data which is date of integer value, which means that it is not a formatted date. Then we have to strip out Year,Day and Month and construct a new date object using the values

        s = str(d["date"]) 
        dt=datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))             
        

Complete code

import urllib2
import json
from datetime import datetime, timedelta
def printData(data):
jsonData = json.loads(data)
print('Printing Json Data....')
for d in jsonData:
s = str(d["date"])
dt=datetime(year=int(s[0:4]), month=int(s[4:6]), day=int(s[6:8]))
print("Date :" + str(dt))
print("State :" + d["state"])
print("Positive :" + str(d["positive"]))
print("Negetive :" + str(d["negative"]))
print("Pending :" + str(d["pending"]))
print("Hospitalized [Today] :" + str(d["hospitalizedCurrently"]))
print("Hospitalized [All] :" + str(d["hospitalizedCumulative"]))
print("Died :" + str(d["death"]))
print("Recoverd :" + str(d["recovered"]))
print('---------------------------------------')
def main():
urldata = 'https://covidtracking.com/api/v1/states/current.json'
weburl = urllib2.urlopen(urldata)
print("result code :" + str(weburl.getcode()))
if (weburl.getcode() == 200):
data = weburl.read()
printData(data)
else:
print "Some server related error occurs"
if __name__ == "__main__":
main()
view raw covid19-livedata.py hosted with ❤ by GitHub

Published by

Manoj

A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.