-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
46 lines (40 loc) · 1.12 KB
/
worker.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
var CronJob = require('cron').CronJob;
var request = require('request');
var _ = require('lodash');
var fs = require('fs');
var updateTime;
var isExist = function(str) {
try{
var fd = fs.openSync('./data/'+str, 'r');
fs.closeSync(fd);
return true;
}catch(e) {
return false;
}
};
var job = new CronJob('0 '+ _.range(0, 60, 1).join(',') +' * * * *', function() {
request.get({
url: 'http://www.gzjt.gov.cn/gztraffic/GetData.ashx',
}, function(err, res, body) {
var time = body.match(/Date\(([0-9]*)\)/)[1];
if (!isExist(time)) {
updateTime = time;
fs.writeFile('./data/'+time, body, function(err) {
request.post({
url: 'https://hook.bearychat.com/=bw5Mw/incoming/2f88bb3f1a8c1b885c079856d66eb382',
json: true,
body: {text:(err?('write file '+time+' failed'):(time+' saved'))}
});
})
}
});
/*
* Runs every weekday (Monday through Friday)
* at 11:30:00 AM. It does not run on Saturday
* or Sunday.
*/
}, function () {
/* This function is executed when the job stops */
},
true /* Start the job right now */
);