Last active
December 29, 2023 07:07
-
-
Save luckylittle/3db9f28ce323c1a883dd9d995075aa7f to your computer and use it in GitHub Desktop.
Officeworks price checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import smtplib | |
import time | |
import requests | |
import sys | |
import random | |
import json | |
from bs4 import BeautifulSoup | |
# chmod x officeworks.py | |
# python3 -m venv venv | |
# source venv/bin/activate | |
# pip install --upgrade pip | |
# pip install requests beautifulsoup4 | |
class bcolors: | |
SUCCESS = "\033[92m" | |
WARNING = "\033[93m" | |
ERROR = "\033[91m" | |
ENDC = "\033[0m" | |
URL = "https://www.officeworks.com.au/shop/officeworks/p/newhaven-electric-sit-stand-desk-with-drawer-1200mm-white-oak-jbnewhawoa" | |
smtp_username = "<REMOVED>" | |
smtp_password = "<REMOVED>" | |
email_address = "<REMOVED>" | |
headers = { | |
"cache-control": "max-age=0", | |
"upgrade-insecure-requests": "1", | |
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36", | |
"sec-fetch-dest": "document", | |
"accept": "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", | |
"sec-fetch-site": "none", | |
"sec-fetch-mode": "navigate", | |
"sec-fetch-user": "?1", | |
"accept-language": "en-US,en;q=0.9,cs;q=0.8", | |
} | |
today = datetime.date.today() | |
title = "" | |
threshold = 0 | |
server = smtplib.SMTP("smtp.gmail.com", 587) | |
email = email_address | |
def check_price(): | |
global price | |
page = requests.get(URL, headers=headers) | |
soup = BeautifulSoup(page.content, "html.parser") | |
all_scripts = soup.find_all('script') | |
for script in all_scripts: | |
if 'window.__INITIAL_STATE__' in script.text: | |
data = json.loads(script.text[40:].strip()[:-1]) | |
prodAttributes = data['owProduct']['product']['attributes'] | |
for key in prodAttributes: | |
if (key['id'] == 'edlpPrice'): | |
price = key['value'] | |
if len(price) == 0: | |
print(f"{bcolors.ERROR}The item you were tracking was not found{bcolors.ENDC}") | |
send_warning_mail() | |
end_script() | |
print( | |
f"Newhaven Electric Sit Stand Desk is ${price} on {today}" | |
) | |
send_success_mail() | |
def setup_email_service(): | |
server.ehlo() | |
server.starttls() | |
server.ehlo() | |
server.login(smtp_username, smtp_password) | |
def send_email(msg): | |
server.sendmail(smtp_username, email, msg) | |
print("Email has been sent!") | |
server.quit() | |
def send_success_mail(): | |
setup_email_service() | |
subject = f"Newhaven Electric Sit Stand Desk is ${price} on {today}" | |
body = f"Buy it here: {URL}" | |
msg = f"Subject: {subject}\n\n{body}" | |
send_email(msg) | |
def send_warning_mail(): | |
setup_email_service() | |
subject = "The item you were tracking was not found" | |
body = f"Something has changed and the link {URL} is probably no longer valid" | |
msg = f"Subject: {subject}\n\n{body}" | |
send_email(msg) | |
def end_script(): | |
print("Exiting the script...") | |
sys.exit() | |
check_price() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# /etc/cron.daily/officeworks.sh | |
source /home/centos/officeworks/venv/bin/activate | |
python /home/centos/officeworks/officeworks.py >> /home/centos/officeworks/officeworks.log | |
deactivate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment