Skip to content

Commit

Permalink
fixup: Format Python code with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Sep 25, 2020
1 parent 2fd7acc commit 5c99e51
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 29 deletions.
1 change: 1 addition & 0 deletions cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 44 additions & 19 deletions stores/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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.")
Expand All @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions stores/nvidia.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
19 changes: 11 additions & 8 deletions utils/selenium_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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()

Expand Down

0 comments on commit 5c99e51

Please sign in to comment.