dragon-bot/app/corona.py

52 lines
2.0 KiB
Python

from pandas import read_csv
import pandas as pd
import requests
import os
import discord
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
if not os.path.exists(local_csv):
print("no local csv found, downloading latest")
r = requests.get(download_url, allow_redirects=True)
open(local_csv, 'wb').write(r.content)
return local_csv
def sum_numbers(state):
series = read_csv(get_csv(), header=0, parse_dates=[0], index_col=0, squeeze=True)
last_updated = series['Last_Update'].iloc[0]
state_query = series.loc[series['Province_State'] == state]
if state_query.empty:
embed = discord.Embed(description="No results found for %s.\n\nPlease enter a valid US state" % state, color=0x428bca, type="rich")
embed.set_author(name="CSSE at Johns Hopkins University", icon_url='https://avatars2.githubusercontent.com/u/60674295')
return embed
sums = state_query.sum()
confirmed = sums['Confirmed']
deaths = sums['Deaths']
recovered = sums['Recovered']
embed = discord.Embed(description="Most recent Corona stats for %s\nUpdated once a day\n(Last update was %s)" % (state, last_updated), color=0x428bca, type="rich")
embed.set_author(name="CSSE at Johns Hopkins University", icon_url='https://avatars2.githubusercontent.com/u/60674295')
embed.add_field(name='Confirmed Cases', value=confirmed)
embed.add_field(name='Recovered Cases', value=recovered)
embed.add_field(name='Deaths', value=deaths)
embed.add_field(name='Source', value='https://github.com/CSSEGISandData/COVID-19')
return embed
def parse_message(message):
try:
state = string.capwords(' '.join(message.lstrip('!corona').split()))
if not state:
state = 'California'
except IndexError:
state = 'California'
return sum_numbers(state)