From c1176d804cfc487fc1d8c7a9f6be07578ab4e9a9 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Thu, 26 Mar 2020 14:17:56 -0700 Subject: [PATCH] Fixing corona herror handling --- app/corona.py | 15 +++++++++++---- app/help_methods.py | 6 +++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/corona.py b/app/corona.py index 6e47acc1..b9f9ceea 100644 --- a/app/corona.py +++ b/app/corona.py @@ -14,7 +14,6 @@ def get_csv(): if not os.path.exists(local_csv): print("no local csv found, downloading latest") - # url = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/%s.csv" % date r = requests.get(download_url, allow_redirects=True) open(local_csv, 'wb').write(r.content) @@ -23,12 +22,18 @@ def get_csv(): def sum_numbers(state): series = read_csv(get_csv(), header=0, parse_dates=[0], index_col=0, squeeze=True) - sums = series.loc[series['Province_State'] == state].sum() + 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" % state, color=0x428bca, type="rich") + 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) @@ -38,7 +43,9 @@ def sum_numbers(state): def parse_message(message): try: - state = string.capwords(' '.join(message.split()[1:])) + state = string.capwords(' '.join(message.lstrip('!corona').split())) + if not state: + state = 'California' except IndexError: state = 'California' return sum_numbers(state) diff --git a/app/help_methods.py b/app/help_methods.py index 7a518469..8098bc9f 100644 --- a/app/help_methods.py +++ b/app/help_methods.py @@ -33,6 +33,10 @@ def get_help_message(method): 'Admin use only. Deletes dragon bot\'s messages from the channel', 'Usage: !cleanup' ], + 'corona': [ + 'Pulls the latest corona virus stats per state from Johns Hopkins data', + 'Usage: !corona \neg: !corona Colorado' + ], 'decide': [ 'Dragon-bot will help make the tough decisions for you', ' If there is only one option, it will give you a yes or no', @@ -162,7 +166,7 @@ def get_help_message(method): def get_help_embed(client): categories = { 'fun': ['clap', 'redanda', 'birb', 'youtube', 'dog', 'excuse', 'greentext', 'lewd', 'message', 'meme', 'homepage', 'pout', 'roll', 'smug', 'quake', 'wink',], - 'util': ['8ball', 'decide', 'info', 'icon', 'wallpaper', 'weather', 'stock', 'tts', 'issue'], + 'util': ['corona', '8ball', 'decide', 'info', 'icon', 'wallpaper', 'weather', 'stock', 'tts', 'issue'], 'users': ['help', 'invite', 'purge', 'roles', 'source', 'minecraft'], 'admin': ['emoji', 'cleanup'] }