-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
209 lines (196 loc) · 7.89 KB
/
app.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
var request = require('request');
var nodemailer = require("nodemailer");
var os = require('os');
var fs = require('fs');
var moment = require('moment');
var CHECK_INTERVAL_MIN = 120;
var EMAIL_ALERT_ENABLED = 'no';
var ALWAYS_ALERT_AFTER_START = 'no';
// Build the post string from an object
var post_data = {
lang : '',
_page: '_target0',
app : '',
identifierType: '1',
identifier : undefined,
surname : undefined,
dateOfBirth : undefined,
countryOfBirth : undefined,
_submit : 'Continue'
};
// setup e-mail data with unicode symbols
var mailOptions = {
from: 'Immigration Tracker', // sender address
to: undefined, // list of receivers
subject: 'Permanent Resident Application Status Update', // Subject line
text: ''
};
var senderGmailInfo = {
service: 'Gmail',
auth: {
user: undefined,
pass: undefined
}
};
var req_options_general = {
url : 'https://services3.cic.gc.ca/ecas/authenticate.do',
method : 'POST',
form : post_data,
jar : true,
followAllRedirects : true
};
var numItems = 0;
process.on('uncaughtException', function (err) {
log('An error was found, the program will end.');
log('Please check your config file and make sure all inputs are correct.');
});
fs.readFile('config.txt', 'utf8', function(err, data) {
if(err) {
return log('Error reading the config file.');
}
readConfigInfo(data);
readClientInfo(data);
// check if client info is valid
if(!isClientInfoValid()) {
return log('Invalid CIC client information.');
}
readEmailInfo(data);
// check if email info is valid
if(!isEmailInfoValid()) {
return log('Invalid Email client information.');
}
checkApplicationStatus();
setInterval(checkApplicationStatus, CHECK_INTERVAL_MIN * 1000 * 60);
});
function readConfigInfo(data) {
var lines = data.toString().replace(/\r/g, '').split('\n');
for(var i = 0; i < lines.length; i++) {
if(lines[i].indexOf('#') !== 0 && lines[i].indexOf('=') !== -1) {
var key = lines[i].split('=');
if(key[0] === 'Check Interval (Minutes)' && key[1] > CHECK_INTERVAL_MIN) {
CHECK_INTERVAL_MIN = key[1];
} else if(key[0] === 'Enable Email Alert') {
EMAIL_ALERT_ENABLED = key[1].toLowerCase();
} else if(key[0] === 'Always Send Alert After Start') {
ALWAYS_ALERT_AFTER_START = key[1].toLowerCase();
}
}
}
log('Check interval set to [' + CHECK_INTERVAL_MIN + '] minutes.');
log('Use Email alert: ' + EMAIL_ALERT_ENABLED + '.');
log('Always send alert after start: ' + ALWAYS_ALERT_AFTER_START + '.');
}
function readClientInfo(data) {
var lines = data.toString().replace(/\r/g, '').split('\n');
for(var i = 0; i < lines.length; i++) {
if(lines[i].indexOf('#') !== 0 && lines[i].indexOf('=') !== -1) {
var key = lines[i].split('=');
if(key[0] === 'UCI') {
post_data.identifier = key[1];
} else if(key[0] === 'Surname') {
post_data.surname = key[1];
} else if(key[0] === 'Date Of Birth (YYYY-MM-DD)') {
post_data.dateOfBirth = key[1];
} else if(key[0] === 'Country Of Birth') {
post_data.countryOfBirth = key[1];
}
}
}
}
function isClientInfoValid() {
return (post_data.identifier && post_data.surname && post_data.dateOfBirth && post_data.countryOfBirth);
}
function readEmailInfo(data) {
var lines = data.toString().replace(/\r/g, '').split('\n');
for(var i = 0; i < lines.length; i++) {
if(lines[i].indexOf('#') !== 0 && lines[i].indexOf('=') !== -1) {
var key = lines[i].split('=');
if(key[0] === 'Send To') {
mailOptions.to = key[1];
} else if(key[0] === 'Subject') {
mailOptions.subject = key[1];
} else if(key[0] === 'Gmail Address') {
senderGmailInfo.auth.user = key[1];
} else if(key[0] === 'Gmail Password') {
senderGmailInfo.auth.pass = key[1];
}
}
}
}
function isEmailInfoValid() {
return (mailOptions.to && mailOptions.subject && senderGmailInfo.auth.user && senderGmailInfo.auth.pass);
}
function getResultStr(firstName, lastName, applicantHeader, statusHeader, status, items) {
var result = '─ ' + applicantHeader + ' : ['+ firstName + ' ' + lastName +']' + os.EOL;
result += '─ ' + statusHeader + ' : ['+ status +']' + os.EOL;
result += '─ ' + 'Status Details:' + os.EOL;
for(var i = 0; i < items.length; i++) {
if(i < items.length - 1) {
result += '├─ ';
} else {
result += '└─ ';
}
result += items[i].replace(/<(\/?)li>/g, '') + os.EOL;
}
return result;
}
function checkApplicationStatus() {
log('Starting to check application status...Next check will be after [' + CHECK_INTERVAL_MIN + '] minutes.')
request(req_options_general, function (error, response, body) {
if (!error && response.statusCode === 200) {
// get headers
var header = body.match(/<thead>[\w\W]*<\/thead>/)[0].match(/<th>(.+)<\/th>/g);
var applicantHeader = header[0].replace(/<(\/?)th>/g, '');
var statusHeader = header[1].replace(/<(\/?)th>/g, '');
// get application info
var info = body.match(/<tbody>[\w\W]*<\/tbody>/)[0].replace(/<\/?\w+>/g, '').match(/[^\t^\n]+/g);
var firstName = info[1];
var lastName = info[2];
var detailLink = info[3].match(/"(.*?)"/)[0].replace(/"/g, '').replace(/&/g, '&');
var status = info[4];
var req_options_detail = {
'url' : 'https://services3.cic.gc.ca/ecas/' + detailLink,
'method' : 'GET',
'jar' : true,
'followAllRedirects' : true
};
request(req_options_detail, function (error2, response2, body2) {
if (!error2 && response2.statusCode == 200) {
var items = body2.match(/<li>(.+)\.<\/li>/g);
var result = getResultStr(firstName, lastName, applicantHeader, statusHeader, status, items);
log(result);
if(numItems === 0 && ALWAYS_ALERT_AFTER_START === 'no') {
// initial item length
numItems = items.length;
log('First time check without alert. There are [' + numItems + '] items.');
}
if(items.length > numItems) {
// add number
numItems = items.length;
log('Status changed to [' + numItems + '] items.');
// send email notification
if(EMAIL_ALERT_ENABLED === 'yes') {
log('Now sending alert email.');
mailOptions.text = result;
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport('SMTP', senderGmailInfo);
smtpTransport.sendMail(mailOptions, function(error, response) {
if(error){
log(error);
}else{
log("Email alert sent: " + response.message);
}
smtpTransport.close();
});
} else {
log('!!!!!!!!!!!!!!!!!! ALERT FROM CONSOLE !!!!!!!!!!!!!!!!!!');
}
}
}
});
}
});
}
function log(str) {
console.log('[' + moment().format('MM-DD HH:mm') + '] ' + str);
}