From 0d9b62adcf838128d7c7b56cb1a1ecbfedcf4d66 Mon Sep 17 00:00:00 2001 From: acid Date: Sat, 6 Mar 2021 16:28:31 +0000 Subject: [PATCH] Documentation --- scrape.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/scrape.py b/scrape.py index e15397a..574b20e 100644 --- a/scrape.py +++ b/scrape.py @@ -1,5 +1,4 @@ import os -from sys import exit from pathlib import Path import logging import requests @@ -19,6 +18,8 @@ config = { def get_logger(log_level=logging.WARN): + """Creates, configures and returns a logger that outputs to console with supplied log_level + """ log = logging.getLogger(__name__) log.setLevel(log_level) log_handler = logging.StreamHandler() @@ -38,6 +39,7 @@ def send_sms(target: str, msg: str): account_sid = os.environ['TWILIO_ACCOUNT_SID'] auth_token = os.environ['TWILIO_AUTH_TOKEN'] messaging_sid = os.environ['TWILIO_MESSAGE_SID'] + client = Client(account_sid, auth_token) message = client.messages.create( @@ -50,20 +52,28 @@ def send_sms(target: str, msg: str): log.debug(message.sid) -def stop_if_previous_action(lock_file: str): - if os.path.isfile(lock_file): - log = logging.getLogger(__name__) - log.info("Script has already run it's action before") - exit() +def does_file_exist(lock_file: str) -> bool: + """Checks for presence of lockfile + + Args: + lock_file (str): path to lockfile + + Returns: + (bool): True if file exists + """ + return os.path.isfile(lock_file) -def action_taken(lock_file): +def create_file(lock_file): Path(lock_file).touch() def main(config, log): # This script runs once, to avoid spamming SMS every time run - stop_if_previous_action(config['message_sent_file']) + if does_file_exist(config['message_sent_file']): + log.info( + f'Already had positive result, remove {config["message_sent_file"]} to allow script to run again') + return json = get_json_from_url(config['api_url']) @@ -73,10 +83,11 @@ def main(config, log): log.info("Item is out of stock") else: log.info("Item is in stock") - send_sms(config['msg_to'], 'The coat is in stock at ' + config['website_url']) + send_sms( + config['msg_to'], 'The coat is in stock at ' + config['website_url']) # Prevents it being run again - action_taken(config['message_sent_file']) + create_file(config['message_sent_file']) if __name__ == '__main__':