-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (55 loc) · 1.63 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
var request = require("request"),
schedule = require("node-schedule"),
config = require("./config.json"),
mailgun = require("mailgun-js")({apiKey: config.mailgunKey, domain: config.mailgunDomain}),
fs = require("fs");
var j = schedule.scheduleJob('0 0 0,12 * *', function(){
// send request
var caseNumber = config.caseNumber;
var url = "https://icert.doleta.gov/index.cfm?event=ehRegister.caseStatusSearchGridData&caseNumberList="
+ caseNumber + "&page=1&rows=10&sidx=case_number&sord=asc&nd=1428902731768&_search=false";
request({
"url": url,
"json": true
}, function(err, res, body) {
if (err) {
console.log(err);
}
else {
// read old result
var oldResult = JSON.parse(fs.readFileSync("result.json", "utf8"));
var oldRows = oldResult.rows ? oldResult.rows : [];
var modified = false;
if (body.rows) {
if (body.rows.length !== oldRows.length) {
modified = true;
} else {
for (var i = oldRows.length - 1; i >= 0; i--) {
if (oldRows[i].toString() !== body.rows[i].toString()) {
modified = true;
break;
}
};
}
}
if (modified) {
console.log("visa status updated! " + new Date().toISOString());
// update old result
fs.writeFileSync("result.json", JSON.stringify(body, null, 4), "utf8");
// send email
mailgun.messages().send({
from: "Visa Status Checker <" + config.senderEmail + ">",
to: config.receiverEmail,
subject: config.emailSubject,
text: JSON.stringify(body.rows, null, 4)
}, function (err, body) {
if (err) {
console.log(err);
} else {
console.log(body);
}
});
}
}
});
});