-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
215 lines (191 loc) · 5.26 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
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
var config = require('./config');
var casper = require('casper').create({
clientScripts: ['./jquery.min.js'],
});
var OAuth = require('oauth-1.0a');
var jsSHA = require('jssha');
var oauth = OAuth({
consumer: {
key: config.twitter.consumerKey,
secret: config.twitter.consumerSecret,
},
signature_method: 'HMAC-SHA1',
hash_function: function(base_string, key) {
var shaObj = new jsSHA('SHA-1', 'TEXT');
shaObj.setHMACKey(key, 'TEXT');
shaObj.update(base_string);
return shaObj.getHMAC('B64');
},
});
var url = 'https://goes-app.cbp.dhs.gov/goes/jsp/login.jsp';
var username = config.username;
var password = config.password;
var airport = config.airport;
var accountSid = config.twilio.accountSid;
var serviceSid = config.twilio.serviceSid;
var authToken = config.twilio.authToken;
var toNumber = config.twilio.toNumber;
var fromNumber = config.twilio.fromNumber;
var twilioLinked = (
accountSid &&
serviceSid &&
authToken &&
toNumber &&
fromNumber
);
var twitterLinked = (
config.twitter.accessToken &&
config.twitter.accessTokenSecret &&
config.twitter.consumerKey &&
config.twitter.consumerSecret
);
var providedDay;
var notify;
function CasperException(message, stack) {
this.name = 'CasperException';
this.message = message;
this.stack = stack;
}
casper.on('error', function(msg, backtrace) {
this.echo('Exception: ' + msg + backtrace);
this.capture('./out/error.png');
throw new CasperException(msg, backtrace);
});
casper.on('remote.message', function(msg) {
this.echo('remote console.log:' + msg);
});
casper.start(url);
casper.then(function() {
this.echo('Landed on page: ' + this.getTitle());
});
casper.then(function() {
this.echo('Clicking on login button...');
this.evaluate(function(u, p) {
$('#j_username').val(u);
$('#j_password').val(p);
$('#SignIn').click();
}, username, password);
});
casper.then(function() {
this.echo('Waiting for checkbox...');
this.waitForSelector('#checkMe');
});
casper.then(function() {
this.echo('Checkbox found. Clicking on checkbox...');
this.evaluate(function() {
$('#checkMe').click();
});
});
casper.then(function() {
this.echo('Waiting on manage appointments button...');
this.waitForSelector('input[name=manageAptm]');
});
casper.then(function() {
this.echo('Appointments button found. Clicking on button...');
this.evaluate(function() {
$('input[name=manageAptm]').click();
});
});
casper.then(function() {
this.echo('Waiting on reschedule appointment button...');
this.waitForSelector('input[name=reschedule]');
});
casper.then(function() {
this.echo('Reschedule button found. Clicking on button...');
this.evaluate(function() {
$('input[name=reschedule]').click();
});
});
casper.then(function() {
this.echo('Waiting on airport table...');
this.waitForSelector('.sectionheader');
});
casper.then(function() {
this.echo('Airport table found, selecting next...');
this.evaluate(function(a) {
$('input[value=' + a + ']').click();
$('input[name=next]').click();
}, airport);
});
casper.then(function() {
this.echo('Waiting on calendar to render...');
this.waitForSelector('.date');
});
casper.then(function() {
this.echo('Calendar found. Parsing date...');
providedDay = this.evaluate(function() {
var day = $('.date td')[0].innerHTML;
console.log(day);
var monthYear = $('.date div')[1].innerText;
console.log(monthYear);
return day + ' ' + monthYear;
});
});
casper.then(function() {
this.echo('Date found: ' + providedDay);
var nextDay = new Date(providedDay);
var today = new Date();
console.log('Next available: ' + nextDay);
console.log('Today: ' + today);
var oneDay = 1000 * 60 * 60 * 24;
var numDays = Math.ceil(
(nextDay.getTime() - today.getTime()) / (oneDay)
);
this.echo('Number of days away: ' + numDays);
if (numDays < config.threshold) {
notify = true;
this.echo('New appointment slot available within a month');
} else {
notify = false;
this.echo('No appointment slots available within a month');
}
});
casper.then(function() {
if (twilioLinked && notify) {
this.echo('Sending twilio request...');
this.open(
'https://' + accountSid + ':' + authToken + '@' +
'api.twilio.com/2010-04-01/Accounts/' + accountSid + '/Messages',
{
method: 'post',
data: {
To: toNumber,
From: fromNumber,
Body: 'New appointment slot open: ' + providedDay,
MessagingServiceSid: serviceSid,
},
}
).then(function() {
require('utils').dump(this.getPageContent());
});
}
});
casper.then(function() {
if (twitterLinked && notify) {
this.echo('Sending twitter request...');
var message = 'New appointment slot open: ' + providedDay;
var requestData = {
url: 'https://api.twitter.com/1.1/statuses/update.json',
method: 'POST',
data: {
status: message,
},
};
var token = {
key: config.twitter.accessToken,
secret: config.twitter.accessTokenSecret,
};
this.open(
requestData.url,
{
method: requestData.method,
data: requestData.data,
headers: oauth.toHeader(oauth.authorize(requestData, token)),
}
);
}
});
casper.run(function() {
this.echo('Done');
this.exit();
});