Documentation
This commit is contained in:
31
scrape.py
31
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__':
|
||||
|
||||
Reference in New Issue
Block a user