forked from AFTERWAKE/giftcards_galore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
giftcards_galore.py
executable file
·204 lines (173 loc) · 8.2 KB
/
giftcards_galore.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""Auto buy Amazon Giftcards"""
import os
import random
import re
import sys
import time
from dotenv import load_dotenv
from selenium import webdriver
from selenium.common.exceptions import *
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
# Load env variables from ".env" file in the same folder
load_dotenv()
# User-defined variables
# Your Amazon username (email)
AMAZON_USERNAME = os.getenv("AMAZON_USERNAME")
# Your Amazon password
AMAZON_PASSWORD = os.getenv("AMAZON_PASSWORD")
# 0-indexed array of your Amazon payment methods
# Refer to https://www.amazon.com/gp/wallet
CARDS = [0, 1, 2, 3, 4, 5, 6]
# Your credit card numbers, corresponding to the index of each card in the CARDS array
CARD_NUMBERS = [os.getenv("CC0"), os.getenv("CC1"), os.getenv("CC2"), os.getenv("CC3")]
# Iterations array, corresponds to the number of purchases for each card
ITERATIONS = [0, 0, 0, 0, 12, 0, 12]
# Amount to be loaded onto each gift card
GIFT_CARD_AMOUNT = os.getenv("GIFT_CARD_AMOUNT")
# Automatically get and cache the webdriver for Chrome
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
class AuthenticationError(Exception):
pass
# Function to buy the giftcards
def giftcard_buyer():
wait = WebDriverWait(driver, 10)
print(AMAZON_USERNAME)
driver.get("https://www.amazon.com/asv/reload/")
try:
wait.until(expected_conditions.title_contains("Gift Card Balance Reload"))
print("Select one time reload")
# Note the .// for finding child button in the xpath
driver.find_element(By.ID, "reload_type_0").find_element(By.XPATH, ".//button").click()
time.sleep(1)
driver.find_element(By.ID, "gc-ui-form-custom-amount").send_keys(str(GIFT_CARD_AMOUNT))
time.sleep(1)
inputs = driver.find_elements(By.NAME, "submit.gc-buy-now")
for x in inputs:
if x.is_displayed():
x.click()
print("Sign in")
# Added wait times between most page loads because the driver was going too fast
# The wait.until() did not seem to work. Could probably change the wait.until to check for the next element.
time.sleep(4)
wait.until(expected_conditions.title_contains("Amazon Sign-In"))
# time.sleep(4)
driver.find_element(By.ID, "ap_email").send_keys(AMAZON_USERNAME)
except:
print("Error?")
print(sys.exc_info()[0])
pass
driver.find_element(By.ID, "continue").click()
time.sleep(3)
driver.find_element(By.ID, "ap_password").send_keys(AMAZON_PASSWORD)
driver.find_element(By.ID, "signInSubmit").click()
time.sleep(12)
# Not sure how to handle 2FA. Manually inputting during the previous sleep, but the button presses and the rest could be automated.
# Now there's a page asking for phone number. That could be automatically skipped as well.
try:
driver.find_element(By.XPATH, "//h1[contains(.,'Authentication Required')]")
raise AuthenticationError("You need to manually confirm your login")
except NoSuchElementException:
print("Error?")
print(sys.exc_info()[0])
pass
i = 0
for card in CARDS:
print(f"card: {card}")
for iteration in range(ITERATIONS[i]):
print(f"iteration: {iteration}")
# Submit new gift card amount after the first time, but also for the first iteration of each subsequent card
if iteration != 0 or (card > 0 and iteration == 0 and ITERATIONS[card - 1] != 0):
print("New iteration gift card amount setup")
wait.until(expected_conditions.title_contains("Gift Card Balance Reload"))
print("Select one time reload")
# Note the .// for finding child button in the xpath
driver.find_element(By.ID, "reload_type_0").find_element(By.XPATH, ".//button").click()
time.sleep(1)
print("Input gift card amount")
driver.find_element(By.ID, "gc-ui-form-custom-amount").send_keys(str(GIFT_CARD_AMOUNT))
time.sleep(1)
inputs = driver.find_elements(By.NAME, "submit.gc-buy-now")
for x in inputs:
if x.is_displayed():
x.click()
# One time I was prompted for the password and token again. Haven't ran into it again yet, so this can probably be deleted.
try:
driver.find_element(By.ID, "ap_password")
driver.find_element(By.ID, "ap_password").send_keys(AMAZON_PASSWORD)
driver.find_element(By.ID, "signInSubmit").click()
time.sleep(12)
except NoSuchElementException:
print("No need to log in again, right?")
print(sys.exc_info()[0])
pass
time.sleep(2)
print("Click change card link")
try:
wait.until(expected_conditions.presence_of_element_located((By.XPATH, "//a[contains(@href,'chg_payselect')]")))
driver.find_element(By.XPATH, "//a[contains(@href,'chg_payselect')]").click()
time.sleep(6)
except:
print("Error?")
print(sys.exc_info()[0])
try:
driver.find_element(By.ID, "payChangeButtonId").click()
time.sleep(4)
except:
print("Error?")
print(sys.exc_info()[0])
pass
pass
print("Click CC radio button")
driver.find_elements(By.XPATH, "//input[@type='radio']")[card].click()
time.sleep(4)
# Searching for placeholder$='<last-4-of-card>'. Needs single quotes. Always only 1 card, so [0].
try:
cc_input_box = driver.find_elements(By.CSS_SELECTOR, "[placeholder$='" + CARD_NUMBERS[card][-4:] + "']")[0]
if cc_input_box:
try:
print("Found verification input")
print("Sending CC number to input box")
cc_input_box.send_keys(CARD_NUMBERS[card])
time.sleep(random.randint(1, 5))
print("Verify card")
# Find Verify button by cc_input_box_id adding 1 to the trailing digits.
# When the page reloads the previously verified cards do not render those buttons,
# so the array can't be trusted to be the same each time.
cc_input_box_id = cc_input_box.get_attribute("id") # pp-kFRldw-171
button_id = re.sub("[0-9]+$", str(int(re.match(".*?([0-9]+)$", cc_input_box_id).group(1)) + 1), cc_input_box_id) #
driver.find_elements(By.XPATH, "//button[contains(@id,'" + button_id + "')]")[0].click()
time.sleep(random.randint(1, 5))
except:
print("Error?")
print(sys.exc_info()[0])
except:
print("No need for CC validation?")
print(sys.exc_info()[0])
pass
try:
use_this_card = driver.find_element(By.XPATH, "//input[@aria-labelledby='orderSummaryPrimaryActionBtn-announce']")
if use_this_card:
print("Click use this card button")
use_this_card.click()
del use_this_card
time.sleep(4)
except:
print("Error?")
print(sys.exc_info()[0])
pass
print("Click place your order button")
driver.find_element(By.XPATH, "//input[@aria-labelledby='submitOrderButtonId-announce']").click()
time.sleep(8)
print("Continue loop")
driver.get("https://www.amazon.com/asv/reload/")
time.sleep(4)
i += 1
driver.quit()
try:
giftcard_buyer()
except:
driver.quit