Changed bot format to match bot mode in the Discord python API. Created a top X summary report command.
This commit is contained in:
parent
d826ebcd06
commit
391def59c2
5 changed files with 62 additions and 34 deletions
|
@ -1,10 +1,11 @@
|
||||||
#!/bin/python
|
#!/bin/python
|
||||||
|
from discord.ext import commands
|
||||||
from lib.covidBot import covidBot
|
from lib.covidBot import add_commands
|
||||||
|
|
||||||
|
|
||||||
if(__name__ == '__main__'):
|
if(__name__ == '__main__'):
|
||||||
|
bot = commands.Bot(command_prefix='!')
|
||||||
with open('.keys/bot_api.key') as key:
|
with open('.keys/bot_api.key') as key:
|
||||||
discord_api_key = key.readline().strip()
|
discord_api_key = key.readline().strip()
|
||||||
client = covidBot()
|
add_commands(bot)
|
||||||
client.run(discord_api_key)
|
bot.run(discord_api_key)
|
||||||
|
|
|
@ -1,23 +1,33 @@
|
||||||
from discord import Client
|
from discord.ext import commands
|
||||||
from re import match
|
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):
|
@commands.command()
|
||||||
async def on_ready(self):
|
async def ping(ctx):
|
||||||
print('Logged on as', self.user)
|
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'):
|
@commands.command()
|
||||||
await message.channel.send('pong')
|
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):
|
@commands.command()
|
||||||
if(report_match.group(2).upper() == 'KEYS'):
|
async def top(ctx, arg='5'):
|
||||||
await message.channel.send(covid_db.keys())
|
try:
|
||||||
else:
|
num = int(arg)
|
||||||
await message.channel.send(get_covid_data(report_match.group(2).upper()))
|
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)
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
|
|
||||||
class covidData():
|
class covidData():
|
||||||
def __init__(self, selection='', import_data=[]):
|
def __init__(self, selection='', import_data=[]):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
@ -36,4 +33,4 @@ class covidData():
|
||||||
output = ''
|
output = ''
|
||||||
for key in self.data.keys():
|
for key in self.data.keys():
|
||||||
output += f'{key}: {self.data[key]}\n'
|
output += f'{key}: {self.data[key]}\n'
|
||||||
return output
|
return output
|
|
@ -1,9 +1,5 @@
|
||||||
#!/bin/python
|
#!/bin/python
|
||||||
|
from lib.parse_data import update_data, get_covid_data
|
||||||
try:
|
|
||||||
from parse_data import update_data, get_covid_data
|
|
||||||
except:
|
|
||||||
from lib.parse_data import update_data, get_covid_data
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pytz import timezone
|
from pytz import timezone
|
||||||
from asyncio import sleep
|
from asyncio import sleep
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
try:
|
from lib.covidData import covidData
|
||||||
from covidData import covidData
|
|
||||||
except:
|
|
||||||
from lib.covidData import covidData
|
|
||||||
|
|
||||||
# temporary database
|
# temporary database
|
||||||
covid_db = {}
|
covid_db = {}
|
||||||
|
|
||||||
|
# San Antonio url
|
||||||
|
sa_data_url = 'https://www.sanantonio.gov/health/news/alerts/coronavirus'
|
||||||
|
|
||||||
|
|
||||||
def update_data():
|
def update_data():
|
||||||
data_html = requests.get('https://www.worldometers.info/coronavirus/')
|
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()
|
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__'):
|
if(__name__ == '__main__'):
|
||||||
print(get_covid_data())
|
print(get_covid_data())
|
||||||
|
|
Reference in a new issue