109 lines
4.8 KiB
Python
Executable File
109 lines
4.8 KiB
Python
Executable File
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
|
|
import datetime
|
|
|
|
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()
|
|
|
|
today = datetime.datetime.utcnow().date()
|
|
todays_date = today.strftime('%m-%d-%Y')
|
|
yesterday = today - datetime.timedelta(days=1)
|
|
yesterdays_date = yesterday.strftime('%m-%d-%Y')
|
|
|
|
most_recent = next(filter(lambda x: yesterdays_date in x['name'], git_blob))
|
|
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)
|
|
|
|
two_days_ago = today - datetime.timedelta(days=2)
|
|
two_days_ago_date = two_days_ago.strftime('%m-%d-%Y')
|
|
yesterday_object = next(filter(lambda x: two_days_ago_date in x['name'], git_blob))
|
|
yesterday_filename = yesterday_object['name']
|
|
yesterday_url = yesterday_object['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()
|
|
try:
|
|
last_updated, yesterday_sum = query_csv(yesterday, location)
|
|
last_updated, today_sum = query_csv(today, location)
|
|
except TypeError:
|
|
return discord.Embed(description="No data for %s. Did you make a typo?" % location, color=0x428bca, type="rich")
|
|
|
|
|
|
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 Covid19 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)
|
|
# if 'Not available' not in get_vaccination_data(location):
|
|
# embed.add_field(name=':sparkles: :syringe: Percent vaccinated :syringe: :sparkles:', value=get_vaccination_data(location) + "\nVaccination numbers pulled from\nhttps://covidvaxcount.live", inline=False)
|
|
embed.add_field(name='Source', value="https://github.com/CSSEGISandData/COVID-19")
|
|
|
|
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)
|