From 113bfc1510069ff842c24b5f3ba59a49708cfae7 Mon Sep 17 00:00:00 2001 From: Luke Date: Wed, 1 Apr 2020 12:40:18 -0700 Subject: [PATCH] Now corona virus stats show change since yesterday --- app/corona.py | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/app/corona.py b/app/corona.py index 1419cc13..a8eb553c 100644 --- a/app/corona.py +++ b/app/corona.py @@ -7,21 +7,32 @@ import string def get_csv(): git_url = 'https://api.github.com/repos/CSSEGISandData/COVID-19/contents/csse_covid_19_data/csse_covid_19_daily_reports' - git_blob = requests.get(git_url).json()[-2] - file_name = git_blob['name'] - download_url = git_blob['download_url'] - local_csv = "/app/%s" % file_name + git_blob = requests.get(git_url).json() - if not os.path.exists(local_csv): - print("no local csv found, downloading latest") + most_recent = git_blob[-2] + file_name = most_recent['name'] + download_url = most_recent['download_url'] + most_recent_csv = "/app/%s" % file_name + if not os.path.exists(most_recent_csv): + print("no local csv found, downloading %s" % file_name) r = requests.get(download_url, allow_redirects=True) - open(local_csv, 'wb').write(r.content) + open(most_recent_csv, 'wb').write(r.content) - return local_csv + yesterday = git_blob[-3] + yesterday_filename = yesterday['name'] + yesterday_url = yesterday['download_url'] + yesterday_csv = "/app/%s" % yesterday_filename + + if not os.path.exists(yesterday_csv): + print("no local csv found, downloading %s" % yesterday_filename) + r = requests.get(yesterday_url, allow_redirects=True) + open(yesterday_csv, 'wb').write(r.content) + + return most_recent_csv, yesterday_csv -def sum_numbers(location): - series = read_csv(get_csv(), header=0, parse_dates=[0], index_col=0, squeeze=True) +def query_csv(csv_path, location): + series = read_csv(csv_path, header=0, parse_dates=[0], index_col=0, squeeze=True) last_updated = series['Last_Update'].iloc[0] location_query = series.loc[series['Province_State'] == location] if location_query.empty: @@ -30,10 +41,18 @@ def sum_numbers(location): embed = discord.Embed(description="No results found for %s.\n\nPlease enter a valid Country name or US state" % location, color=0x428bca, type="rich") embed.set_author(name="CSSE at Johns Hopkins University", icon_url='https://www.pngitem.com/pimgs/m/27-270528_johns-hopkins-was-a-founding-member-of-the.png') return embed - sums = location_query.sum() - confirmed = sums['Confirmed'] - deaths = sums['Deaths'] - recovered = sums['Recovered'] + return last_updated, location_query.sum() + + +def sum_numbers(location): + today, yesterday = get_csv() + + last_updated, today_sum = query_csv(today, location) + last_updated, yesterday_sum = query_csv(yesterday, location) + + confirmed = "%s (+%s from yesterday)" % (today_sum['Confirmed'], int(today_sum['Confirmed']) - int(yesterday_sum['Confirmed'])) + deaths = "%s (+%s from yesterday)" % (today_sum['Deaths'], int(today_sum['Deaths']) - int(yesterday_sum['Deaths'])) + recovered = "%s (+%s from yesterday)" % (today_sum['Recovered'], int(today_sum['Recovered']) - int(yesterday_sum['Recovered'])) embed = discord.Embed(description="Most recent Corona stats for %s\nUpdated once a day\n(Last update was %s)" % (location, last_updated), color=0x428bca, type="rich") embed.set_author(name="CSSE at Johns Hopkins University", icon_url='https://www.pngitem.com/pimgs/m/27-270528_johns-hopkins-was-a-founding-member-of-the.png')