forked from 5igm4/buysol-lysol-miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buysol.py
147 lines (117 loc) · 4.42 KB
/
buysol.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
import os
import time
from logger import l
from selenium.common.exceptions import NoSuchElementException
LOGIN_ID = os.environ.get('UUID', None)
LOGIN_PASSWORD = os.environ.get('PASS', None)
# https://www.amazon.com/Accoutrements-11761-Yodelling-Pickle/dp/B0010VS078/ref=sr_1_1
ITEM_URL = os.environ.get('ITEM', None)
HOME = 'https://www.amazon.com/'
CART = 'https://www.amazon.com/gp/cart/view.html/ref=dp_atch_dss_cart'
ACCEPT_SHOP = 'Amazon'
LIMIT_VALUE = 100.00
def login(b):
# Tries to log you in
l('Attempting to sign-in')
b.get(HOME)
b.find_element_by_id("nav-link-accountList").click()
b.find_element_by_id('ap_email').send_keys(LOGIN_ID)
b.find_element_by_id('continue').click()
b.find_element_by_id('ap_password').send_keys(LOGIN_PASSWORD)
b.find_element_by_id('signInSubmit').click()
l('Successfully signed-in as: {}'.format(b.find_element_by_id(
"nav-link-accountList").text.split(' ')[1].split('\n')[0]))
def check_item_stock(b):
"""Checks if item is unavailable
Refreshes every 10 seconds until item is in-stock
"""
l("Loading item page")
outOfStock = True
b.get(ITEM_URL)
l("Loaded item page")
while(outOfStock):
l("Checking item stock")
try:
b.find_element_by_id("outOfStock")
l("Item is outOfStock")
b.refresh()
except NoSuchElementException as e:
try:
b.find_element_by_id("merchant-info")
l("Item is in-stock!")
outOfStock = False
continue
except NoSuchElementException as e:
time.sleep(0.33)
b.refresh()
continue
return
def verify_seller(b):
# verify that the seller is (fulfilled by) Amazon
l("Verifying Seller")
element = b.find_element_by_id("merchant-name")
shop = element.text.find(ACCEPT_SHOP)
if shop == -1:
raise Exception("Amazon is not the seller")
l("Successfully verified Seller")
def add_to_cart(b):
# adds the item to our cart
l("Attempting to add to cart")
try:
# sometimes, amazon has a box called "Other Sellers on Amazon"
# underneath the regular buy-box. This will select the first
# option if it's available
b.find_element_by_id("mbc-buybutton-addtocart-1").click()
except BaseException:
try:
l('Checking if See-all-buying-choices loop')
b.find_element_by_id(
"buybox-see-all-buying-choices").click()
l("See-all-buying-choices loop")
b.find_element_by_name("submit.addToCart").click()
except BaseException:
try:
# check if amazon wants us to sub
l("Check if subscription is offered")
b.find_element_by_id("oneTimeBuyBox").click()
l("Selected one time purchase")
except BaseException:
l("No subscription offered")
pass
# not under except clause here because
# add-to-cart-button can be on either a statndard
# page, or after "one time purchase" is selected
b.find_element_by_id("add-to-cart-button").click()
l("Successfully added to cart")
def place_order(b):
"""Purchases the item using default settings
"""
l("Proceeding to checkout")
try:
b.find_element_by_id("sc-buy-box-ptc-button").click()
l("Placing the order")
count = 0
while( count is not 3 ):
try:
b.find_element_by_id("submitOrderButtonId").click()
l("Successfully placed order!")
return
except BaseException as err:
count += 1
l("Failed to place order: {}".format(err))
l("Retrying {}".format(count))
l("Page Source: {}".format(b.page_source))
raise Exception("Failed to find submit order button")
except BaseException as err:
l("Failed to proceed to checkout: {}".format(err))
raise Exception("Failed to complete checkout")
def print_subtotal(b):
"""Goes to cart and prints the subtotal
Since page navigates to cart this also preps
for checkout.
"""
b.get(CART)
# time.sleep(0.5)
sub_total = b.find_element_by_id(
"sc-subtotal-label-buybox").text + b.find_element_by_id("sc-subtotal-amount-buybox").text
l('Subtotal: {}'.format(sub_total))