-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
136 lines (113 loc) · 5.14 KB
/
main.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
import time
from os import environ
from os.path import isdir
from traceback import print_exc, format_exc
import telegram
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException, WebDriverException
def notify(message: str):
token = environ.get('TELEGRAM_TOKEN')
if not token:
return
chat_id = environ.get('TELEGRAM_CHAT_ID')
if not chat_id:
return
bot = telegram.Bot(token)
bot.send_message(chat_id=chat_id, text=message, disable_web_page_preview=True,
parse_mode=telegram.ParseMode.MARKDOWN)
def send_keys_slow(el, text, delay=0.1):
for c in text:
el.send_keys(c)
time.sleep(delay)
def do_magic():
login = environ['LOGIN']
password = environ['PASSWORD']
host = environ.get('SELENIUM_HOST', 'selenium')
driver = webdriver.Remote(command_executor='http://%s:4444/wd/hub' % host,
desired_capabilities=DesiredCapabilities.CHROME)
try:
driver.set_window_size(1440, 900)
driver.get('https://global.americanexpress.com/login')
login_input = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'eliloUserID'))
)
send_keys_slow(login_input, login)
send_keys_slow(driver.find_element_by_id('eliloPassword'), password)
btn = driver.find_element_by_id('loginSubmit')
btn.click()
WebDriverWait(driver, 10).until(EC.staleness_of(btn))
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//h2[text()="Total balance"]'))
)
driver.get('https://global.americanexpress.com/offers/eligible')
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//span[text()="Amex Offers"]'))
)
try:
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'offer-category-menu'))
)
except TimeoutException as e:
alert_xpath = '//section[@class="offers-list"]//div[contains(@class, "alert-dialog")]//p/span'
notice = None
try:
notice = driver.find_element_by_xpath(alert_xpath)
except NoSuchElementException:
print('Notice not found')
pass
if not notice or notice.text != 'You currently have no available Offers. Please try again later.':
if notice:
print('Unexpected notice text: "%s"' % notice.text)
raise e
offers = driver.find_elements_by_xpath('//section[@class="offers-list"]/section/div[@data-rowtype="offer"][.//button/span[text()="Save to Card"]]')
if offers:
print('Found offers: %d' % len(offers))
try:
WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '//div[@class="consentContainer"]//input[@id="sprite-DeclineButton_EN"]'))
).click()
except TimeoutException:
pass
driver.execute_script('var btn = document.getElementById("lpButtonDiv"); if (btn) btn.remove();')
for offer in offers:
details = offer.find_elements_by_xpath('.//div[contains(concat(" ", @class, " "), " offer-info ")]/p')
bonus = details[0].text
merchant = details[1].text
print('%s: %s... ' % (merchant, bonus), end='')
try:
element = driver.find_element_by_css_selector('#consentContainer #sprite-ContinueButton_EN')
if element.is_displayed():
element.click()
except NoSuchElementException:
try:
element = driver.find_element_by_css_selector('#consentContainer #sprite-AcceptButton_EN')
if element.is_displayed():
element.click()
except NoSuchElementException:
pass
btn = offer.find_element_by_xpath('.//button[@type="button"][./span]')
btn.click()
WebDriverWait(driver, 15).until(EC.staleness_of(btn))
print('saved')
notify('AMEX offer saved — %s: %s ' % (merchant, bonus))
except WebDriverException:
print_exc()
notify('Exception from AmEx saver: \n```\n' + format_exc() + '\n```')
if isdir('./screenshots'):
filename = './screenshots/%d.png' % time.time()
driver.save_screenshot(filename)
print('Got exception! Screenshot saved to %s' % filename)
finally:
driver.quit()
if __name__ == '__main__':
while True:
do_magic()
sleep = int(environ.get('RESTART_DELAY', 0))
if sleep > 0:
time.sleep(sleep)
else:
break