-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
70 lines (54 loc) · 2.07 KB
/
tracker.py
File metadata and controls
70 lines (54 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# selenium is picked because of the dynamically loaded content
# otherwise, you would only get the page source and not the content the javascript loads
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from dotenv import load_dotenv
import os
load_dotenv()
EMAIL = os.getenv('EMAIL')
PASSWORD = os.getenv('PASSWORD')
import smtplib
from email.mime.text import MIMEText
op = webdriver.ChromeOptions()
op.add_argument('headless')
op.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')
driver = webdriver.Chrome(options=op)
# kirkland irons
# https://www.costco.com/kirkland-signature-7-piece-players-iron-set%2C-right-handed.product.4000236767.html
# kirkland putter
# https://www.costco.com/kirkland-signature-ks1-putter---right-handed-.product.100645850.html
# change link here
link = "https://www.costco.com/kirkland-signature-7-piece-players-iron-set%2C-right-handed.product.4000236767.html"
# change your email to here
recipients = ['']
# change the message here
msg = ''
driver.get(link)
try:
element = WebDriverWait(driver, 5).until(
EC.presence_of_element_located((By.ID, "add-to-cart-btn"))
)
button_html = element.get_attribute('outerHTML')
out_of_stock = 'value="Out of Stock"'
in_stock = 'value="Add to Cart"'
if out_of_stock in button_html:
print("out of stock")
elif in_stock in button_html:
send_email("in_stock", msg)
else:
send_email("error", "incorrect link")
except Exception as e:
send_email("error", e)
finally:
driver.quit()
def send_email(subject, body):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = EMAIL
msg['To'] = ', '.join(recipients)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp_server:
smtp_server.login(EMAIL, PASSWORD)
smtp_server.sendmail(EMAIL, recipients, msg.as_string())
print("Message sent!")