From 4540458250c9cd1980f25924b8a0a39df94d4ad3 Mon Sep 17 00:00:00 2001 From: ScoopGracie Date: Thu, 14 May 2020 08:15:23 -0700 Subject: [PATCH] New COVID module Better formatting --- modules/covid.py | 81 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 63 insertions(+), 18 deletions(-) diff --git a/modules/covid.py b/modules/covid.py index 35c3959f2..b47c38662 100644 --- a/modules/covid.py +++ b/modules/covid.py @@ -3,26 +3,71 @@ ''' import web - -@web.with_scraped_page_no_cache('https://www.worldometers.info/coronavirus/') -def scrape_stats(doc): - elements = doc.find_class('maincounter-number') - return ( i.text_content().strip() for i in elements ) +def scrape_stats(url): + @web.with_scraped_page_no_cache(url) + def scrape_stats_sub(doc): + elements = doc.find_class('maincounter-number') + try: + heading = doc.find('h1').text_content().strip() + except AttributeError: + heading = 'the world' + if heading == 'Coronavirus Cases:': #on the global stats page, the first h1 is just a data heading + heading == 'the world' + return tuple(( i.text_content().strip() for i in elements )) + ( heading, ) + return scrape_stats_sub() def covid(phenny, input): - phenny.say('Loading COVID-19 statistics...') - cases, deaths, recovered = scrape_stats() - current = format(int(cases.replace(',', '')) - ( int(deaths.replace(',', '')) + int(recovered.replace(',', '')) ), ',d') - phenny.say('Cases: {}'.format(cases)) - phenny.say('Current: {}'.format(current)) - phenny.say('Deaths: {}'.format(deaths)) - phenny.say('Recovered: {}'.format(recovered)) - phenny.say('Recovery rate: {}%'.format(round(int(recovered.replace(',', ''))/int(cases.replace(',', '')) * 10000) / 100)) - phenny.say('Death rate: {}%'.format(round(int(deaths.replace(',', ''))/int(cases.replace(',', '')) * 10000) / 100)) - phenny.say('Recoveries/Deaths: {}'.format(round(int(recovered.replace(',', ''))/int(deaths.replace(',', '')) * 100 ) / 100 )) - phenny.say('This is not medical advice.') - + '''.covid [country]: COVID-19 statistics; omit [country] for global stats (if country == 'us', you can specify a state after it)''' + if input.group(1): + country = input.group(1).split()[0] + if country.lower() == 'usa': + country == 'us' + if country.lower() == 'georgia': + phenny.say('Note: this information is for the country of Georgia. US states are not supported.') + #try: + # state = input.group(1).split()[1] + #except IndexError: + # state = None + #if country.lower() == 'us' and state: + # url = 'https://www.worldometers.info/coronavirus/usa/{}'.format(state) + #else: + url = 'https://www.worldometers.info/coronavirus/country/{}'.format(country) + else: + url = 'https://www.worldometers.info/coronavirus/' + try: + cases, deaths, recovered, place = scrape_stats(url) + except ValueiError: + if 'country' in locals(): + phenny.say('Sorry, an error occurred. If there is another name for {}, try it.'.format(state if state else country)) + else: + phenny.say('Sorry, an error occurred.') + return + except Exception as e: + phenny.say('Sorry, an error occurred.') + raise e + try: + int(deaths.replace(',', '')) + except ValueError: + deaths = '10,000,000,000' #more than the world population; this will never be real data + try: + int(recovered.replace(',', '')) + except ValueError: + recovered = '10,000,000,000' + try: + int(cases.replace(',', '')) + except ValueError: + cases = '10,000,000,000' + current = format(int(cases.replace(',', '')) - ( int(deaths.replace(',', '')) + int(recovered.replace(',', '')) ), ',d') + outcome = format(int(deaths.replace(',', '')) + int(recovered.replace(',', '')), ',d') + phenny.say('{}here have been {} cases of COVID-19, including {} with an outcome ({} recoveries and {} deaths), leaving {} current cases. There have been {} recoveries for every 1 death. This is not medical advice. Source: {}'.format( + 'T' if place == 'the world' else 'In {}, t'.format(place), + '' if cases == '10,000,000,000' else cases, + '' if '10,000,000,000' in (recovered, deaths) else outcome, + '' if recovered == '10,000,000,000' else recovered, + '' if deaths == '10,000,000,000' else deaths, + '' if '10,000,000,000' in (cases, recovered, deaths) else current, + '' if '10,000,000,000' in (cases, recovered, deaths) else round(int(recovered.replace(',', ''))/int(deaths.replace(',', '')) * 100 ) / 100, url )) covid.commands = ['covid', 'covid19', 'coronavirus', '2019ncov', 'sarscov2', 'corona', 'pandemic'] -covid.example = '.covid' +covid.example = '.covid uk' covid.priority = 'medium'