Skip to content

Instantly share code, notes, and snippets.

@Radcliffe
Created March 17, 2020 18:41
Show Gist options
  • Save Radcliffe/06a15095eb1e7af062e478d0d7221338 to your computer and use it in GitHub Desktop.
Save Radcliffe/06a15095eb1e7af062e478d0d7221338 to your computer and use it in GitHub Desktop.
Python script to display a Covid-19 report
# This Python 3 script displays a Covid-19 report.
#
# Usage: Type the following command in a terminal window.
# python3 covidreport.py
#
# Author: David Radcliffe ([email protected])
# License: Public domain
# Date: 17 March 2020
from datetime import datetime
import json
import requests
base_url = 'https://covid19.mathdro.id/api/'
def fetch(route=''):
return requests.get(f'https://covid19.mathdro.id/api{route}').json()
def get_world():
data = fetch()
return {
'confirmed': data['confirmed']['value'],
'recovered': data['recovered']['value'],
'deaths': data['deaths']['value'],
'lastUpdate': data['lastUpdate'],
}
def get_country(country='USA'):
data = fetch(f'/countries/{country}')
return {
'confirmed': data['confirmed']['value'],
'recovered': data['recovered']['value'],
'deaths': data['deaths']['value'],
'lastUpdate': data['lastUpdate'],
}
def get_state(state='Minnesota'):
data = fetch('/countries/USA/confirmed')
for row in data:
if row['provinceState'] == state:
return {
'confirmed': row['confirmed'],
'recovered': row['recovered'],
'deaths': row['deaths'],
'active': row['active'],
'lastUpdate': datetime.fromtimestamp(row['lastUpdate']//1000).isoformat() + '.000Z',
}
return {}
def get_all_data():
return {
'World': get_world(),
'USA': get_country('USA'),
'Minnesota': get_state(),
}
def detail(region, d):
region = region.ljust(20)
confirmed = str(d['confirmed']).rjust(10)
recovered = str(d['recovered']).rjust(10)
deaths = str(d['deaths']).rjust(10)
return f" {region}: {confirmed} confirmed, {recovered} recovered," \
f" {deaths} deaths."
if __name__ == '__main__':
data = get_all_data()
today = datetime.now().strftime('%Y-%m-%d')
report = [
f"Coronavirus report for {today}:",
detail('World', data['World']),
detail('USA', data['USA']),
detail('Minnesota', data['Minnesota'])
]
print(*report, sep='\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment