-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
155 lines (132 loc) · 3.89 KB
/
index.js
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
var Q = require('q');
var clc = require('cli-color');
var assert = require('assert');
var prompt = require('prompt');
var moment = require('moment');
var zombie = require('zombie');
var nodemailer = require('nodemailer');
var config = require('./config');
var browser = new zombie;
var dateFormat = 'dddd D MMMM YYYY h:mma';
var currentTestDate;
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: config.GMAIL_USERNAME,
pass: config.GMAIL_PASSWORD
}
});
var mailOptions = {
from: 'Cancellation Finder',
to: config.RECIPIENT,
subject: 'CANCELLATION FOUND',
text: ''
};
function timestamp() {
return moment().format('DD/MM/YYYY HH:mm:ss:');
}
function log(message) {
console.log(clc.blackBright(timestamp()), message);
}
function logError(message) {
log(clc.red(message));
}
function contains(text, string) {
return text.indexOf(string) > -1 ? true : false;
}
function sendMessage(message) {
mailOptions.text = message;
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
logError('Couldn\'t send email: ' + error);
} else {
log('Message sent: ' + info.response);
}
});
}
function notify(dates) {
var message = 'Found the following dates: \n\n' + dates.join('\n');
log(clc.green(message));
sendMessage(message);
}
function getTestDate() {
var text = browser.query('#confirm-booking-details')
.querySelectorAll('section')[0]
.querySelectorAll('dd')[0]
.innerHTML;
return moment(text, dateFormat);
}
function findCaptchaURL() {
var elem = browser.query('#recaptcha_challenge_image');
if (elem) return elem.src;
}
function solveCaptcha() {
var deferred = Q.defer();
var captchaURL = findCaptchaURL(browser);
if (captchaURL) {
logError('Captcha found: \n' + captchaURL);
prompt.start();
prompt.get(['captcha'], function(error, result) {
browser.fill('#recaptcha_response_field', result.captcha);
if (browser.query('#recaptcha-submit')) deferred.resolve(browser.pressButton('#recaptcha-submit'))
else deferred.resolve(browser);
});
} else {
deferred.resolve(browser);
}
return deferred.promise;
}
function login() {
log('login');
assert(browser.window.location.pathname == '/login', 'Did not reach login page.');
return browser
.fill('#driving-licence-number', config.LICENCE_NUMBER)
.fill('#application-reference-number', config.APPLICATION_REFERENCE_NUMBER)
.pressButton('#booking-login');
}
function gotoChange() {
log('gotoChange');
assert(browser.window.location.pathname == '/manage', 'Did not reach manage page.');
currentTestDate = getTestDate();
return browser.clickLink('#date-time-change');
}
function findDates() {
log('findDates');
assert(browser.window.location.pathname == '/manage' && contains(browser.window.location.search, '?execution=e1s2'), 'Did not reach find dates page.');
return browser.pressButton('#driving-licence-submit');
}
function listDates() {
log('listDates');
assert(browser.window.location.pathname == '/manage' && contains(browser.window.location.search, '?execution=e1s3'), 'Did not reach list dates page.');
var dates = browser.queryAll('.slotDateTime')
.map(function(elem) {
return moment(elem.innerHTML, dateFormat);
})
.filter(function(date) {
return date.isBefore(currentTestDate);
})
.map(function(date) {
return date.format(dateFormat);
});
if (dates.length > 0) {
notify(dates);
} else {
log('No cancellations found.');
}
return browser;
}
function main() {
browser.visit(config.LOGIN_URL)
// Going to avoid captchas by extending the time between checks.
// .then(solveCaptcha)
.then(login)
.then(gotoChange)
.then(findDates)
// .then(solveCaptcha)
.then(listDates)
.fail(function(error) {
logError(error);
});
}
main();
setInterval(main, 1000*60*45); // Check every 45min