82 lines
2.6 KiB
Python
Executable file
82 lines
2.6 KiB
Python
Executable file
#!/usr/bin/python3
|
|
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
from inspect import getmembers, isroutine
|
|
from lib.config_lib import create_session
|
|
from lib.covid_data_lib import set_data, get_data, get_top_n_rows
|
|
from lib.covidData import Base
|
|
|
|
# San Antonio url
|
|
sa_data_url = 'https://www.sanantonio.gov/health/news/alerts/coronavirus'
|
|
|
|
|
|
def format_parse_int(num):
|
|
output = ''
|
|
count = 0
|
|
while not num == 0:
|
|
if(count == 3):
|
|
output += ','
|
|
count = 0
|
|
output += f'{num%10}'
|
|
num = int(num/10)
|
|
count += 1
|
|
return output[::-1]
|
|
|
|
|
|
def update_data():
|
|
try:
|
|
print('Creating session.')
|
|
session = create_session(Base)
|
|
except Exception as e:
|
|
print(f'There was an error trying to create a database session:\n{e}')
|
|
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')):
|
|
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):
|
|
set_data(
|
|
session, row.findAll('td')[0].text.replace(':', ''), [r.text for r in row.findAll('td')])
|
|
session.close()
|
|
|
|
|
|
def get_covid_data(selection):
|
|
print('Updating data.')
|
|
session = create_session(Base)
|
|
data_dict = get_data(session, selection)
|
|
session.close()
|
|
output = f'Selection: {data_dict["selection_original"]}\n'
|
|
for key in data_dict:
|
|
temp_key = ' '.join([d.capitalize() for d in key.replace(
|
|
'per', '/').split('_')])
|
|
if(key == 'selection' or key == 'selection_original'):
|
|
pass
|
|
elif(key == 'total_cases_per_one_mil'):
|
|
output += f'{temp_key}: {data_dict[key]}\n'
|
|
else:
|
|
output += f'{temp_key}: {format_parse_int(data_dict[key])}\n'
|
|
return output
|
|
|
|
|
|
def get_top_data(number):
|
|
session = create_session(Base)
|
|
top_n_rows = get_top_n_rows(session, number + 2)
|
|
session.close()
|
|
output = ''
|
|
count = 0
|
|
for row in top_n_rows:
|
|
if(count > 1):
|
|
output += f'# {count - 1}\n{row["selection_original"]}: {format_parse_int(row["total_cases"])}'
|
|
if(not count == number + 1):
|
|
output += '\n'
|
|
count += 1
|
|
return output
|
|
|
|
|
|
if(__name__ == '__main__'):
|
|
print(get_covid_data('TOTAL'))
|