From 527723d605867f59ed3eed4baebe8da1ee2d219d Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Wed, 25 Mar 2020 08:12:59 -0500 Subject: [PATCH] Initial commit. --- .gitignore | 7 +++++++ Pipfile | 18 ++++++++++++++++++ README.md | 3 +++ main.py | 29 +++++++++++++++++++++++++++++ parse_data.py | 31 +++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 .gitignore create mode 100644 Pipfile create mode 100644 README.md create mode 100644 main.py create mode 100644 parse_data.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..26c8bf8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.vscode/ +.idea/ +**/__pycache__/** +**.pyc +Pipfile.lock +.keys/ +**/.credentials diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..a0f52bd --- /dev/null +++ b/Pipfile @@ -0,0 +1,18 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] +pipfile = "*" +autopep8 = "*" +pylint = "*" + +[packages] +pipfile = "*" +requests = "*" +discord = "*" +beautifulsoup4 = "*" + +[required] +python = "*" # Replace this with your project's stable Python version diff --git a/README.md b/README.md new file mode 100644 index 0000000..8413198 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# COVID-19 Discord Bot + +It's just reporting some numbers and stats from https://www.worldometers.info/coronavirus/ \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..b22b035 --- /dev/null +++ b/main.py @@ -0,0 +1,29 @@ +#!/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()) diff --git a/parse_data.py b/parse_data.py new file mode 100644 index 0000000..b42a60f --- /dev/null +++ b/parse_data.py @@ -0,0 +1,31 @@ +#!/usr/bin/python + +import requests +from bs4 import BeautifulSoup + + +def get_current_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) + table = parsed_html.find('table', id='main_table_countries_today') + all_rows = table.findAll('tr') + for row in all_rows: + if(row and row.findAll('td') and row.find('a')): + all_cols = row.findAll('td') + if(row.find('a').text == 'USA'): + return f''' + {row.find('a').text} + Total Cases: {all_cols[1].text} + New Cases: {all_cols[2].text} + Total Deaths: {all_cols[3].text} + New Deaths: {all_cols[4].text} + Total Recovered: {all_cols[5].text} + Active Cases: {all_cols[6].text} + Serious/Critical: {all_cols[7].text} + Total Cases/1M Population: {all_cols[8].text} + ''' + + +if(__name__ == '__main__'): + print(get_current_data())