-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot.py
299 lines (251 loc) · 10.7 KB
/
bot.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
294
295
296
297
298
299
import os.path
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchWindowException
from selenium.common.exceptions import ElementNotVisibleException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver
import datetime
import time as clock
from random import random
from datetime import datetime
isPayPal = 0
checkedListings = []
sizeC = 0
keywords = []
matchedClothes = []
LOGFILE = True
useConfig = False
password = ''
peu = {'Visa':'visa', 'American Express':'american_express', 'Mastercard':'master', 'Solo':'solo', 'PayPal':'paypal'}
pasia = {'Visa':'visa', 'American Express':'american_express', 'Mastercard':'master', 'JCB':'jcb', '代金引換':'cod'}
ceu = {'GB':'UK', 'UK (N. IRELAND)':'NB', 'AUSTRIA':'AT', 'BELARUS':'BY', 'BELGIUM':'BE', 'BULGARIA':'BG',
'CROATIA':'HR', 'CZECH REPUBLIC':'CZ', 'DENMARK':'DK', 'ESTONIA':'EE', 'FINLAND':'FI', 'FRANCE':'FR',
'GERMANY':'DE', 'GREECE':'GR', 'HUNGARY':'HU', 'ICELAND':'IS', 'IRELAND':'IE', 'ITALY':'IT',
'LATVIA':'LV', 'LITHUANIA':'LT', 'LUXEMBOURG':'LU', 'MONACO':'MC', 'NETHERLANDS':'NL', 'NORWAY':'NO',
'POLAND':'PL', 'PORTUGAL':'PT', 'ROMANIA':'RO', 'RUSSIA':'RU', 'SLOVAKIA':'SK', 'SLOVENIA':'SI',
'SPAIN':'ES', 'SWEDEN':'SE', 'SWITZERLAND':'CH', 'TURKEY':'TR'}
class Found(Exception):
pass
def pause(offset):
clock.sleep(offset+random())
def check_exists_by_xpath(xpath, driver):
try:
return driver.find_element_by_id(xpath[9:-2])
except NoSuchElementException:
myElem = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, xpath)))
return driver.find_element_by_xpath(xpath)
def sendKeys(value, field, driver):
if len(value) < 1:
return None
try:
driver.execute_script("arguments[0].value = '" + value + "';", field)
except WebDriverException:
return None
def selectValue(value, obj):
try:
obj.select_by_value(value)
except WebDriverException:
pass
def getLoc(f):
base_path = os.path.abspath(".")
return os.path.join(base_path, f)
def writeLog(txt):
if not LOGFILE:
return None
f = open(getLoc('logfile.txt'), 'a')
f.write(str(txt) + '\n')
f.close()
def cart():
cartp = check_exists_by_xpath("""//*[@id="cart"]""", driver)
while True:
cart = cartp.find_elements_by_class_name('checkout')[0]
try:
cart.click()
break
except ElementNotVisibleException:
pass
name = check_exists_by_xpath("""//*[@id="order_billing_name"]""", driver)
sendKeys(paydetails['Name'], name, driver)
email = check_exists_by_xpath("""//*[@id="order_email"]""", driver)
sendKeys(paydetails['Email'], email, driver)
phone = check_exists_by_xpath("""//*[@id="order_tel"]""", driver)
sendKeys(paydetails['Phone'], phone, driver)
add1 = check_exists_by_xpath("""//*[@id="bo"]""", driver)
sendKeys(paydetails['Addr1'], add1, driver)
add2 = check_exists_by_xpath("""//*[@id="oba3"]""", driver)
sendKeys(paydetails['Addr2'], add2, driver)
if reg != 'ASIA':
country = Select(driver.find_element_by_name("order[billing_country]"))
if reg == 'EU':
selectValue(ceu[paydetails['Country']], country)
elif reg == 'US':
selectValue(paydetails['Country'], country)
if reg == 'EU':
add3 = check_exists_by_xpath("""//*[@id="order_billing_address_3"]""", driver)
sendKeys(paydetails['Addr3'], add3, driver)
elif reg == 'US':
state = Select(driver.find_element_by_name("order[billing_state]"))
selectValue(paydetails['Addr3'], state)
city = check_exists_by_xpath("""//*[@id="order_billing_city"]""", driver)
sendKeys(paydetails['City'], city, driver)
postcode = check_exists_by_xpath("""//*[@id="order_billing_zip"]""", driver)
sendKeys(paydetails['Post/zip code'], postcode, driver)
if reg == 'EU' or reg == 'ASIA':
cardType = Select(driver.find_element_by_id("credit_card_type"))
if reg == 'EU':
selectValue(peu[paydetails['CardType']], cardType)
else:
selectValue(pasia[paydetails['cardType']], cardType)
if paydetails['CardType'].lower() != 'paypal' or paydetails['CardType'] != '代金引換':
if reg == 'EU':
cardno = check_exists_by_xpath("""//*[@id="cnb"]""", driver)
elif reg == 'US':
cardno = check_exists_by_xpath("""//*[@id="nnaerb"]""", driver)
sendKeys(paydetails['Cardno'], cardno, driver)
if reg == 'EU':
cvv = check_exists_by_xpath("""//*[@id="vval"]""", driver)
elif reg == 'US':
cvv = check_exists_by_xpath("""//*[@id="orcer"]""", driver)
sendKeys(paydetails['CardCVV'], cvv, driver)
expiraryDate1 = Select(driver.find_element_by_name("credit_card[month]"))
selectValue(paydetails['CardMonth'], expiraryDate1)
expiraryDate2 = Select(driver.find_element_by_name("credit_card[year]"))
selectValue(paydetails['CardYear'], expiraryDate2)
tickBox = driver.find_elements_by_class_name('iCheck-helper')
tickBox[1].click()
complete = driver.find_element_by_name('commit')
complete.click()
def searchItem(item):
url = 'http://www.supremenewyork.com/shop/all/'
url += item['selectedCategory']
driver.get(url)
try:
while True:
bestMatch = [0, 0, 0]
driver.get(url)
listings = driver.find_elements_by_class_name("name-link")
leng = len(listings)
for i in range(0, leng):
if i % 2 == 0:
text = listings[i].text
split = text.strip()
matches = 0
colour = 0
for keyword in item['keywords']:
if keyword.encode('ascii', 'ignore') in split.encode('ascii', 'ignore'):
matches += 1
try:
lcolour = listings[i+1].text
if lcolour.encode('ascii', 'ignore') in item['selectedColour'].encode('ascii', 'ignore'):
colour = 1
except AttributeError:
colour = 0
writeLog([item['keywords'], item['selectedColour'], split, lcolour, matches,
colour, len(item['keywords']) + colour])
if len(item['keywords']) == matches and strict:
if item['selectedColour'] != '' and colour == 1:
listings[i].click()
raise Found
elif item['selectedColour'] == '' and colour == 0:
listings[i].click()
raise Found
if bestMatch[0] <= matches and colour >= bestMatch[1]:
bestMatch[0] = matches
bestMatch[1] = colour
bestMatch[2] = i
if not strict:
break
listings[bestMatch[2]].click()
except Found:
pass
pause(0.5)
try:
if item['selectedSize'] != 'First available' and item['selectedSize'] != '':
if reg == 'EU':
size = Select(driver.find_element_by_id("size"))
elif reg == 'US':
size = Select(driver.find_element_by_id("s"))
op = size.options
found = False
for x in op:
if item['selectedSize'] in x.text:
found = True
break
if found:
selectValue(item['selectedSize'], size)
add = driver.find_element_by_xpath("""//*[@id="add-remove-buttons"]/input""")
add.click()
elif nextS:
add = driver.find_element_by_xpath("""//*[@id="add-remove-buttons"]/input""")
add.click()
elif skipS:
pass
else:
print("Sorry the item size is sold out!")
return None
else:
add = driver.find_element_by_xpath("""//*[@id="add-remove-buttons"]/input""")
add.click()
except NoSuchElementException:
if not skipS:
print("Sorry the item is sold out!")
return None
def returnTime():
timeInput = droptime.split(":")
tarHour = int(timeInput[0])
tarMin = int(timeInput[1])
tarSec = int(timeInput[2])
target = datetime.now()
target = target.replace(hour=tarHour, minute=tarMin, second=tarSec)
while True:
cur = datetime.now()
if cur >= target:
clock.sleep(1)
break
def openTab(url, driver):
m = driver.current_window_handle
try:
driver.execute_script('''window.open("{0}","_blank")'''.format(url))
except WebDriverException:
pass
driver.switch_to.window(m)
def openChrome(paydetailsO, itemdetsO, timeO, strictO, skipO, nextO, cdloc, capabilities, useProxy, PROXY):
global driver, strict, password, reg, items, droptime, pDescr, paydetails, category, skipS, nextS
chrome_options = webdriver.ChromeOptions()
if useProxy:
prx = Proxy()
prx.proxy_type = ProxyType.MANUAL
prx.http_proxy = PROXY
prx.socks_proxy = PROXY
prx.ssl_proxy = PROXY
prx.add_to_capabilities(capabilities)
else:
prx = Proxy()
prx.proxy_type = ProxyType.SYSTEM
prx.add_to_capabilities(capabilities)
chrome_options.binary_location = capabilities['chrome.binary']
driver = webdriver.Chrome(cdloc, desired_capabilities=capabilities)
openTab('https://www.google.com', driver)
paydetails = paydetailsO
reg = paydetailsO['Region']
strict = strictO
skipS = skipO
nextS = nextO
droptime = timeO
items = []
for x in itemdetsO:
print(x[0],x[1],x[2],x[3])
items.append({'selectedCategory': x[0], 'keywords': x[1].split(','), 'selectedColour': x[2], 'selectedSize': x[3]})
returnTime()
try:
for it in items:
searchItem(it)
cart()
except NoSuchWindowException:
print('[!] Chrome window closed. Click GO! to re-start')
return None