This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
covid-19-discord-bot/lib/parse_data.py

61 lines
2.3 KiB
Python
Raw Normal View History

2020-03-25 08:12:59 -05:00
#!/usr/bin/python
import requests
from bs4 import BeautifulSoup
from lib.covidData import covidData
2020-03-25 08:12:59 -05:00
# temporary database
covid_db = {}
2020-03-25 08:12:59 -05:00
# San Antonio url
sa_data_url = 'https://www.sanantonio.gov/health/news/alerts/coronavirus'
def update_data():
2020-03-25 08:12:59 -05:00
data_html = requests.get('https://www.worldometers.info/coronavirus/')
if(data_html.status_code == '200' or data_html.status_code == 200):
parsed_html = BeautifulSoup(data_html.text, features='html.parser')
2020-03-25 08:12:59 -05:00
table = parsed_html.find('table', id='main_table_countries_today')
for row in table.findAll('tr'):
if(row and row.findAll('td')):
if(row.find('a')):
covid_db[row.find('a').text.upper()] = covidData(
row.find('a').text, [r.text for r in row.findAll('td')])
elif(row.findAll('td')[0] and row.findAll('td')[0].text):
covid_db[row.findAll('td')[0].text.replace(':', '').upper()] = covidData(
row.findAll('td')[0].text.replace(':', ''), [r.text for r in row.findAll('td')])
def get_covid_data(selection):
if(not covid_db):
update_data()
return covid_db[selection].get_formatted_data()
2020-03-25 08:12:59 -05:00
def get_top_data(number):
if(not covid_db):
update_data()
covid_db_dict = {}
for data in covid_db.keys():
try:
if(covid_db[data].data['Selection'] and covid_db[data].data['Total Cases'] and not covid_db[data].data['Selection'] == 'Total'):
covid_db_dict[covid_db[data].data['Selection'].upper()] = int(covid_db[data].data['Total Cases'].replace(',', ''))
except Exception as e:
print(e)
return f'{e} tests'
covid_db_dict_sorted = {key: value for key, value in sorted(covid_db_dict.items(), key=lambda item: item[1], reverse=True)}
top_covid_data = [(covid_db[selection].data['Selection'], covid_db[selection].data['Total Cases']) for selection in list(covid_db_dict_sorted.keys())[:number]]
output = ''
counter = number + 1
for data in top_covid_data:
output += f'# {counter - number}\n'
output += f'{data[0]}: {data[1]}'
if(not counter == number):
output += '\n\n'
counter += 1
return output
2020-03-25 08:12:59 -05:00
if(__name__ == '__main__'):
print(get_covid_data())