dragon-bot/app/corona.py

97 lines
4.2 KiB
Python

from bs4 import BeautifulSoup
from pandas import read_csv
from urllib.parse import urlparse
import discord
import os
import pandas as pd
import requests
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()
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(most_recent_csv, 'wb').write(r.content)
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 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:
location_query = series.loc[series['Country_Region'] == location]
if location_query.empty:
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
return last_updated, location_query.sum()
def sum_numbers(location):
today, yesterday = get_csv()
last_updated, yesterday_sum = query_csv(yesterday, location)
last_updated, today_sum = query_csv(today, 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')
embed.add_field(name=':rotating_light: Confirmed Cases :rotating_light:', value=confirmed, inline=False)
embed.add_field(name=':hospital: Recovered Cases :hospital:', value=recovered, inline=False)
embed.add_field(name=':skull_crossbones: Deaths :skull_crossbones:', value=deaths, inline=False)
embed.add_field(name=':sparkles: :syringe: NEW!: Percent vaccinated :syringe: :sparkles:', value=get_vaccination_data(location), inline=False)
embed.add_field(name='Source', value="https://github.com/CSSEGISandData/COVID-19\nVaccination numbers pulled from\nhttps://covidvaxcount.live")
return embed
def get_vaccination_data(location):
url = requests.get('https://covidvaxcount.live')
soup = BeautifulSoup(url.content, features="html.parser")
data = []
table = soup.find("div", {"class": 'table-responsive'})
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele]) # Get rid of empty values
try:
state_name, num_vaccinated, population, percent_vaccinated = list(filter(lambda x: x[0] == location, data))[0]
except Exception:
return "Not available for %s" % location
return percent_vaccinated
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)