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

86 lines
3.1 KiB
Python
Executable file

#!/usr/bin/python
import requests
from bs4 import BeautifulSoup
import json
from os.path import exists
from lib.covid_data_lib import init_database, set_data, get_formatted_data
# temporary database
covid_db = {}
# San Antonio url
sa_data_url = 'https://www.sanantonio.gov/health/news/alerts/coronavirus'
def import_config(path='config/config.json'):
if(exists(path)):
try:
with open(path) as config_file:
config_dict = json.load(config_file)
except Exception as e:
print(f'There was some issue opening and loading the config.\n{e}')
exit(1)
else:
print('Didn\'t find the config file.')
exit(1)
return config_dict
def update_data():
try:
print('Creating session.')
session = init_database(import_config())
except Exception as e:
session.rollback()
print(f'There was an error trying to create a database session:\n{e}')
# exit(1)
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')
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()] = set_data(
session, 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()] = set_data(
session, row.findAll('td')[0].text.replace(':', ''), [r.text for r in row.findAll('td')])
def get_covid_data(selection):
print('Updating data.')
update_data()
return get_formatted_data(init_database(import_config()), selection)
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
if(__name__ == '__main__'):
print(get_covid_data())