#!/usr/bin/python import requests from bs4 import BeautifulSoup from lib.covidData import covidData # temporary database covid_db = {} # San Antonio url sa_data_url = 'https://www.sanantonio.gov/health/news/alerts/coronavirus' def update_data(): 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()] = 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() 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())