Client and bot functions had to be split into separate running files covid_bot.py and main.py, but initial promised functionality is implemented.
This commit is contained in:
parent
527723d605
commit
4fa6747e69
5 changed files with 128 additions and 48 deletions
39
covidData.py
Normal file
39
covidData.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
|
||||||
|
|
||||||
|
class covidData():
|
||||||
|
def __init__(self, selection='', import_data=[]):
|
||||||
|
super().__init__()
|
||||||
|
if(selection and len(import_data) > 8):
|
||||||
|
self.set_data(selection, import_data)
|
||||||
|
else:
|
||||||
|
self.data = {
|
||||||
|
'Selection': '',
|
||||||
|
'Total Cases': '',
|
||||||
|
'New Cases': '',
|
||||||
|
'Total Deaths': '',
|
||||||
|
'New Deaths': '',
|
||||||
|
'Total Recovered': '',
|
||||||
|
'Active Cases': '',
|
||||||
|
'Serious/Critical': '',
|
||||||
|
'Total Cases/1M Population': ''
|
||||||
|
}
|
||||||
|
|
||||||
|
def set_data(self, selection, import_data):
|
||||||
|
self.data = {
|
||||||
|
'Selection': selection,
|
||||||
|
'Total Cases': import_data[1].strip(),
|
||||||
|
'New Cases': import_data[2].strip(),
|
||||||
|
'Total Deaths': import_data[3].strip(),
|
||||||
|
'New Deaths': import_data[4].strip(),
|
||||||
|
'Total Recovered': import_data[5].strip(),
|
||||||
|
'Active Cases': import_data[6].strip(),
|
||||||
|
'Serious/Critical': import_data[7].strip(),
|
||||||
|
'Total Cases/1M Population': import_data[8].strip()
|
||||||
|
}
|
||||||
|
|
||||||
|
def get_formatted_data(self):
|
||||||
|
output = ''
|
||||||
|
for key in self.data.keys():
|
||||||
|
output += f'{key}: {self.data[key]}\n'
|
||||||
|
return output
|
37
covid_bot.py
Normal file
37
covid_bot.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
from parse_data import update_data, get_covid_data, covid_db
|
||||||
|
from re import match
|
||||||
|
from datetime import datetime
|
||||||
|
from asyncio import sleep
|
||||||
|
|
||||||
|
|
||||||
|
class MyClient(discord.Client):
|
||||||
|
async def on_ready(self):
|
||||||
|
print('Logged on as', self.user)
|
||||||
|
|
||||||
|
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')
|
||||||
|
|
||||||
|
report_match = match(r'(!report )([a-zA-Z\-\_\. ]+)', message.content)
|
||||||
|
if(report_match):
|
||||||
|
with open('.keys/report_channel_id') as cid:
|
||||||
|
channel = self.get_channel(int(cid.readline().strip()))
|
||||||
|
if(report_match.group(2).upper() == 'KEYS'):
|
||||||
|
await channel.send(covid_db.keys())
|
||||||
|
else:
|
||||||
|
await channel.send(get_covid_data(report_match.group(2).upper()))
|
||||||
|
|
||||||
|
|
||||||
|
if(__name__ == '__main__'):
|
||||||
|
with open('.keys/bot_api.key') as key:
|
||||||
|
discord_api_key = key.readline().strip()
|
||||||
|
client = MyClient()
|
||||||
|
client.run(discord_api_key)
|
31
daily_report.py
Normal file
31
daily_report.py
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
#!/bin/python
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from discord.ext import commands
|
||||||
|
from parse_data import update_data, get_covid_data, covid_db
|
||||||
|
from re import match
|
||||||
|
from datetime import datetime
|
||||||
|
from asyncio import sleep
|
||||||
|
|
||||||
|
|
||||||
|
async def background_task(client):
|
||||||
|
await client.wait_until_ready()
|
||||||
|
with open('.keys/report_channel_id') as cid:
|
||||||
|
channel = client.get_channel(int(cid.readline().strip()))
|
||||||
|
while not client.is_closed():
|
||||||
|
current_hour_and_minute = [int(t) for t in str(
|
||||||
|
datetime.now().time()).split(':')[:-1]]
|
||||||
|
if(current_hour_and_minute[0] == 12 and (current_hour_and_minute[1] == 0)):
|
||||||
|
update_data()
|
||||||
|
await channel.send('Daily report from: https://www.worldometers.info/coronavirus/')
|
||||||
|
await channel.send(get_covid_data('USA'))
|
||||||
|
await channel.send(get_covid_data('TOTAL'))
|
||||||
|
await sleep(60)
|
||||||
|
|
||||||
|
|
||||||
|
if(__name__ == '__main__'):
|
||||||
|
with open('.keys/bot_api.key') as key:
|
||||||
|
discord_api_key = key.readline().strip()
|
||||||
|
client = discord.Client()
|
||||||
|
client.loop.create_task(background_task(client))
|
||||||
|
client.run(discord_api_key)
|
29
main.py
29
main.py
|
@ -1,29 +0,0 @@
|
||||||
#!/bin/python
|
|
||||||
|
|
||||||
import discord
|
|
||||||
from discord.ext import commands
|
|
||||||
from parse_data import get_current_data
|
|
||||||
|
|
||||||
|
|
||||||
class MyClient(discord.Client):
|
|
||||||
async def on_ready(self):
|
|
||||||
print('Logged on as', self.user)
|
|
||||||
|
|
||||||
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')
|
|
||||||
|
|
||||||
if(message.content == 'report'):
|
|
||||||
with open('.keys/report_channel_id') as cid:
|
|
||||||
channel = self.get_channel(int(cid.readline().strip()))
|
|
||||||
await channel.send(get_current_data())
|
|
||||||
|
|
||||||
|
|
||||||
if(__name__ == '__main__'):
|
|
||||||
with open('.keys/bot_api.key') as key:
|
|
||||||
client = MyClient()
|
|
||||||
client.run(key.readline().strip())
|
|
|
@ -2,30 +2,32 @@
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from covidData import covidData
|
||||||
|
|
||||||
|
# temporary database
|
||||||
|
covid_db = {}
|
||||||
|
|
||||||
|
|
||||||
def get_current_data():
|
def update_data():
|
||||||
data_html = requests.get('https://www.worldometers.info/coronavirus/')
|
data_html = requests.get('https://www.worldometers.info/coronavirus/')
|
||||||
if(data_html.status_code == '200' or data_html.status_code == 200):
|
if(data_html.status_code == '200' or data_html.status_code == 200):
|
||||||
parsed_html = BeautifulSoup(data_html.text)
|
parsed_html = BeautifulSoup(data_html.text, features='html.parser')
|
||||||
table = parsed_html.find('table', id='main_table_countries_today')
|
table = parsed_html.find('table', id='main_table_countries_today')
|
||||||
all_rows = table.findAll('tr')
|
for row in table.findAll('tr'):
|
||||||
for row in all_rows:
|
if(row and row.findAll('td')):
|
||||||
if(row and row.findAll('td') and row.find('a')):
|
if(row.find('a')):
|
||||||
all_cols = row.findAll('td')
|
covid_db[row.find('a').text.upper()] = covidData(
|
||||||
if(row.find('a').text == 'USA'):
|
row.find('a').text, [r.text for r in row.findAll('td')])
|
||||||
return f'''
|
elif(row.findAll('td')[0] and row.findAll('td')[0].text):
|
||||||
{row.find('a').text}
|
covid_db[row.findAll('td')[0].text.replace(':', '').upper()] = covidData(
|
||||||
Total Cases: {all_cols[1].text}
|
row.findAll('td')[0].text.replace(':', ''), [r.text for r in row.findAll('td')])
|
||||||
New Cases: {all_cols[2].text}
|
|
||||||
Total Deaths: {all_cols[3].text}
|
|
||||||
New Deaths: {all_cols[4].text}
|
def get_covid_data(selection):
|
||||||
Total Recovered: {all_cols[5].text}
|
if(not covid_db):
|
||||||
Active Cases: {all_cols[6].text}
|
update_data()
|
||||||
Serious/Critical: {all_cols[7].text}
|
return covid_db[selection].get_formatted_data()
|
||||||
Total Cases/1M Population: {all_cols[8].text}
|
|
||||||
'''
|
|
||||||
|
|
||||||
|
|
||||||
if(__name__ == '__main__'):
|
if(__name__ == '__main__'):
|
||||||
print(get_current_data())
|
print(get_covid_data())
|
||||||
|
|
Reference in a new issue