-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
154 lines (93 loc) · 3.87 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
import os
import re
import sys
from time import sleep
from selenium import webdriver
import login
class LotsBot():
def __init__(self, driver: webdriver.Chrome) -> None:
self.driver = driver
self.added_skus = []
def run(self, item_count: int) -> None:
"""parse individual items tab (https://beta.888lots.com/catalog)
and add every item to the cart
"""
print('running bot...')
self.driver.refresh()
table = self.driver.find_elements_by_xpath('//div[@class="row products_page"]/div/ul/li')
for i in range(item_count):
item = table[i]
sku = re.search('[BX]\d.{8}', item.text)
if not sku:
print('sku not able to be found, continuing')
continue
sku = sku.group()
if sku in self.added_skus:
continue
print('processing: SKU ' + sku)
if 'not order it' in item.text:
print(' ~ beep beep boop boop the item is already in someone\'s cart')
continue
if 'In your cart' in item.text:
print(' ~ beep beep boop boop the item is already in your cart')
self.added_skus.append(sku)
continue
buy_now = item.find_element_by_xpath('.//div[@class="cartbrnh"]/a')
buy_now.click()
sleep(1)
add_to_cart = item.find_element_by_xpath('//button[contains(text(), "Add to cart")]')
if not add_to_cart.is_displayed() or not add_to_cart.is_enabled():
print(' ~ beep beep boop boop the item is already in someone\s cart')
cancel = item.find_element_by_xpath('//button[contains(text(), "Cancel")]')
cancel.click()
continue
add_to_cart.click()
sleep(1)
print(' ~ the item has been added to your cart')
self.added_skus.append(sku)
self.added_skus = self.added_skus[:item_count]
print('finished running the bot')
def run_loop(self, item_count: int) -> None:
while True:
#if not os.path.isfile('run'):
# print('run file does not exist, sleeping bot for 10 seconds...\n')
#
# sleep(10)
try:
self.run(item_count)
except Exception as e:
print(e)
print(' ~ beep beep boop boop there was an error')
print(' ~ unsure of how to handle this error, so restarting...')
def main() -> None:
options = webdriver.ChromeOptions()
if '--debug' not in sys.argv:
options.add_argument('headless')
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)
driver.get('https://888lots.com/')
login_prompt = driver.find_element_by_xpath('//*[contains(text(), "Login")]')
login_prompt.click()
sleep(1)
email = driver.find_element_by_xpath('//form/div/input[@name="email"]')
password = driver.find_element_by_xpath('//input[@name="password"]')
def send_keys_slow(elem: webdriver.remote.webelement.WebElement, text: str) -> None:
for c in text:
elem.send_keys(c)
sleep(.05)
send_keys_slow(email, login.email)
send_keys_slow(password, login.password)
sleep(1)
login_button = driver.find_element_by_xpath('//button[contains(text(), "Login")]')
login_button.click()
end_tour = driver.find_element_by_xpath('//button[contains(text(), "End tour")]')
if end_tour:
end_tour.click()
driver.get('https://beta.888lots.com/catalog')
bot = LotsBot(driver)
count = 20
if len(sys.argv) > 1:
count = int(sys.argv[len(sys.argv) - 1])
bot.run_loop(count)
if __name__ == '__main__':
main()