Moved constants to config dict

This commit is contained in:
2021-03-06 15:57:56 +00:00
parent 93a5150bf9
commit 5711d4c249
2 changed files with 36 additions and 26 deletions

View File

@@ -1,4 +1,6 @@
{ {
"python.pythonPath": "bin/python3", "python.pythonPath": "bin/python3",
"python.envFile": "${workspaceFolder}/.env" "python.envFile": "${workspaceFolder}/.env",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true
} }

View File

@@ -1,36 +1,40 @@
import requests
from twilio.rest import Client
import os import os
from sys import exit from sys import exit
from pathlib import Path from pathlib import Path
from dotenv import load_dotenv
import logging import logging
import requests
from dotenv import load_dotenv
from twilio.rest import Client
load_dotenv(verbose=True) load_dotenv(verbose=True)
api_url = 'https://www.boden.co.uk/api/stock/v1/styles?market=UK&language=en-gb&id=y1357' config = {
item_id = '40263442' # 40263447 low stock, 40263442 12-18m 'api_url': 'https://www.boden.co.uk/api/stock/v1/styles?market=UK&language=en-gb&id=y1357',
message_sent_file = './sent.txt' 'item_id': '40263442', # 40263447 low stock, 40263442 12-18m
msg_to = '+447557447469' 'message_sent_file': './sent.txt',
website_url = 'https://www.boden.co.uk/en-gb/rainbow-3-in-1-printed-jacket/sty-y1357' 'msg_to': '+447557447469',
'website_url': 'https://www.boden.co.uk/en-gb/rainbow-3-in-1-printed-jacket/sty-y1357',
'log_level': logging.WARN
}
def get_logger(log_level=logging.WARN): def get_logger(log_level=logging.WARN):
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.setLevel(logging.WARN) log.setLevel(log_level)
log_handler = logging.StreamHandler() log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter('[%(filename)s:(%(lineno)d)] %(levelname)s >> %(message)s')) log_handler.setFormatter(logging.Formatter(
'[%(filename)s:(%(lineno)d)] %(levelname)s >> %(message)s'))
log.addHandler(log_handler) log.addHandler(log_handler)
return log return log
def get_json_from_url(url: str) -> dict: def get_json_from_url(url: str) -> dict:
r = requests.get(url) req = requests.get(url)
return r.json() return req.json()
def send_sms(to: str, msg: str): def send_sms(target: str, msg: str):
account_sid = os.environ['TWILIO_ACCOUNT_SID'] account_sid = os.environ['TWILIO_ACCOUNT_SID']
auth_token = os.environ['TWILIO_AUTH_TOKEN'] auth_token = os.environ['TWILIO_AUTH_TOKEN']
messaging_sid = os.environ['TWILIO_MESSAGE_SID'] messaging_sid = os.environ['TWILIO_MESSAGE_SID']
@@ -39,38 +43,42 @@ def send_sms(to: str, msg: str):
message = client.messages.create( message = client.messages.create(
messaging_service_sid=messaging_sid, messaging_service_sid=messaging_sid,
body=msg, body=msg,
to=to to=target
) )
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.debug(message.sid) log.debug(message.sid)
def stop_if_previous_action(lock_file: str): def stop_if_previous_action(lock_file: str):
if os.path.isfile(lock_file): if os.path.isfile(lock_file):
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
log.info("Script has already run it's action before") log.info("Script has already run it's action before")
exit() exit()
def action_taken(lock_file): def action_taken(lock_file):
Path(lock_file).touch() Path(lock_file).touch()
def main(log):
def main(config, log):
# This script runs once, to avoid spamming SMS every time run # This script runs once, to avoid spamming SMS every time run
stop_if_previous_action(message_sent_file) stop_if_previous_action(config['message_sent_file'])
j = get_json_from_url(api_url) json = get_json_from_url(config['api_url'])
for id in j['Y1357']: for id in json['Y1357']:
if id['edp'] == item_id: if id['edp'] == config['item_id']:
if id['stockStatus'] == 'OutOfStock': if id['stockStatus'] == 'OutOfStock':
log.info(f"Item is out of stock") log.info("Item is out of stock")
else: else:
log.info(f"Item is in stock") log.info("Item is in stock")
send_sms(msg_to, 'The coat is in stock at ' + website_url) send_sms(config['msg_to'], 'The coat is in stock at ' + config['website_url'])
# Prevents it being run again # Prevents it being run again
action_taken(message_sent_file) action_taken(config['message_sent_file'])
if __name__ == '__main__': if __name__ == '__main__':
log = get_logger(logging.WARN) log = get_logger(config['log_level'])
main(log) main(config, log)