-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
94 lines (82 loc) · 3.33 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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import requests
import json
import time
from datetime import datetime
import random
#configuring selenium
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
PATH = "C:\Program Files (x86)\chromedriver.exe"
#array of bools to track what sizes are instock
lastKnownStock = []
#function to check stock of item
def checkStock(url):
driver = webdriver.Chrome(PATH, options=options)
driver.get(url)
elements = driver.find_elements_by_name("skuAndSize")
sizes = driver.find_elements_by_class_name("css-xf3ahq")
name = driver.find_element_by_id("pdp_product_title")
sizeList = []
newStock = ["**" + name.get_attribute("innerHTML") + "**"]
for i in sizes:
sizeList.append(i.get_attribute("innerHTML"))
lastKnownStock.append(False)
if driver.current_url == url:
counter = 0
restockCount = 0
for i in elements:
try:
if elements[counter].is_enabled():
if lastKnownStock[counter] != True:
lastKnownStock[counter] = True
newStock.append(sizeList[counter])
restockCount += 1
else:
if lastKnownStock[counter] == True:
lastKnownStock[counter] = False
except:
pass
counter += 1
driver.quit()
if restockCount > 0:
print(restockCount, "new restock(s) detected - check Discord")
return newStock
#function to send embed to discord webhook
def sendWebhook(urlInp, titleInp, descriptionInp):
url = urlInp
data = {}
data["content"] = ""
data["username"] = "Restock Monitor"
data["embeds"] = []
embed = {}
embed["description"] = descriptionInp
embed["title"] = titleInp
data["embeds"].append(embed)
result = requests.post(url, data=json.dumps(data), headers={"Content-Type": "application/json"})
try:
result.raise_for_status()
except requests.exceptions.HTTPError as err:
print(err)
#infinite loop - checks for restocks every 2-3 hours
print(""" _ ___ __ ____ __ __ __ ___ _ __
/ | / (_) /_____ / __ \___ _____/ /_____ _____/ /__ / |/ /___ ____ (_) /_____ _____
/ |/ / / //_/ _ \ / /_/ / _ \/ ___/ __/ __ \/ ___/ //_/ / /|_/ / __ \/ __ \/ / __/ __ \/ ___/
/ /| / / ,< / __/ / _, _/ __(__ ) /_/ /_/ / /__/ ,< / / / / /_/ / / / / / /_/ /_/ / /
/_/ |_/_/_/|_|\___/ /_/ |_|\___/____/\__/\____/\___/_/|_| /_/ /_/\____/_/ /_/_/\__/\____/_/ """)
url = input("\n\nPlease input the url to check: ")
webhook = input("Please input webhook for restock alerts: ")
runForever = True
while runForever == True:
detectedStock = checkStock(url)
if len(detectedStock) > 1:
embedDescription = ""
for i in detectedStock:
embedDescription = embedDescription +"\n" + i
sendWebhook(webhook, "Restock Detected", embedDescription)
print("Last checked at", datetime.now())
wait = random.randint(7200, 10800)
time.sleep(wait)