diff --git a/covid_bot.py b/covid_bot.py index 26ad7b4..f9174da 100644 --- a/covid_bot.py +++ b/covid_bot.py @@ -1,10 +1,11 @@ #!/bin/python - -from lib.covidBot import covidBot +from discord.ext import commands +from lib.covidBot import add_commands if(__name__ == '__main__'): + bot = commands.Bot(command_prefix='!') with open('.keys/bot_api.key') as key: discord_api_key = key.readline().strip() - client = covidBot() - client.run(discord_api_key) + add_commands(bot) + bot.run(discord_api_key) diff --git a/lib/covidBot.py b/lib/covidBot.py index 4c94f6a..2aaa0e8 100644 --- a/lib/covidBot.py +++ b/lib/covidBot.py @@ -1,23 +1,33 @@ -from discord import Client +from discord.ext import commands from re import match -from lib.parse_data import update_data, get_covid_data, covid_db +from lib.parse_data import update_data, get_covid_data, covid_db, get_top_data -class covidBot(Client): - async def on_ready(self): - print('Logged on as', self.user) +@commands.command() +async def ping(ctx): + await ctx.send('pong') - async def on_message(self, message): - # don't respond to ourselves - if(message.author == self.user): - return - if(message.content == '!ping'): - await message.channel.send('pong') +@commands.command() +async def report(ctx, arg): + if(arg): + if(arg == 'KEYS'): + await ctx.send(covid_db.keys()) + else: + await ctx.send(get_covid_data(arg.upper())) - report_match = match(r'(!report )([a-zA-Z\-\_\. ]+)', message.content) - if(report_match): - if(report_match.group(2).upper() == 'KEYS'): - await message.channel.send(covid_db.keys()) - else: - await message.channel.send(get_covid_data(report_match.group(2).upper())) + +@commands.command() +async def top(ctx, arg='5'): + try: + num = int(arg) + except Exception as e: + await ctx.send(f'{arg} isn\'t a number.') + return + await ctx.send(get_top_data(num)) + + +def add_commands(bot): + bot.add_command(ping) + bot.add_command(report) + bot.add_command(top) diff --git a/lib/covidData.py b/lib/covidData.py index ec42fb3..f74ebf1 100755 --- a/lib/covidData.py +++ b/lib/covidData.py @@ -1,6 +1,3 @@ -#!/usr/bin/python - - class covidData(): def __init__(self, selection='', import_data=[]): super().__init__() @@ -36,4 +33,4 @@ class covidData(): output = '' for key in self.data.keys(): output += f'{key}: {self.data[key]}\n' - return output + return output \ No newline at end of file diff --git a/lib/covid_report_lib.py b/lib/covid_report_lib.py index 76d0aa6..5a6f082 100644 --- a/lib/covid_report_lib.py +++ b/lib/covid_report_lib.py @@ -1,9 +1,5 @@ #!/bin/python - -try: - from parse_data import update_data, get_covid_data -except: - from lib.parse_data import update_data, get_covid_data +from lib.parse_data import update_data, get_covid_data from datetime import datetime from pytz import timezone from asyncio import sleep diff --git a/lib/parse_data.py b/lib/parse_data.py index 18ae751..fc5750c 100755 --- a/lib/parse_data.py +++ b/lib/parse_data.py @@ -2,14 +2,14 @@ import requests from bs4 import BeautifulSoup -try: - from covidData import covidData -except: - from lib.covidData import covidData +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/') @@ -32,5 +32,29 @@ def get_covid_data(selection): 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())