From b58143fdf3248df544c5a6642f2cfdf4ad14346a Mon Sep 17 00:00:00 2001 From: Alex Huddleston Date: Sat, 28 Mar 2020 19:47:46 -0500 Subject: [PATCH] Moved most configuration settings out from being hardcoded to a JSON formatted config file and included a template. --- .gitignore | 3 +-- Dockerfile | 1 + Dockerfile_report | 1 + covid_bot.py | 6 +++--- covid_report.py | 7 ++++--- lib/covidBot.py | 2 +- lib/covid_report_lib.py | 21 +++++++++++---------- lib/parse_data.py | 25 ++++++++++++++++++++++--- template_config.json | 8 ++++++++ 9 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 template_config.json diff --git a/.gitignore b/.gitignore index 26c8bf8..a49c0c0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ **/__pycache__/** **.pyc Pipfile.lock -.keys/ -**/.credentials +config.json diff --git a/Dockerfile b/Dockerfile index d1921ba..31a9b8c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -29,5 +29,6 @@ RUN pip install pipenv RUN pipenv install Pipfile COPY covid_bot.py covid_bot.py +COPY config.json config.json ENTRYPOINT [ "pipenv", "run", "python", "covid_bot.py" ] \ No newline at end of file diff --git a/Dockerfile_report b/Dockerfile_report index 05809e5..51bc983 100644 --- a/Dockerfile_report +++ b/Dockerfile_report @@ -29,5 +29,6 @@ RUN pip install pipenv RUN pipenv install Pipfile COPY covid_report.py covid_report.py +COPY config.json config.json ENTRYPOINT [ "pipenv", "run", "python", "covid_report.py" ] \ No newline at end of file diff --git a/covid_bot.py b/covid_bot.py index f9174da..735a632 100644 --- a/covid_bot.py +++ b/covid_bot.py @@ -1,11 +1,11 @@ #!/bin/python from discord.ext import commands from lib.covidBot import add_commands +from lib.parse_data import import_config if(__name__ == '__main__'): bot = commands.Bot(command_prefix='!') - with open('.keys/bot_api.key') as key: - discord_api_key = key.readline().strip() + config_dict = import_config() add_commands(bot) - bot.run(discord_api_key) + bot.run(config_dict['api_key']) diff --git a/covid_report.py b/covid_report.py index d9d35a3..9a625bf 100644 --- a/covid_report.py +++ b/covid_report.py @@ -2,11 +2,12 @@ from discord import Client from lib.covid_report_lib import background_task +from lib.parse_data import import_config if(__name__ == '__main__'): - with open('.keys/bot_api.key') as key: - discord_api_key = key.readline().strip() + config_dict = import_config() + discord_api_key = config_dict['api_key'] client = Client() - client.loop.create_task(background_task(client)) + client.loop.create_task(background_task(client, config_dict)) client.run(discord_api_key) diff --git a/lib/covidBot.py b/lib/covidBot.py index d7d778d..7e9ee1d 100644 --- a/lib/covidBot.py +++ b/lib/covidBot.py @@ -1,6 +1,6 @@ from discord.ext import commands from re import match -from lib.parse_data import update_data, get_covid_data, covid_db, get_top_data +from lib.parse_data import * @commands.command() diff --git a/lib/covid_report_lib.py b/lib/covid_report_lib.py index 9404d66..0b31f35 100644 --- a/lib/covid_report_lib.py +++ b/lib/covid_report_lib.py @@ -3,19 +3,20 @@ from lib.parse_data import update_data, get_covid_data, get_top_data from datetime import datetime from pytz import timezone from asyncio import sleep +from math import floor -async def background_task(client): +async def background_task(client, config_dict): await client.wait_until_ready() - with open('.keys/report_channel_id') as cid: - channel = client.get_channel(int(cid.readline().strip())) + channel = client.get_channel(config_dict['report_channel_id']) while not client.is_closed(): current_hour_and_minute = [int(t) for t in str( - datetime.now(timezone('US/Central')).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 channel.send(get_top_data(5)) + datetime.now(timezone(config_dict['report_timezones'])).time()).split(':')[:-1]] + for rt in config_dict['report_times']: + if(current_hour_and_minute[0] == floor(rt/100) and (current_hour_and_minute[1] == rt % 100)): + update_data() + await channel.send('Daily report from: https://www.worldometers.info/coronavirus/') + for selection in config_dict['report_selections']: + await channel.send(get_covid_data(selection)) + await channel.send(get_top_data(config_dict['report_top_number'])) await sleep(60) diff --git a/lib/parse_data.py b/lib/parse_data.py index fc5750c..8343ea8 100755 --- a/lib/parse_data.py +++ b/lib/parse_data.py @@ -3,6 +3,8 @@ import requests from bs4 import BeautifulSoup from lib.covidData import covidData +import json +from os.path import exists # temporary database covid_db = {} @@ -11,6 +13,20 @@ covid_db = {} sa_data_url = 'https://www.sanantonio.gov/health/news/alerts/coronavirus' +def import_config(path='config.json'): + if(exists(path)): + try: + with open(path) as config_file: + config_dict = json.load(config_file) + except Exception as e: + print(f'There was some issue opening and loading the config.\n{e}') + exit(1) + else: + print('Didn\'t find the config file.') + exit(1) + return config_dict + + def update_data(): data_html = requests.get('https://www.worldometers.info/coronavirus/') if(data_html.status_code == '200' or data_html.status_code == 200): @@ -39,12 +55,15 @@ def get_top_data(number): 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(',', '')) + 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]] + 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: diff --git a/template_config.json b/template_config.json new file mode 100644 index 0000000..25f2b6c --- /dev/null +++ b/template_config.json @@ -0,0 +1,8 @@ +{ + "api_key": "https://discordapp.com/developers/docs/intro", + "report_channel_id": 0, + "report_timezones":"US/Central", + "report_times": [1200, 1600, 0], + "report_selections": ["USA", "TOTAL"], + "report_top_number": 5 +}