-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpangbot.py
253 lines (195 loc) · 8.54 KB
/
pangbot.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
#!/usr/bin/python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from time import sleep
from random import randrange,random
import smtplib
from playsound import playsound
import os
import sys
import optparse
from multiprocessing import Process
CWD = os.path.dirname(os.path.abspath(__file__))
AMZ_URL = "https://www.amazon.com/"
def init_conf():
parser = optparse.OptionParser('%prog [options]')
parser.add_option("-u", "--amzuser", default='',
action="store", type="string", dest="amz_user",
help = "amazon user, required ")
parser.add_option("-p", "--amzpass", default='',
action="store", type="string", dest="amz_pass",
help = "amazon password, required")
parser.add_option("-c", "--cartype", default='wf',
action="store", type="string", dest="cart_type",
help = "cart type: 'wf' or 'af' default: wf")
parser.add_option("-m", "--smtpserv", default='smtp.gmail.com',
action="store", type="string", dest="smtp_serv",
help = "smtp server for sending SMS alert")
parser.add_option("-s", "--smtpuser", default='',
action="store", type="string", dest="smtp_user",
help = "smtp mail server account")
parser.add_option("-a", "--smtppass", default='',
action="store", type="string", dest="smtp_pass",
help = "smtp mail server password")
parser.add_option("-n", "--sms_number", default='',
action="store", type="string", dest="sms_num",
help = "cellphone number for SMS alert")
parser.add_option("-i", "--insane",
action="store_true", dest="insane",
help = "insane mode: desperately grab slot and auto checkout")
(options, args) = parser.parse_args()
if options.amz_user == '' or options.amz_pass == '':
sys.exit("amazon user and pass can not be empty")
return options
def do_check_wf_cart(conf):
try:
play_process = Process(target=playsound, args=('success.mp3',)) # alerting process
chrome_options = Options()
#chrome_options.add_argument("--headless")
#chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
driver = webdriver.Chrome(options=chrome_options)
driver.get(AMZ_URL)
driver.implicitly_wait(20)
sleep(random()*2)
driver.find_element_by_link_text('Your Orders').click()
e = driver.find_element_by_id('ap_email')
e.click()
e.send_keys(conf.amz_user)
sleep(random()*3)
driver.find_element_by_id('continue').click()
e = driver.find_element_by_id('ap_password')
sleep(random()*3)
e.click()
e.send_keys(conf.amz_pass)
sleep(random()*2)
driver.find_element_by_id('auth-signin-button').click()
driver.find_element_by_id('nav-cart-count')
sleep(random()*3)
driver.find_element_by_id('nav-cart').click()
driver.find_element_by_id('sc-subtotal-amount-buybox')
e = driver.find_element_by_name(
'proceedToALMCheckout-VUZHIFdob2xlIEZvb2Rz')
sleep(random()*3)
e.submit()
sleep(random()*3)
driver.find_element_by_name('proceedToCheckout').click()
sleep(random()*3)
driver.find_element_by_id('subsContinueButton').click()
slot_found = False
ntrial = 0
while slot_found == False:
e = driver.find_elements_by_class_name(
'ufss-date-select-toggle-text-availability')
for i in e:
print(i.text)
if (i.text != 'Not available'):
slot_found = True
play_process.start() # async play music
if conf.smtp_user != '':
send_mail(conf.smtp_serv,
conf.smtp_user, conf.smtp_pass,
conf.smtp_user, conf.sms_num+'@txt.att.net',
'pangbot alert!')
if conf.insane == False:
input('time slot found! Please checkout in the browser then push any key to continue.')
else:
slots = driver.find_elements_by_class_name('ufss-slot-toggle-native-button')
for slot in slots:
print("slot text: %s" % (slot.text))
if slot.text == 'FREE':
slot.click()
driver.find_elements_by_class_name('a-button-input').submit()
sleep(random())
driver.find_elements_by_class_name('a-button-input').submit()
driver.find_element_by_name('placeYourOrder1').submit()
input('Order placed. Play check the order status and push any key to continue')
break
break
sleep_time = randrange(20,40)
print("sleeping %d.." % (sleep_time))
sleep(sleep_time)
driver.refresh()
ntrial += 1
print("retry %d times.\n" % ntrial)
except:
print('error executing actions.')
finally:
sleep(20)
play_process.terminate()
play_process.join(1)
driver.quit()
def do_check_af_cart(conf):
try:
chrome_options = Options()
#chrome_options.add_argument("--headless")
#chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE)
#chrome_options.add_argument("user-data-dir="+CWD+"\\foo")
driver = webdriver.Chrome(options=chrome_options)
driver.get(AMZ_URL)
driver.implicitly_wait(20)
sleep(random()*3)
driver.find_element_by_link_text('Your Orders').click()
e = driver.find_element_by_id('ap_email')
e.click()
e.send_keys(conf.amz_user)
sleep(random()*3)
driver.find_element_by_id('continue').click()
e = driver.find_element_by_id('ap_password')
e.click()
e.send_keys(conf.amz_pass)
sleep(random()*3)
driver.find_element_by_id('auth-signin-button').click()
driver.find_element_by_id('nav-cart-count')
driver.find_element_by_id('nav-cart').click()
driver.find_element_by_id('sc-subtotal-amount-buybox')
e = driver.find_element_by_name(
'proceedToALMCheckout-QW1hem9uIEZyZXNo')
e.submit()
driver.find_element_by_name('proceedToCheckout').click()
slot_found = False
while slot_found == False:
e = driver.find_elements_by_class_name(
'ufss-date-select-toggle-text-availability')
for i in e:
print(i.text)
if (i.text != 'Not available'):
slot_found = True
playsound(CWD+'\\success.mp3', block=False)
if conf.smtp_user != '':
send_mail(conf.smtp_serv,
conf.smtp_user, conf.smtp_pass,
conf.smtp_user, conf.sms_num+'@txt.att.net',
'pangbot alert!')
input('time slot found! push any key to continue.')
break
sleep(randrange(10,30))
driver.refresh()
except:
print('error executing actions.')
finally:
sleep(3)
driver.quit()
def send_mail(smtp_server, user, passwd, sender, recvr, msg):
try:
server = smtplib.SMTP_SSL(smtp_server, 465)
server.ehlo()
server.login(user, passwd)
server.sendmail(sender, recvr, msg)
except:
print('Something went wrong...')
finally:
server.quit()
def main():
conf = init_conf()
print("PangBot configuration summary:")
print(conf)
while True:
if conf.cart_type == 'wf':
do_check_wf_cart(conf)
elif conf.cart_type == 'af':
do_check_af_cart(conf)
else:
print("cart type not supported.\n")
sleep(20)
if __name__ == "__main__":
main()