-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_fs.js
83 lines (66 loc) · 1.91 KB
/
data_fs.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
var https = require("https");
var fs = require("fs");
var Key = require("./key");
var veid = Key.veid;
var apikey = Key.apikey;
var callAction = "getLiveServiceInfo";
// 定义文件上次修改时间
var lastQueryTime;
var options = {
host: "api.64clouds.com",
port: 443,
path: `/v1/${callAction}?veid=${veid}&api_key=${apikey}`
};
var callback = function(res) {
var dataString = "";
res.setEncoding('utf8');
res.on('data', (chunk) => {
dataString += chunk;
});
res.on("end", function() {
wirteFs(dataString);
console.log(dataString.length);
});
};
function wirteFs(data) {
// 清除 email 数据
data = JSON.parse(data);
data["email"] = null;
data = JSON.stringify(data);
// 获取当前时间并赋值
var d = new Date();
var queryStatusTime = `${d.getMonth()+1}月${d.getDate()}日${d.getHours()}时${d.getMinutes()}分`;
var param = `var _statusData = ${data};\n var _queryStatusTime = '${queryStatusTime}';`;
fs.stat('status.js', function (err, stat) {
if (err) {
console.log(err);
} else {
// 获取 status.js 文件上次修改时间
lastQueryTime = stat.mtime.getTime();
}
});
// 限制查询频率,间隔大于 60 秒
if(d.getTime() - lastQueryTime < 60000) {
console.log("限制查询频率");
console.log('modified time: ' + lastQueryTime);
return;
}
fs.writeFile('status.js', param, function (err) {
if (err) {
console.log(err);
} else {
console.log('ok.');
}
});
};
function getStatus(options) {
var req = https.request(options, callback);
req.on('error', (e) => {
console.log(`请求遇到问题: ${e.message}`);
});
req.end();
};
module.exports = {
"options": options,
"getStatus": getStatus
}