├── buy.py ├── test.py ├── test.json ├── config.json └── snkr_bot.py /buy.py: -------------------------------------------------------------------------------- 1 | from snkr_bot import snkr_bot 2 | 3 | json_file = 'config.json' 4 | bot = snkr_bot(json_file) 5 | bot.start_buy() 6 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from snkr_bot import snkr_bot 2 | 3 | json_file = 'test.json' 4 | bot = snkr_bot(json_file) 5 | bot.start_test() 6 | -------------------------------------------------------------------------------- /test.json: -------------------------------------------------------------------------------- 1 | {"chrome_driver" : "/Users/kujin/Downloads/chromedriver", 2 | "profile_path" : "./profile/", 3 | "url" : "https://www.nike.com/launch/t/air-zoom-pegasus-35-turbo-gyakusou-gold-dart/", 4 | "size" : "8.5", 5 | "time" : "2019-03-01 17:08:00", 6 | "cvv" : "1234"} 7 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | {"chrome_driver" : "/Users/GeniusTang/Downloads/chromedriver", 2 | "profile_path" : "./profile/", 3 | "url" : "https://www.nike.com/launch/t/air-jordan-4-white-bright-crimson-black/", 4 | "size" : "8.5", 5 | "time" : "2019-03-01 17:08:00", 6 | "cvv" : "1234"} 7 | -------------------------------------------------------------------------------- /snkr_bot.py: -------------------------------------------------------------------------------- 1 | import json 2 | from selenium import webdriver 3 | from selenium.webdriver.common.action_chains import ActionChains 4 | from selenium.webdriver.common.keys import Keys 5 | from selenium.webdriver.support import expected_conditions as EC 6 | from selenium.webdriver.support.ui import WebDriverWait 7 | from selenium.webdriver.common.by import By 8 | from time import sleep 9 | from time import time 10 | from datetime import datetime 11 | from functools import partial 12 | 13 | class snkr_bot(object): 14 | def __init__(self, json_file): 15 | self._read_json(json_file) 16 | self._start_driver() 17 | 18 | def _read_json(self, json_file): 19 | """ 20 | Load chrome driver path, chrome cookie path, 21 | shoes url, shoes size, credit card cvv, release time 22 | from JSON file. 23 | """ 24 | with open(json_file, 'r') as f: 25 | data = json.load(f) 26 | self.chrome_driver = data["chrome_driver"] 27 | self.profile_path = data["profile_path"] 28 | self.url = data["url"] 29 | self.size = data["size"] 30 | self.cvv = data["cvv"] 31 | self.time = datetime.strptime(data["time"], '%Y-%m-%d %H:%M:%S') 32 | 33 | def _start_driver(self): 34 | """ 35 | Start driver from cookie. 36 | """ 37 | options = webdriver.ChromeOptions() 38 | options.add_argument("user-data-dir={}".format(self.profile_path)) 39 | self.driver = webdriver.Chrome(self.chrome_driver, chrome_options=options) 40 | self.driver.maximize_window() 41 | 42 | def _wait(self, action, freq=0.1, time_out=2, message='Succeeded', err_message='Failed'): 43 | """ 44 | A wrapper to try an action until succeed. 45 | action: a callable function, 46 | freq: how frequent you try, 47 | time_out: maximum time you want to try, 48 | message: print if this action succeeds, 49 | err_message: print if this action fails 50 | """ 51 | FLAG = False 52 | begin = time() 53 | while not FLAG and (time()-begin) < time_out: 54 | try: 55 | action() 56 | except Exception as e: 57 | print(e) 58 | sleep(freq) 59 | else: 60 | FLAG = True 61 | print(message if FLAG else err_message) 62 | if not FLAG: 63 | raise Exception('Program Failed!') 64 | 65 | def _find_size(self): 66 | """ 67 | Try to find size. 68 | """ 69 | print('Try to find size {}.'.format(self.size)) 70 | find_size = self.driver.find_element_by_xpath('//button[contains(text(), "{}")]'.format(self.size)) 71 | self.driver.execute_script("arguments[0].scrollIntoView(true);", find_size) 72 | find_size.click() 73 | 74 | def _add_to_cart(self): 75 | """ 76 | Try to add to cart. 77 | """ 78 | print('Try to add to cart.') 79 | add_cart = self.driver.find_element_by_xpath('//button[contains(translate(text(), "CART", "cart"),"cart")]') 80 | self.driver.execute_script("arguments[0].scrollIntoView(true);", add_cart) 81 | add_cart.click() 82 | 83 | def _quick_buy(self): 84 | """ 85 | Try to buy without adding to cart.' 86 | """ 87 | print('Try to quick buy.') 88 | quick_buy = self.driver.find_element_by_xpath('//button[contains(translate(text(), "BUY", "buy"), "buy")]') 89 | self.driver.execute_script("arguments[0].scrollIntoView(true);", quick_buy) 90 | quick_buy.click() 91 | 92 | def _go_to_url(self, url): 93 | """ 94 | Navigate to a url. 95 | """ 96 | print('Try to navigate to {}'.format(url)) 97 | self.driver.get(url) 98 | 99 | def _find_cvv_iframe(self): 100 | """ 101 | Find and switch domain to the iframe with cvv info. 102 | """ 103 | print('Try to find credit card iframe.') 104 | iframe =self.driver.find_element_by_class_name("credit-card-iframe-cvv") 105 | self.driver.switch_to.frame(iframe) 106 | 107 | def _enter_cvv(self): 108 | """ 109 | Enter cvv and switch back to main domain. 110 | """ 111 | print('Try to enter cvv.') 112 | cvv = self.driver.find_element_by_id("cvNumber") 113 | self.driver.execute_script("arguments[0].scrollIntoView(true);", cvv) 114 | cvv.send_keys(self.cvv) 115 | self.driver.switch_to_default_content() 116 | 117 | def _save(self): 118 | """ 119 | Save credit card info. 120 | """ 121 | print('Try to save and continue.') 122 | self.driver.find_element_by_xpath('//button[contains(translate(text(), "SAVE", "save"), "save")]').click() 123 | 124 | def _place_order(self): 125 | """ 126 | Place order. 127 | """ 128 | print('Try to place order.') 129 | self.driver.find_element_by_xpath('//button[contains(translate(text(), "ORDER", "order"), "order")]').click() 130 | 131 | def _test(self): 132 | """ 133 | Test adding to card and checking out. 134 | Remember to use a wrong cvv number. 135 | """ 136 | self._wait(partial(self._go_to_url, self.url)) 137 | self._wait(self._find_size) 138 | self._wait(self._add_to_cart) 139 | self._wait(partial(self._go_to_url, 'https://www.nike.com/us/en/checkout')) 140 | self._wait(self._find_cvv_iframe) 141 | self._wait(self._enter_cvv) 142 | #self._wait(self._place_order) 143 | 144 | def _buy(self): 145 | """ 146 | Buy it. 147 | """ 148 | self._wait(partial(self._go_to_url, self.url)) 149 | self._wait(self._find_size) 150 | self._wait(self._quick_buy) 151 | self._wait(self._find_cvv_iframe) 152 | self._wait(self._enter_cvv) 153 | self._wait(self._place_order) 154 | 155 | def _start(self, action): 156 | """ 157 | Start an action at a specific time. 158 | """ 159 | while True: 160 | now = datetime.now() 161 | if now >= self.time: 162 | action() 163 | break 164 | else: 165 | sleep(0.1) 166 | 167 | def start_test(self): 168 | """ 169 | Start testing. 170 | """ 171 | self._start(self._test) 172 | 173 | def start_buy(self): 174 | """ 175 | Start buying. 176 | """ 177 | self._start(self._buy) 178 | 179 | 180 | def _buy_deprecated(self): 181 | """ 182 | This function has been deprecated. 183 | """ 184 | self.driver.get(self.url) 185 | #wait = WebDriverWait(self.driver, 10, poll_frequency=0.05) 186 | flag = False 187 | while not flag: 188 | try: 189 | print('try to find size') 190 | find_size = self.driver.find_element_by_xpath('//button[contains(text(), "{}")]'.format(self.size)) 191 | self.driver.execute_script("arguments[0].scrollIntoView(true);", find_size) 192 | find_size.click() 193 | flag = True 194 | except: 195 | sleep(0.05) 196 | #find_size = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[text()="{}"]'.format(self.size)))) 197 | #find_size.click() 198 | flag = False 199 | while not flag: 200 | try: 201 | print('try to add to cart') 202 | #add_cart = self.driver.find_element_by_xpath('//button[text()="ADD TO CART"]') 203 | add_cart = self.driver.find_element_by_xpath('//button[contains(translate(text(), "CART", "cart"),"cart")]') 204 | #add_cart = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[text()="ADD TO CART"]'))) 205 | self.driver.execute_script("arguments[0].scrollIntoView(true);", add_cart) 206 | add_cart.click() 207 | flag = True 208 | except: 209 | sleep(0.05) 210 | sleep(0.1) 211 | self.driver.get('https://www.nike.com/us/en/checkout') 212 | sleep(2) 213 | iframe =self.driver.find_element_by_class_name("credit-card-iframe-cvv") 214 | self.driver.execute_script("arguments[0].scrollIntoView(true);", iframe) 215 | self.driver.switch_to.frame(iframe) 216 | sleep(2) 217 | self.driver.find_element_by_id("cvNumber").send_keys("9999") 218 | sleep(2) 219 | self.driver.switch_to_default_content() 220 | self.driver.find_element_by_xpath('//button[contains(translate(text(), "ORDER", "order"), "order")]').click() 221 | 222 | def _purchase_deprecated(self): 223 | """ 224 | This funcion has been deprecated. 225 | """ 226 | self.driver.get(self.url) 227 | flag = False 228 | while not flag: 229 | try: 230 | print('try to find size') 231 | find_size = self.driver.find_element_by_xpath('//button[contains(text(), "{}")]'.format(self.size)) 232 | self.driver.execute_script("arguments[0].scrollIntoView(true);", find_size) 233 | find_size.click() 234 | flag = True 235 | except: 236 | sleep(0.05) 237 | 238 | flag = False 239 | while not flag: 240 | try: 241 | print('try to add to cart') 242 | #add_cart = self.driver.find_element_by_xpath('//button[contains(text(),"Purchase")]') 243 | add_cart = self.driver.find_element_by_xpath('//button[contains(translate(text(), "BUY", "buy"), "buy")]') 244 | self.driver.execute_script("arguments[0].scrollIntoView(true);", add_cart) 245 | add_cart.click() 246 | flag = True 247 | except: 248 | sleep(0.05) 249 | 250 | flag = False 251 | while not flag: 252 | try: 253 | print('try to place order') 254 | #self.driver.find_element_by_xpath("//input[contains(@placeholder,'XXX')]").send_keys("9273") 255 | self.driver.find_element_by_xpath('//input[@placeholder="XXX"]').send_keys("9273") 256 | #self.driver.find_element_by_xpath("//input[contains(@value,'Place')]").click() 257 | self.driver.find_element_by_xpath('//input[contains(translate(@value, "PLACE", "place"), "place")]').click() 258 | flag = True 259 | except: 260 | sleep(0.05) 261 | 262 | if __name__ == "__main__": 263 | json_file = 'test.json' 264 | bot = snkr_bot(json_file) 265 | bot.start_test() 266 | 267 | --------------------------------------------------------------------------------