-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsite_monitor.py
More file actions
106 lines (78 loc) · 3.07 KB
/
website_monitor.py
File metadata and controls
106 lines (78 loc) · 3.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
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
105
106
# %%
import time
import yaml
from chromedriver_py import binary_path
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
from twilio.rest import Client
# %%
def set_chrome_options():
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
return chrome_options
def get_avail_string(
driver,
url="https://www.lenovo.com/us/en/laptops/legion-laptops/legion-7-series/Legion-7-16ITH6/p/LEN101G0002",
):
driver.get(url)
# check if item is temporarily unavailable
try:
check_avail = driver.find_element_by_xpath(
"//*[contains(text(), 'TEMPORARILY UNAVAILABLE')]"
)
except NoSuchElementException:
check_avail = None
return check_avail
def check_cart_button(
driver,
url="https://www.lenovo.com/us/en/laptops/legion-laptops/legion-7-series/Legion-7-16ITH6/p/LEN101G0002",
):
driver.get(url)
l7_cart = driver.find_element_by_xpath(
'//*[@id="addToCartFormTop82K60004US"]/button'
)
l7_cart_flag = l7_cart.is_enabled()
return l7_cart_flag
def message_avail(twilio_creds):
account_sid = twilio_creds["project_info"]["account_sid"]
auth_token = twilio_creds["project_info"]["auth_token"]
client = Client(account_sid, auth_token)
message = client.messages.create(
body="The Legion 7 is available now! \n Go buy it at: https://www.lenovo.com/us/en/laptops/legion-laptops/legion-7-series/Legion-7-16ITH6/p/LEN101G0002",
from_=twilio_creds["project_info"]["phone_number"],
to="+15852811928",
)
def message_unavail(twilio_creds):
account_sid = twilio_creds["project_info"]["account_sid"]
auth_token = twilio_creds["project_info"]["auth_token"]
client = Client(account_sid, auth_token)
message = client.messages.create(
body="The Legion 7 is NOT available yet",
from_=twilio_creds["project_info"]["phone_number"],
to="+15852811928",
)
def message_maybe(twilio_creds):
account_sid = twilio_creds["project_info"]["account_sid"]
auth_token = twilio_creds["project_info"]["auth_token"]
client = Client(account_sid, auth_token)
message = client.messages.create(
body="The Legion 7 may be available now! \n Manually check it at: https://www.lenovo.com/us/en/laptops/legion-laptops/legion-7-series/Legion-7-16ITH6/p/LEN101G0002",
from_=twilio_creds["project_info"]["phone_number"],
to="+15852811928",
)
# %%
if __name__ == "__main__":
while True:
chrome_options = set_chrome_options()
driver = webdriver.Chrome(executable_path=binary_path, options=chrome_options)
add_to_cart = check_cart_button(driver)
driver.quit()
twilio_creds = yaml.load(open("secrets.yml"), Loader=yaml.FullLoader)
if add_to_cart:
message_avail(twilio_creds)
else:
message_unavail(twilio_creds)
time.sleep(3600)