-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (75 loc) · 2.49 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
const { Chromeless } = require('chromeless');
const fs = require('fs-extra')
const pretty = require('pretty');
const date = require('date-and-time');
const PushBullet = require('pushbullet');
if(process.argv.length<4){
console.error('usage : node index.js "trackNum" "pushBulletId"');
return;
}
const trackNum = process.argv[2];
const pushBulletId = process.argv[3];
async function updatePushBullet(parseH, pHtml){
//console.log(parseH);
updateExist = parseH.needUpdate;
days = parseH.days;
daysStr = "days since last updates "+days;
if(updateExist){
var pusher = new PushBullet(pushBulletId);
await pusher.note(null, "Order "+trackNum, "days:"+days+"\n"+pHtml);
}else{
if(days % 5 == 0){
var pusher = new PushBullet(pushBulletId);
await pusher.note(null, "Order "+trackNum , daysStr);
}
}
}
async function parseHtmlResponse(trNum, htmlResp, fileName){
//console.log('analyzing :'+htmlResp);
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var days = -1;
//console.log('------');
var allDates = htmlResp.match(/\d{2}-\d{2}-\d{2}/gi);
//console.log('extracted date:'+allDates.slice(-1)[0]);
var lastDate = date.parse(allDates.slice(-1)[0], 'DD-MM-YY');
let now = new Date();
var days = Math.round(Math.abs((now.getTime() - lastDate.getTime())/(oneDay)));
//console.log('days:'+days);
var needUpdate = false;
const exists = await fs.pathExists(fileName);
if(exists){
//console.log('file exists '+fileName);
content = await fs.readFile(fileName, 'utf8');
if(content != htmlResp)
needUpdate = true
}
else
needUpdate = true;
return {needUpdate, days};
}
async function getTrackHtml(trackNum){
const chromeless = new Chromeless({})
//console.log('needed : '+trackNum);
const rawHtml = await chromeless
.goto('http://my.exelot.com/public/track')
.type(trackNum, 'input[name="tracking_num"]')
.click('.btn-primary')
.wait('#trackingInfo')
.evaluate(() => {
//console.log('element : '+document.getElementById("trackingInfo").innerHTML);
return document.getElementById("trackingInfo").innerHTML;
});
await chromeless.end()
return pretty(rawHtml);
}
async function run() {
var pHtml = await getTrackHtml(trackNum);
let fileName = "Track"+trackNum+".txt";
let parseH = await parseHtmlResponse(trackNum, pHtml, fileName);
await updatePushBullet(parseH, pHtml);
if(parseH.needUpdate){
console.log('Creating cache file : '+fileName);
await fs.outputFile(fileName, pHtml)
}
}
run().catch(console.error.bind(console))