From 8701aadfab9b02e4d8f25a071ffddd67a9a23903 Mon Sep 17 00:00:00 2001 From: Andy Wu Date: Fri, 7 Oct 2022 16:41:11 -0400 Subject: [PATCH 1/3] Updated README --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c9964ff..d5b95a3 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,14 @@ A Python script that helps check the online status of a list of user-provided we ## 2. Permanently 1. Additionally install the dotenv module with `pip install python-dotenv` 2. Modify the `config_template.json` file replacing the provided placeholders with your own data in the same format. - 1. You can rename the file as you wish, just make sure to pass the correct name when calling the script. + 1. The necessary configuration variables are: + 1. "recipients": List of strings representing email addresses to send status updates to + 2. "urls": List of strings representing website urls following same format as mentioned above + 3. "emails_fail_only": Boolean representing whether to send emails only if a status check fails. + 1. true indicates that you want the server to send an email when at least one status check fails. + 2. This can be used with a lower "check_interval_secs" to prevent a flood of emails while also having the status check run constantly. + 4. "check_interval_secs": Integer representing the number of seconds to delay another round of status checks. + 2. You can rename the file as you wish, just make sure to pass the correct name when calling the script. 3. Create a .env file with your gmail address and [APP PASSWORD](https://support.google.com/accounts/answer/185833?hl=en) 1. If you want to use a different mailing client, you will have to modify `perm_server_status` with the client's required configuration. From 5a3120493204c9c8c0de9fc5ae2d1a146ba453c2 Mon Sep 17 00:00:00 2001 From: Andy Wu Date: Fri, 7 Oct 2022 19:15:15 -0400 Subject: [PATCH 2/3] Added checks for missing configuration variables --- perm_server_status.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/perm_server_status.py b/perm_server_status.py index 8328a1e..ed7f5f5 100644 --- a/perm_server_status.py +++ b/perm_server_status.py @@ -85,6 +85,11 @@ def run(config_data: Config_Data): downtime = config_data.get('check_interval_secs') emails_on_fail_only = config_data.get('emails_fail_only') + if None in [recipients, urls, downtime, emails_on_fail_only]: + print('A key is missing from your configuration!') + print('Please make sure you have all keys as shown in the configuration template.') + return None + while True: url_statuses = [] for url in urls: @@ -101,8 +106,11 @@ def run(config_data: Config_Data): def parseConfigData(filepath: str): - with open(filepath) as f: - data = json.load(f) + try: + with open(filepath) as f: + data = json.load(f) + except (FileNotFoundError, json.JSONDecodeError) as e: + raise e return data @@ -118,5 +126,10 @@ def parseConfigData(filepath: str): except IndexError: sys.exit("A filepath must be provided for the location of your configuration file!") - config_json = parseConfigData(config_path) - run(config_json) + try: + config_json = parseConfigData(config_path) + run(config_json) + except json.JSONDecodeError: + print("Error in parsing JSON. Please check your file for syntax errors!") + except FileNotFoundError: + print("The filename you entered does not exist!") From 39a92d1b62715c5710756b3daf8f066ebfde89b7 Mon Sep 17 00:00:00 2001 From: Andy Wu Date: Fri, 7 Oct 2022 19:26:46 -0400 Subject: [PATCH 3/3] Updated ENV variable logic with error handling --- perm_server_status.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/perm_server_status.py b/perm_server_status.py index ed7f5f5..45c7680 100644 --- a/perm_server_status.py +++ b/perm_server_status.py @@ -16,6 +16,16 @@ Config_Data = TypedDict('Config_Data', {'recipients': List[str], 'urls': List[str], 'emails_fail_only': bool, 'check_interval_secs': int}) +ENV_EMAIL = 'USER_EMAIL' +ENV_PASS = 'APP_PASSWORD' +user_email = os.getenv(ENV_EMAIL) +# Most likely necessary if using gmail or any other highly secure mailing client +app_password = os.getenv(ENV_PASS) + +if not user_email or not app_password: + print(f'Set up your .env file with an email and password set to {ENV_EMAIL} and {ENV_PASS}, respectively') + sys.exit() + def is_site_running(site_url: str): """Ping desired site @@ -116,10 +126,6 @@ def parseConfigData(filepath: str): if __name__ == "__main__": - user_email = os.getenv('USER_EMAIL') - # Most likely necessary if using gmail or any other highly secure mailing client - app_password = os.getenv('APP_PASSWORD') - # Read in command line argument for config file path try: config_path = sys.argv[1]