This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
196 lines (160 loc) · 5.94 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
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
from pathlib import Path
from configparser import ConfigParser
parser = ConfigParser()
parser.read('config.ini')
#Constants from settings
TIME_BETWEEN_CHECKS = int(parser.get('Amazon_Bot', 'TIME_BETWEEN_CHECKS'))
AMOUNT_TO_BUY = int(parser.get('Amazon_Bot', 'AMOUNT_TO_BUY'))
CHROME_DRIVER_LOCATION = Path(parser.get('Selenium_Settings', 'CHROME_DRIVER_LOCATION'))
#Unused
WEB_PAGES = ['https://www.amazon.co.uk/BIC-Cello-Comfort-Ballpoint-Medium/dp/B07RY6ZC83/ref=sr_1_2_sspa?dchild=1&keywords=pen&qid=1613387800&sr=8-2-spons&psc=1&smid=A2RCZCHI7CGC8P&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUFTS0tQSkFYRkRNMEYmZW5jcnlwdGVkSWQ9QTAwNzI2MDMzOVdOSVhDV1lKMDc1JmVuY3J5cHRlZEFkSWQ9QTA4NjIxMjczVFBRQkVHSVhKM0EzJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==']
bought = 0
class AmazonBot():
def __init__(self, WEB_PAGES):
try:
self.driver = webdriver.Chrome(CHROME_DRIVER_LOCATION)
except:
try:
self.driver = webdriver.Chrome()
except:
print('Cant find valid Chrome driver')
quit()
self.WEB_PAGES = WEB_PAGES
def login(self, username, password):
self.driver.get('https://www.amazon.co.uk/')
#Accepts Cookies
try:
self.driver.find_element_by_id("sp-cc-accept").click()
except:
pass
#Enters User details
self.driver.find_element_by_id("nav-link-accountList").click()
self.driver.find_element_by_id("ap_email").send_keys(username)
self.driver.find_element_by_id("continue").click()
sleep(0.5)
self.driver.find_element_by_name('rememberMe').click()
self.driver.find_element_by_id("ap_password").clear()
self.driver.find_element_by_id("ap_password").send_keys(password)
self.driver.find_element_by_id("signInSubmit").click()
sleep(2)
#If they need to use 2fa gives longer
url = self.driver.current_url
if url.startswith('https://www.amazon.co.uk/ap/cvf/approval'):
print('Please complete 2fa')
sleep(120)
i=0
while i<120:
url = self.driver.current_url
print(url)
if url.startswith('https://www.amazon.co.uk/ap/cvf/approval'):
sleep(1)
i = i + 1
print(url)
else:
break
else:
sleep(5)
#Enable Oneclick purchase
self.driver.get('https://www.amazon.co.uk/cpe/yourpayments/settings/manageoneclick')
sleep(1)
self.driver.find_element_by_css_selector('.a-switch-control').click()
sleep(2)
#Unused/Needs improving
def checkStock(self):
for webpage in self.WEB_PAGES:
self.driver.get(webpage)
try:
self.driver.find_element_by_id("sp-cc-accept").click()
except:
pass
try:
buyNow = self.driver.find_element_by_id('buy-now-button')
buyNow.click()
print('In stock')
except:
print('Not in stock')
#Checks What the Xbox Button is
def findXboxButton(self):
self.driver.get('https://www.amazon.co.uk/Xbox-RRT-00007-Series-X/dp/B08H93GKNJ/')
#Accepts Cookies
try:
self.driver.find_element_by_id("sp-cc-accept").click()
except:
pass
sleep(1)
#Trys all product options till error which indicates all have been tried or Xbox Series X has been found
try:
i = 0
while True:
selectbtn = self.driver.find_element_by_id(f'edition_{i}')
#Sees if button value is eqal to Xbox Series X
if selectbtn.text == 'Xbox Series X':
self.xboxNumber = i
break
i=i+1
except:
#Needs proper error handling adding
print('Error')
#Checks Stock
def xboxCheckStock(self):
#Gets Xbox Page
self.driver.get('https://www.amazon.co.uk/Xbox-RRT-00007-Series-X/dp/B08H93GKNJ/')
#Accepts Cookies
try:
self.driver.find_element_by_id("sp-cc-accept").click()
except:
pass
sleep(1)
#Selects the Xbox
selectbtn = self.driver.find_element_by_id(f'edition_{self.xboxNumber}')
if selectbtn.text == 'Xbox Series X':
selectbtn.click()
#self.driver.find_element_by_partial_link_text('Xbox Series X')
sleep(2)
#Looks for buy now
try:
buyNow = self.driver.find_element_by_id('buy-now-button')
print('In stock')
return True
#If error its not availible
except:
print('Not in stock')
return False
sleep(1)
#This runs when the btutton is wrong
else:
#Needs proper error handling adding
print('Error')
#Need creating
def Buy(self):
buyNow = self.driver.find_element_by_id('buy-now-button')
buyNow.click()
#Needs creating
def AlertUser():
#Add:
# - Discord Notifications
# - Instagram Notifications
# - Email Notifications
pass
bot = AmazonBot(WEB_PAGES)
username = parser.get('Amazon_Bot', 'username')
password = parser.get('Amazon_Bot', 'password')
bot.login(username, password)
bot.findXboxButton()
#Runs while not in stock/availible
stock = False
while not stock:
stock = bot.xboxCheckStock()
sleep(TIME_BETWEEN_CHECKS)
AlertUser()
bot.login(username, password)
while bought <= AMOUNT_TO_BUY:
try:
bot.Buy()
bought = bought + 1
except:
break
print('Error buying')