-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpubot2020.py
134 lines (109 loc) · 4.92 KB
/
gpubot2020.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
# McKay Mower, [email protected]
# 12/18/2020
# This python script represents a bot that can buy the product given a certain url from bestbuy
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException, ElementNotInteractableException
import time
import bs4
# actual gpu
url = 'https://www.bestbuy.com/site/nvidia-geforce-rtx-3070-8gb-gddr6-pci-express-4-0-graphics-card-dark-platinum-and-black/6429442.p?skuId=6429442 '
# tests below
# url = 'https://www.bestbuy.com/site/sandisk-ultra-dual-drive-luxe-128gb-usb-3-1-usb-type-c-flash-drive/6422271.p?skuId=6422271'
# add to cart test
# url = 'https://www.bestbuy.com/site/amd-ryzen-7-3700x-3rd-generation-8-core-16-thread-3-6-ghz-4-4-ghz-max-boost-socket-am4-unlocked-desktop-processor/6356277.p?skuId=6356277'
# url = 'https://www.bestbuy.com/site/corsair-vengeance-rgb-pro-32gb-2pk-16gb-3-2ghz-pc4-25600-ddr4-dimm-unbuffered-non-ecc-desktop-memory-kit-with-rgb-lighting-black/6333800.p?skuId=6333800'
# sold out test
# url = 'https://www.bestbuy.com/site/msi-aegis-rs-gaming-desktop-intel-core-i7-10700kf-16gb-memory-nvidia-geforce-rtx-3080-1tb-ssd-black-black/6439310.p?skuId=6439310'
# creates a web driver with my personal google chrome options and returns it
def make_driver():
# open a new instance of chrome
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=C:\\Users\\mmowe\\AppData\\Local\\Google\\Chrome\\User Data')
# might be able to use this: options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome('C:\\Users\\mmowe\\PycharmProjects\\chromedriver.exe', options=options)
return driver
# finds the gpu from the link
def find_card(driver):
# open url in chrome
driver.get(url)
# gather html from page
markup = bs4.BeautifulSoup(driver.page_source, 'html.parser')
while True:
try:
# try to get either a sold out or atc button
add_to_cart = markup.find('button', {'class': 'btn btn-primary btn-lg btn-block btn-leading-ficon '
'add-to-cart-button'})
sold_out = markup.find('button', {'class': 'btn btn-disabled btn-lg btn-block add-to-cart-button'})
# check which button it is
if add_to_cart:
print(f'found button:', add_to_cart.get_text())
add_cart(driver)
elif sold_out:
print(f'found button:', sold_out.get_text())
print('sold out, refreshing')
time.sleep(4)
driver.refresh()
except (NoSuchElementException, TimeoutException, ElementNotInteractableException):
time.sleep(4)
driver.refresh()
def add_cart(driver):
# get atc button name from css
atc_button = '.add-to-cart-button'
# try to click button
while True:
try:
driver.find_element_by_css_selector(atc_button).click()
print('clicked button, check cart')
break
finally:
driver.implicitly_wait(1)
# once add to cart button is clicked, navigate to cart
driver.get('https://www.bestbuy.com/cart')
initiate_checkout(driver)
def initiate_checkout(driver):
time.sleep(1)
# get checkout button name from css
checkout_button = '.btn-primary'
while True:
try:
driver.find_element_by_css_selector(checkout_button).click()
print('clicked to initiate checkout')
break
except (NoSuchElementException, TimeoutException, ElementNotInteractableException):
pass
# once checkout is initiated, last thing we need to enter is the cvv code of the card
enter_cvv(driver)
def enter_cvv(driver):
time.sleep(1)
# try to switch to shipping from in store pick up
print('switching to shipping method instead of ispu')
try:
driver.find_element_by_class_name('ispu-card__switch').click()
except (NoSuchElementException, TimeoutException):
pass
while True:
try:
cvv = driver.find_element_by_id('credit-card-cvv')
print('found cvv input, going to send input')
cvv.send_keys('600')
time.sleep(1)
print('typed cvv, placing order')
break
except (NoSuchElementException, TimeoutException, ElementNotInteractableException):
print('transaction could not be made')
# once cvv is in, just need to confirm order
place_order(driver)
def place_order(driver):
time.sleep(1)
print('Trying to place order')
while True:
try:
# click the place order button
driver.find_element_by_css_selector('.btn-primary').click()
break
except (NoSuchElementException, TimeoutException, ElementNotInteractableException):
pass
print('order placed!')
if __name__ == '__main__':
driver = make_driver()
find_card(driver)