Refactored to functional style to increase reusability
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -163,3 +163,5 @@ pip-selfcheck.json
|
|||||||
*.code-workspace
|
*.code-workspace
|
||||||
|
|
||||||
# End of https://www.toptal.com/developers/gitignore/api/python,vscode,venv
|
# End of https://www.toptal.com/developers/gitignore/api/python,vscode,venv
|
||||||
|
|
||||||
|
sent.txt
|
||||||
13
requirements.txt
Normal file
13
requirements.txt
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
autopep8==1.5.5
|
||||||
|
certifi==2020.12.5
|
||||||
|
chardet==4.0.0
|
||||||
|
idna==2.10
|
||||||
|
pycodestyle==2.6.0
|
||||||
|
PyJWT==1.7.1
|
||||||
|
python-dotenv==0.15.0
|
||||||
|
pytz==2021.1
|
||||||
|
requests==2.25.1
|
||||||
|
six==1.15.0
|
||||||
|
toml==0.10.2
|
||||||
|
twilio==6.53.0
|
||||||
|
urllib3==1.26.3
|
||||||
90
scrape.py
90
scrape.py
@@ -4,43 +4,73 @@ import os
|
|||||||
from sys import exit
|
from sys import exit
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
import logging
|
||||||
|
|
||||||
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'
|
api_url = 'https://www.boden.co.uk/api/stock/v1/styles?market=UK&language=en-gb&id=y1357'
|
||||||
website_url = 'https://www.boden.co.uk/en-gb/rainbow-3-in-1-printed-jacket/sty-y1357'
|
item_id = '40263442' # 40263447 low stock, 40263442 12-18m
|
||||||
# 40263447 low stock
|
|
||||||
# 40263442 12-18m
|
|
||||||
item_id = '40263442'
|
|
||||||
message_sent_file = './sent.txt'
|
message_sent_file = './sent.txt'
|
||||||
|
msg_to = '+447557447469'
|
||||||
|
website_url = 'https://www.boden.co.uk/en-gb/rainbow-3-in-1-printed-jacket/sty-y1357'
|
||||||
|
|
||||||
|
|
||||||
if os.path.isfile(message_sent_file):
|
def get_logger(log_level=logging.WARN):
|
||||||
print("We have already sent SMS")
|
log = logging.getLogger(__name__)
|
||||||
exit()
|
log.setLevel(logging.WARN)
|
||||||
|
log_handler = logging.StreamHandler()
|
||||||
|
log_handler.setFormatter(logging.Formatter('[%(filename)s:(%(lineno)d)] %(levelname)s >> %(message)s'))
|
||||||
|
log.addHandler(log_handler)
|
||||||
|
|
||||||
r = requests.get(api_url)
|
return log
|
||||||
j = r.json()
|
|
||||||
|
|
||||||
for id in j['Y1357']:
|
|
||||||
if id['edp'] == item_id:
|
|
||||||
if id['stockStatus'] == 'OutOfStock':
|
|
||||||
print(f"Item is out of stock")
|
|
||||||
else:
|
|
||||||
print(f"Item is in stock")
|
|
||||||
# Your Account Sid and Auth Token from twilio.com/console
|
|
||||||
# and set the environment variables. See http://twil.io/secure
|
|
||||||
account_sid = os.environ['TWILIO_ACCOUNT_SID']
|
|
||||||
auth_token = os.environ['TWILIO_AUTH_TOKEN']
|
|
||||||
client = Client(account_sid, auth_token)
|
|
||||||
|
|
||||||
client = Client(account_sid, auth_token)
|
def get_json_from_url(url: str) -> dict:
|
||||||
|
r = requests.get(url)
|
||||||
message = client.messages.create(
|
return r.json()
|
||||||
messaging_service_sid='MG18978bd18247c16cc39d743251106813',
|
|
||||||
body='The coat is in stock at ' + website_url,
|
|
||||||
to='+447557447469'
|
def send_sms(to: str, msg: str):
|
||||||
)
|
account_sid = os.environ['TWILIO_ACCOUNT_SID']
|
||||||
|
auth_token = os.environ['TWILIO_AUTH_TOKEN']
|
||||||
print(message.sid)
|
messaging_sid = os.environ['TWILIO_MESSAGE_SID']
|
||||||
Path(message_sent_file).touch()
|
client = Client(account_sid, auth_token)
|
||||||
|
|
||||||
|
message = client.messages.create(
|
||||||
|
messaging_service_sid=messaging_sid,
|
||||||
|
body=msg,
|
||||||
|
to=to
|
||||||
|
)
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
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 action_taken(lock_file):
|
||||||
|
Path(lock_file).touch()
|
||||||
|
|
||||||
|
def main(log):
|
||||||
|
# This script runs once, to avoid spamming SMS every time run
|
||||||
|
stop_if_previous_action(message_sent_file)
|
||||||
|
|
||||||
|
j = get_json_from_url(api_url)
|
||||||
|
|
||||||
|
for id in j['Y1357']:
|
||||||
|
if id['edp'] == item_id:
|
||||||
|
if id['stockStatus'] == 'OutOfStock':
|
||||||
|
log.info(f"Item is out of stock")
|
||||||
|
else:
|
||||||
|
log.info(f"Item is in stock")
|
||||||
|
send_sms(msg_to, 'The coat is in stock at ' + website_url)
|
||||||
|
|
||||||
|
# Prevents it being run again
|
||||||
|
action_taken(message_sent_file)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
log = get_logger(logging.WARN)
|
||||||
|
main(log)
|
||||||
Reference in New Issue
Block a user