forked from alexschimpf/Snkrs-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
293 lines (237 loc) · 11.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import sys
import six
import pause
import argparse
import logging.config
import re
from selenium import webdriver
from dateutil import parser as date_parser
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"default": {
"format": "%(asctime)s [PID %(process)d] [Thread %(thread)d] [%(levelname)s] [%(name)s] %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "default",
"stream": "ext://sys.stdout"
}
},
"root": {
"level": "INFO",
"handlers": [
"console"
]
}
})
NIKE_HOME_URL = "https://www.nike.com/login"
LOGGER = logging.getLogger()
def run(driver, shoe_type, username, password, url, shoe_size, login_time=None, release_time=None,
page_load_timeout=None, screenshot_path=None, html_path=None, select_payment=False, purchase=False,
num_retries=None, dont_quit=False):
driver.maximize_window()
driver.set_page_load_timeout(page_load_timeout)
if login_time:
LOGGER.info("Waiting until login time: " + login_time)
pause.until(date_parser.parse(login_time))
try:
login(driver=driver, username=username, password=password)
except Exception as e:
LOGGER.exception("Failed to login: " + str(e))
six.reraise(Exception, e, sys.exc_info()[2])
if release_time:
LOGGER.info("Waiting until release time: " + release_time)
pause.until(date_parser.parse(release_time))
num_retries_attempted = 0
while True:
try:
try:
LOGGER.info("Requesting page: " + url)
driver.get(url)
except TimeoutException:
LOGGER.info("Page load timed out but continuing anyway")
try:
select_shoe_size(driver=driver, shoe_size=shoe_size, shoe_type=shoe_type)
except Exception as e:
# Try refreshing page since you can't click Buy button without selecting size
LOGGER.exception("Failed to select shoe size: " + str(e))
continue
try:
click_buy_button(driver=driver)
except Exception as e:
LOGGER.exception("Failed to click buy button: " + str(e))
six.reraise(Exception, e, sys.exc_info()[2])
if select_payment:
try:
select_payment_option(driver=driver)
except Exception as e:
LOGGER.exception("Failed to select payment option: " + str(e))
six.reraise(Exception, e, sys.exc_info()[2])
try:
click_save_button(driver=driver)
except Exception as e:
LOGGER.exception("Failed to click save button: " + str(e))
six.reraise(Exception, e, sys.exc_info()[2])
if purchase:
try:
click_submit_button(driver=driver)
except Exception as e:
LOGGER.exception("Failed to click submit button: " + str(e))
six.reraise(Exception, e, sys.exc_info()[2])
LOGGER.info("Purchased shoe")
break
except Exception:
if num_retries and num_retries_attempted < num_retries:
num_retries_attempted += 1
continue
else:
break
if screenshot_path:
LOGGER.info("Saving screenshot")
driver.save_screenshot(screenshot_path)
if html_path:
LOGGER.info("Saving HTML source")
with open(html_path, "w") as f:
f.write(driver.page_source)
if dont_quit:
LOGGER.info("Preventing driver quit...")
input("Press Enter to quit...")
driver.quit()
def login(driver, username, password):
try:
LOGGER.info("Requesting page: " + NIKE_HOME_URL)
driver.get(NIKE_HOME_URL)
except TimeoutException:
LOGGER.info("Page load timed out but continuing anyway")
LOGGER.info("Waiting for login fields to become visible")
wait_until_visible(driver=driver, xpath="//input[@name='emailAddress']")
LOGGER.info("Entering username and password")
email_input = driver.find_element_by_xpath("//input[@name='emailAddress']")
email_input.clear()
email_input.send_keys(username)
password_input = driver.find_element_by_xpath("//input[@name='password']")
password_input.clear()
password_input.send_keys(password)
LOGGER.info("Logging in")
driver.find_element_by_xpath("//input[@value='SIGN IN']").click()
wait_until_visible(driver=driver, xpath="//a[@data-path='myAccount:greeting']")
LOGGER.info("Successfully logged in")
def select_shoe_size(driver, shoe_size, shoe_type):
LOGGER.info("Waiting for size dropdown to appear")
wait_until_visible(driver, class_name="size-grid-button", duration=10)
LOGGER.info("Selecting size from dropdown")
# Get first element found text
size_text = driver.find_element_by_xpath("//li[@data-qa='size-available']/button").text
# Determine if size only displaying or size type + size
if re.search("[a-zA-Z]", size_text):
if shoe_type in ("Y", "C"):
shoe_size_type = shoe_size + shoe_type
else:
shoe_size_type = shoe_type + " " + shoe_size
driver.find_element_by_xpath("//li[@data-qa='size-available']").find_element_by_xpath(
"//button[text()[contains(.,'"+shoe_size_type+"')]]").click()
else:
driver.find_element_by_xpath("//li[@data-qa='size-available']").find_element_by_xpath(
"//button[text()='{}']".format(shoe_size)).click()
def click_buy_button(driver):
xpath = "//button[@data-qa='feed-buy-cta']"
LOGGER.info("Waiting for buy button to become clickable")
wait_until_clickable(driver, xpath=xpath, duration=10)
LOGGER.info("Clicking buy button")
driver.find_element_by_xpath(xpath).click()
def select_payment_option(driver):
xpath = "//input[@data-qa='payment-radio']"
LOGGER.info("Waiting for payment checkbox to become clickable")
wait_until_clickable(driver, xpath=xpath, duration=10)
LOGGER.info("Checking payment checkbox")
driver.find_element_by_xpath(xpath).click()
def click_save_button(driver):
xpath = "//button[text()='Save & Continue']"
LOGGER.info("Waiting for save button to become clickable")
wait_until_clickable(driver, xpath=xpath, duration=10)
LOGGER.info("Clicking save button")
driver.find_element_by_xpath(xpath).click()
def click_submit_button(driver):
xpath = "//button[text()='Submit Order']"
LOGGER.info("Waiting for submit button to become clickable")
wait_until_clickable(driver, xpath=xpath, duration=10)
LOGGER.info("Clicking submit button")
driver.find_element_by_xpath(xpath).click()
def wait_until_clickable(driver, xpath=None, class_name=None, duration=10000, frequency=0.01):
if xpath:
WebDriverWait(driver, duration, frequency).until(EC.element_to_be_clickable((By.XPATH, xpath)))
elif class_name:
WebDriverWait(driver, duration, frequency).until(EC.element_to_be_clickable((By.CLASS_NAME, class_name)))
def wait_until_visible(driver, xpath=None, class_name=None, duration=10000, frequency=0.01):
if xpath:
WebDriverWait(driver, duration, frequency).until(EC.visibility_of_element_located((By.XPATH, xpath)))
elif class_name:
WebDriverWait(driver, duration, frequency).until(EC.visibility_of_element_located((By.CLASS_NAME, class_name)))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--username", required=True)
parser.add_argument("--password", required=True)
parser.add_argument("--url", required=True)
parser.add_argument("--shoe-size", required=True)
parser.add_argument("--login-time", default=None)
parser.add_argument("--release-time", default=None)
parser.add_argument("--screenshot-path", default=None)
parser.add_argument("--html-path", default=None)
parser.add_argument("--page-load-timeout", type=int, default=2)
parser.add_argument("--driver-type", default="firefox", choices=("firefox", "chrome"))
parser.add_argument("--headless", action="store_true")
parser.add_argument("--select-payment", action="store_true")
parser.add_argument("--purchase", action="store_true")
parser.add_argument("--num-retries", type=int, default=1)
parser.add_argument("--dont-quit", action="store_true")
parser.add_argument("--shoe-type", default="M", choices=("M", "F", "W", "Y", "C"))
parser.add_argument("--webdriver-path", required=False, default=None)
args = parser.parse_args()
driver = None
if args.driver_type == "firefox":
options = webdriver.FirefoxOptions()
if args.headless:
options.add_argument("--headless")
if args.webdriver_path != None:
executable_path = args.webdriver_path
elif sys.platform == "darwin":
executable_path = "./bin/geckodriver_mac"
elif "linux" in sys.platform:
executable_path = "./bin/geckodriver_linux"
elif "win32" in sys.platform:
executable_path = "./bin/geckodriver_win32.exe"
else:
raise Exception("Drivers for installed operating system not found. Try specifying the path to the drivers with the --webdriver-path option")
driver = webdriver.Firefox(executable_path=executable_path, firefox_options=options, log_path=os.devnull)
elif args.driver_type == "chrome":
options = webdriver.ChromeOptions()
if args.headless:
options.add_argument("headless")
if args.webdriver_path != None:
executable_path = args.webdriver_path
elif sys.platform == "darwin":
executable_path = "./bin/chromedriver_mac"
elif "linux" in sys.platform:
executable_path = "./bin/chromedriver_linux"
elif "win32" in sys.platform:
executable_path = "./bin/chromedriver_win32.exe"
else:
raise Exception("Drivers for installed operating system not found. Try specifying the path to the drivers with the --webdriver-path option")
driver = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
else:
raise Exception("Specified web browser not supported, only Firefox and Chrome are supported at this point")
shoe_type = args.shoe_type
run(driver=driver, shoe_type=shoe_type, username=args.username, password=args.password, url=args.url, shoe_size=args.shoe_size,
login_time=args.login_time, release_time=args.release_time, page_load_timeout=args.page_load_timeout,
screenshot_path=args.screenshot_path, html_path=args.html_path, select_payment=args.select_payment,
purchase=args.purchase, num_retries=args.num_retries, dont_quit=args.dont_quit)