-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_alert.py
310 lines (249 loc) · 10.9 KB
/
run_alert.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from twilio.rest import TwilioRestClient
import logging
import os
import time
import getopt
import sys
import csv
import smtplib
logger = logging.getLogger('alert')
# config file
cfg = {}
with open('alerts.conf') as hlr:
for line in hlr:
split_line = line.split('::')
cfg[split_line[0].strip()] = split_line[1].strip()
# driver
if cfg['browser'] == 'Chrome-OSX':
driver = webdriver.Chrome()
elif cfg['browser'] == 'Chrome':
driver_path = os.path.join(os.path.dirname(__file__), 'chromedriver.exe')
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
whl = "#busyindicator svg"
def alert():
# for testing and setting up csv file
# _write_row_csv(['Name', 'Status', 'Phone', 'Failure', 'Amount', 'Date'])
driver.get(cfg['site_url'])
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.ID, 'BusinessCode'))).send_keys(cfg['code'])
driver.find_element_by_css_selector('#UserName').send_keys(cfg['username'])
driver.find_element_by_css_selector('#Password').send_keys(cfg['pass'])
driver.find_element_by_css_selector('.button-area button').click()
_wait_for_spinning_wheel_loaded()
time.sleep(2)
_navigate_to_customer_page()
tbl_tr = ".clickable-table tr.trk_customer_record"
els = WebDriverWait(driver, 30).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, tbl_tr)))
for r_, el in enumerate(els, 1):
logger.debug('Row ({}): {}'.format(r_, el.text))
logger.info('Total rows (users) found: {}'.format(len(els)))
ppl_to_alert = []
for i in range(1, len(els) + 1):
person_to_alert = []
for y in range(1, 8):
item = "tr.trk_customer_record:nth-of-type({})>td:nth-of-type({})".format(i, y)
el = driver.find_element_by_css_selector(item)
row_data = el.text.strip()
# logger.debug(row_data)
if y in [1, 2, 5]:
person_to_alert.append(row_data)
# only Active people
# Ended people are omitted
logger.info('User: {}, state: {}'.format(person_to_alert[0], person_to_alert[1]))
if person_to_alert[1] == "Active":
item = "tr.trk_customer_record:nth-of-type({})>td:nth-of-type(1)".format(i)
driver.find_element_by_css_selector(item).click()
# spinning wheel appears twice
for _ in range(2):
_wait_for_spinning_wheel_loaded()
time.sleep(2)
_document_ready()
logger.info("Fetching user direct debits: {}".format(person_to_alert[0]))
tbl_tr_ = '.clickable-table .debit-details'
els_ = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, tbl_tr_)))
# scan the table for Failed
# break if Success or Written off appears first
for row_ in range(1, len(els_) + 1):
item = ".debit-details:nth-of-type({})".format(row_)
el = driver.find_element_by_css_selector(item)
line_= el.text.strip()
logger.info(line_)
if line_.count('Success') or line_.count('Written-Off') and cfg['test'].lower() != 'true':
break
elif line_.count('Failed'):
for td_ in range(2, 5):
it_ = ".debit-details:nth-of-type({})>td:nth-of-type({})".format(row_, td_)
td_text_ = driver.find_element_by_css_selector(it_).text
td_text_ = td_text_.replace('\n',' ')
person_to_alert.append(td_text_)
email_ = _get_email(person_to_alert[0])
person_to_alert.append(email_)
# all data retrieved
# add to people to alert
ppl_to_alert.append(person_to_alert)
logger.info('user added to alert list: {}'.format(person_to_alert))
break
_navigate_to_customer_page()
logger.info('reading all alerts')
csv_ = _read_alerts()
for prs_ in ppl_to_alert:
for csv_alert in csv_:
# user name and date
# if match we dont send out
if prs_[0] == csv_alert[0] and prs_[5] == csv_alert[5]:
logger.info('Alert already sent: {} on: {}. Ignoring.'.format(prs_[0], csv_alert[5]))
break
else:
logger.info('Alert not sent yet to: {}'.format(prs_[0]))
_text_message(prs_)
_send_email(prs_)
_write_alert(prs_)
time.sleep(1)
def _document_ready():
try:
WebDriverWait(driver, 20).until(lambda d: d.execute_script('return document.readyState') == 'complete')
logger.debug('Document ready')
except TimeoutException:
logger.warning('Document not ready!')
def _wait_for_spinning_wheel():
try:
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, whl)))
except TimeoutException:
logger.debug('Spinning wheel has not appeared')
def _wait_for_spinning_wheel_loaded():
_wait_for_spinning_wheel()
for _ in range(60):
try:
WebDriverWait(driver, 0.5).until_not(EC.presence_of_element_located((
By.CSS_SELECTOR, whl)))
return True
except TimeoutException:
logger.debug('Spinning the page. Updating')
def _write_alert(row):
with open('alerts.csv', 'ab') as hlr:
wrt = csv.writer(hlr, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
wrt.writerow(row)
logger.info('Alert added to .csv file: {}'.format(row))
def _read_alerts():
with open('alerts.csv', 'rb') as hlr:
rd = csv.reader(hlr, delimiter=',', quotechar='"')
return [row for row in rd if row[1] != "Status"]
def _navigate_to_customer_page():
try:
driver.get(cfg['customers_url'])
time.sleep(2)
_wait_for_spinning_wheel_loaded()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.showMore-button'))).click()
logger.debug('Show more clicked')
_wait_for_spinning_wheel()
_wait_for_spinning_wheel_loaded()
except:
logger.error('_navigate_to_customer_page failed: ', exc_info=True)
def _get_email(name_):
try:
with open('users.csv', 'rb') as hlr:
rd = csv.reader(hlr, delimiter=',', quotechar='"')
users_csv = [row for row in rd if row[1] != "Name"]
for user_csv in users_csv:
if user_csv[0] == name_ and user_csv[1] != '':
logger.debug('Email found: {} for user: {}'.format(user_csv[1], user_csv[0]))
return user_csv[1]
else:
logger.debug('Email not found for: {}'.format(name_))
except:
logger.error('_get_email failed: ', exc_info=True)
def _text_message(text_recipient):
logger.info('Texting: {}'.format(text_recipient))
# recipient phone number
phone_number = "+61" + text_recipient[2][1:]
logger.info('Phone number: {}'.format(phone_number))
if cfg['test'].lower() == 'true':
logger.debug('Intended recipient: {}'.format(phone_number))
phone_number = cfg['twilio_to_test']
logger.debug('Test mode, text recipient: {}'.format(phone_number))
acc = cfg['twilio_account']
token = cfg['twilio_token']
from_number = cfg['twilio_from']
text_content = cfg['text_content']
try:
client = TwilioRestClient(acc, token)
message = client.messages.create(to=phone_number, from_=from_number,
body=text_content)
sid = message.sid
body = client.messages.get(sid)
status = body.status
logger.info('Message sent to: {}, status: {}'.format(phone_number, status))
except:
logger.error('Text message failed to send to: {}'.format(phone_number), exc_info=True)
logger.info('Sending a copy of message to: {}'.format(from_number))
try:
client = TwilioRestClient(acc, token)
message = client.messages.create(to=from_number, from_=from_number,
body=text_content)
sid = message.sid
body = client.messages.get(sid)
status = body.status
logger.info('Message sent to: {}, status: {}'.format(from_number, status))
except:
logger.error('Text message failed to send to: {}'.format(from_number), exc_info=True)
def _send_email(recipient):
logger.debug('Emailing: {}'.format(recipient))
# recipient
name_recipient = recipient[0]
email_recipient = recipient[6]
if cfg['test'].lower() == 'true':
logger.debug('Intended recipient: {}'.format(email_recipient))
email_recipient = cfg['gmail_user']
logger.debug('Test mode, email recipient: {}'.format(email_recipient))
if not recipient:
logger.info('Email not provided, not sent out to: {}'.format(name_recipient))
return
if cfg['gmail_method'] == 'less-secure':
gmail_user = cfg['gmail_user']
gmail_pwd = cfg['gmail_pwd']
email_sender = cfg['gmail_user']
email_subject = cfg['email_subject']
email_content = cfg['email_content']
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (email_sender, email_recipient, email_subject, email_content)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(email_sender, email_recipient, message)
server.close()
logger.info('Email sent to: {}'.format(email_recipient))
except:
logger.error('Email failed to send to: {}'.format(email_recipient), exc_info=True)
if __name__ == '__main__':
verbose = None
timestamp = time.strftime('%d%m%y', time.localtime())
log_file = os.path.join(os.path.dirname(__file__), timestamp + ".log")
file_hndlr = logging.FileHandler(log_file)
logger.addHandler(file_hndlr)
console = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(console)
ch = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
console.setFormatter(ch)
file_hndlr.setFormatter(ch)
argv = sys.argv[1:]
opts, args = getopt.getopt(argv, "c:v", ["csv=", "verbose"])
for opt, arg in opts:
if opt in ("-c", "--csv"):
csv_file = arg
elif opt in ("-v", "--verbose"):
verbose = True
if verbose:
logger.setLevel(logging.getLevelName('DEBUG'))
else:
logger.setLevel(logging.getLevelName('INFO'))
logger.info('Alert script started.')
logger.debug('CLI args: {}'.format(opts))
alert()
driver.quit()