From 5c99e5186a7a1dc5949d1d889d3c84c87e231aca Mon Sep 17 00:00:00 2001 From: autoblack Date: Fri, 25 Sep 2020 17:56:05 +0000 Subject: [PATCH] fixup: Format Python code with Black --- cli/cli.py | 1 + stores/amazon.py | 63 ++++++++++++++++++++++++++++------------- stores/nvidia.py | 2 -- utils/selenium_utils.py | 19 +++++++------ 4 files changed, 56 insertions(+), 29 deletions(-) diff --git a/cli/cli.py b/cli/cli.py index 8e6bfe72..c0a244fd 100644 --- a/cli/cli.py +++ b/cli/cli.py @@ -43,6 +43,7 @@ def amazon(no_image, headless, test): amzn_obj = Amazon(headless=headless) amzn_obj.run_item(test=test) + @click.command() @click.option("--sku", type=str, required=True) @click.option("--headless", is_flag=True) diff --git a/stores/amazon.py b/stores/amazon.py index c175aa78..19bc6c3e 100644 --- a/stores/amazon.py +++ b/stores/amazon.py @@ -18,16 +18,28 @@ AMAZON_URLS = { "BASE_URL": "https://www.{}/", - "CART_URL": "https://www.{}/gp/aws/cart/add.html" + "CART_URL": "https://www.{}/gp/aws/cart/add.html", } AUTOBUY_CONFIG_PATH = "amazon_config.json" -SIGN_IN_TITLES = ["Amazon Sign In", "Amazon Sign-In", "Amazon Anmelden"] -SHOPING_CART_TITLES = ["Amazon.com Shopping Cart", "Amazon.co.uk Shopping Basket", "Amazon.de Basket"] -CHECKOUT_TITLES = ["Amazon.com Checkout", "Place Your Order - Amazon.co.uk Checkout", "Place Your Order - Amazon.de Checkout"] +SIGN_IN_TITLES = ["Amazon Sign In", "Amazon Sign-In", "Amazon Anmelden"] +SHOPING_CART_TITLES = [ + "Amazon.com Shopping Cart", + "Amazon.co.uk Shopping Basket", + "Amazon.de Basket", +] +CHECKOUT_TITLES = [ + "Amazon.com Checkout", + "Place Your Order - Amazon.co.uk Checkout", + "Place Your Order - Amazon.de Checkout", +] ORDER_COMPLETE_TITLES = ["Amazon.com Thanks You", "Thank you"] -ADD_TO_CART_TITLES = ["Amazon.com: Please Confirm Your Action", "Amazon.de: Bitte bestätigen Sie Ihre Aktion", "Amazon.de: Please Confirm Your Action"] +ADD_TO_CART_TITLES = [ + "Amazon.com: Please Confirm Your Action", + "Amazon.de: Bitte bestätigen Sie Ihre Aktion", + "Amazon.de: Please Confirm Your Action", +] class Amazon: @@ -48,23 +60,29 @@ def __init__(self, headless=False): self.amazon_website = config.get("amazon_website", "amazon.com") assert isinstance(self.asin_list, list) except Exception: - raise InvalidAutoBuyConfigException("amazon_config.json file not formatted properly.") + raise InvalidAutoBuyConfigException( + "amazon_config.json file not formatted properly." + ) else: raise InvalidAutoBuyConfigException("Missing amazon_config.json file.") for key in AMAZON_URLS.keys(): AMAZON_URLS[key] = AMAZON_URLS[key].format(self.amazon_website) print(AMAZON_URLS) - self.driver.get(AMAZON_URLS['BASE_URL']) + self.driver.get(AMAZON_URLS["BASE_URL"]) if self.is_logged_in(): log.info("Already logged in") else: log.info("Lets log in.") - selenium_utils.button_click_using_xpath(self.driver, '//*[@id="nav-link-accountList"]/div/span') + selenium_utils.button_click_using_xpath( + self.driver, '//*[@id="nav-link-accountList"]/div/span' + ) selenium_utils.wait_for_any_title(self.driver, SIGN_IN_TITLES) self.login() log.info("Waiting 15 seconds.") - time.sleep(15) # We can remove this once I get more info on the phone verification page. + time.sleep( + 15 + ) # We can remove this once I get more info on the phone verification page. def is_logged_in(self): try: @@ -96,19 +114,19 @@ def run_item(self, delay=3, test=False): log.info("Checking stock for items.") while not self.something_in_stock(): time.sleep(delay) - self.notification_handler.send_notification("Your items on Amazon.com were found!") + self.notification_handler.send_notification( + "Your items on Amazon.com were found!" + ) self.checkout(test=test) def something_in_stock(self): - params = { - 'anticache': str(secrets.token_urlsafe(32)) - } + params = {"anticache": str(secrets.token_urlsafe(32))} for x in range(len(self.asin_list)): - params[f'ASIN.{x + 1}'] = self.asin_list[x] - params[f'Quantity.{x + 1}'] = 1 + params[f"ASIN.{x + 1}"] = self.asin_list[x] + params[f"Quantity.{x + 1}"] = 1 - f = furl(AMAZON_URLS['CART_URL']) + f = furl(AMAZON_URLS["CART_URL"]) f.set(params) self.driver.get(f.url) selenium_utils.wait_for_any_title(self.driver, ADD_TO_CART_TITLES) @@ -131,7 +149,10 @@ def wait_for_pyo_page(self): self.login() def finalize_order_button(self, test): - button_xpaths = ['//*[@id="bottomSubmitOrderButtonId"]/span/input', '//*[@id="placeYourOrder"]/span/input'] + button_xpaths = [ + '//*[@id="bottomSubmitOrderButtonId"]/span/input', + '//*[@id="placeYourOrder"]/span/input', + ] button = None for button_xpath in button_xpaths: try: @@ -150,7 +171,9 @@ def wait_for_order_completed(self, test): if not test: selenium_utils.wait_for_any_title(self.driver, ORDER_COMPLETE_TITLES) else: - log.info("This is a test, so we don't need to wait for the order completed page.") + log.info( + "This is a test, so we don't need to wait for the order completed page." + ) def checkout(self, test): log.info("Clicking continue.") @@ -160,7 +183,9 @@ def checkout(self, test): self.wait_for_cart_page() log.info("clicking checkout.") - self.driver.find_element_by_xpath('//*[@id="sc-buy-box-ptc-button"]/span/input').click() + self.driver.find_element_by_xpath( + '//*[@id="sc-buy-box-ptc-button"]/span/input' + ).click() log.info("Waiting for Place Your Order Page") self.wait_for_pyo_page() diff --git a/stores/nvidia.py b/stores/nvidia.py index 1bc9aaef..26db1953 100644 --- a/stores/nvidia.py +++ b/stores/nvidia.py @@ -158,8 +158,6 @@ def __init__(self): super().__init__("Product IDS changed. We need to re run.") - - PRODUCT_IDS_FILE = "stores/store_data/nvidia_product_ids.json" PRODUCT_IDS = json.load(open(PRODUCT_IDS_FILE)) diff --git a/utils/selenium_utils.py b/utils/selenium_utils.py index c8ac984a..bb811364 100644 --- a/utils/selenium_utils.py +++ b/utils/selenium_utils.py @@ -12,18 +12,22 @@ class AnyEc: - """ Use with WebDriverWait to combine expected_conditions - in an OR. + """Use with WebDriverWait to combine expected_conditions + in an OR. """ + def __init__(self, *args): self.ecs = args + def __call__(self, driver): for fn in self.ecs: try: - if fn(driver): return True + if fn(driver): + return True except: pass + def no_amazon_image(): prefs = {"profile.managed_default_content_settings.images": 2} options.add_experimental_option("prefs", prefs) @@ -72,9 +76,7 @@ def wait_for_either_title(d, title1, title2, time=30): Uses webdriver(d) to wait for page title(title1 or title2) to become visible """ try: - WebDriverWait(d, time).until(AnyEc( - ec.title_is(title1), - ec.title_is(title2))) + WebDriverWait(d, time).until(AnyEc(ec.title_is(title1), ec.title_is(title2))) except Exception: pass @@ -90,12 +92,13 @@ def wait_for_any_title(d, titles, time=30): WebDriverWait(d, time).until(AnyEc(*my_args_list)) - def button_click_using_xpath(d, xpath): """ Uses webdriver(d) to click a button using an XPath(xpath) """ - button_menu = WebDriverWait(d, 10).until(ec.element_to_be_clickable((By.XPATH, xpath))) + button_menu = WebDriverWait(d, 10).until( + ec.element_to_be_clickable((By.XPATH, xpath)) + ) action = ActionChains(d) action.move_to_element(button_menu).pause(1).click().perform()