-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·104 lines (84 loc) · 3.03 KB
/
main.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
import os
from dotenv import load_dotenv
load_dotenv()
def check_info(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
title = 'Item name not found'
price = 'Price not found'
in_stock = False
# title
title_element = soup.find('h1', class_='product-template__body--title')
if title_element:
title = title_element.text.strip()
# price
price_element = soup.find('div', class_='costco-price')
if price_element:
price_span = price_element.find('span')
price = price_span.text.strip()
# stock
stock_element = soup.select_one('.quantity-add-to-cart input[type="submit"]')
if stock_element:
stock_status = stock_element['value']
in_stock = 'add to cart' in stock_status.lower()
return {
'title': title,
'price': price,
'stock': in_stock
}
def send_email(subject, body, to_email):
from_email = os.getenv('FROM_EMAIL_ADDRESS')
if not from_email:
raise ValueError('FROM_EMAIL_ADDRESS environment variable not set.')
password = os.getenv('FROM_EMAIL_PASSWORD')
if not password:
raise ValueError('FROM_EMAIL_PASSWORD environment variable not set.')
msg = MIMEText(body, 'html')
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
smtp_server = os.getenv('SMTP_SERVER')
if not smtp_server:
raise ValueError('SMTP_SERVER environment variable not set.')
smtp_port = os.getenv('SMTP_PORT')
if not smtp_port:
raise ValueError('SMTP_PORT environment variable not set.')
try:
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(from_email, password)
server.sendmail(from_email, to_email, msg.as_string())
print('Email sent successfully!')
except smtplib.SMTPAuthenticationError as e:
print(f'SMTPAuthenticationError: {e}')
except Exception as e:
print(f'Error: {e}')
def main():
update = False
# add product URLs here
urls = [
'https://costco.bysophieofficial.com/products/dr-althea-345-relief-cream-50ml',
]
to_email = os.getenv('TO_EMAIL_ADDRESS')
if not to_email:
raise ValueError('TO_EMAIL_ADDRESS environment variable not set.')
body = ''
for url in urls:
info = check_info(url)
if info['stock']:
body += f'<ul><a href="{url}">{info["title"]}</a> is now in stock for {info["price"]}</ul>'
update = True
else:
body += f'<ul><a href="{url}">{info["title"]}</a> is still not in stock</ul>'
if update:
subject = f'Sophie Alert'
body += f'<ul>Please remember to remove items from the tracking list to stop receiving emails about in-stock items</ul>'
send_email(subject, body, to_email)
else:
print('No new updates.')
if __name__ == '__main__':
main()