-
Notifications
You must be signed in to change notification settings - Fork 6
/
worker.js
91 lines (72 loc) · 2.31 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
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
var database = require("./server/database");
init();
function init() {
console.log("Get Latest AQI Information at " + (new Date()));
try{
// read API key
var fs = require('fs');
var data = fs.readFileSync('./metadata/API-KEY.txt');
var api_key = data.toString();
// get AQI data
var http = require('http');
var url = 'http://pm25.in/api/querys/all_cities.json?token=' + api_key;
//var url = 'http://pm25.in/api/querys.json?token=' + api_key;
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(JSON.stringify(body, null, 3));
//console.log("Got response: " + response);
// write file to json
// fs.writeFile('./metadata/allcities.json', body, function (err) {
// if (err) throw err;
// console.log('It\'s saved!');
// });
// write back to database
var doc = { cities : body};
database.openDb(database.onDbOpen, doc);
}).on('error', function(e){
console.log("Got error: " + e.message);
});
});
}catch(err){
throw err;
}
}
setInterval(function() {
console.log("Get Latest AQI Information at " + (new Date()));
try{
// read API key
var fs = require('fs');
var data = fs.readFileSync('./metadata/API-KEY.txt');
var api_key = data.toString();
// get AQI data
var http = require('http');
var url = 'http://pm25.in/api/querys/all_cities.json?token=' + api_key;
//var url = 'http://pm25.in/api/querys.json?token=' + api_key;
http.get(url, function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var response = JSON.parse(JSON.stringify(body, null, 3));
//console.log("Got response: " + response);
// write file to json
// fs.writeFile('./metadata/allcities.json', body, function (err) {
// if (err) throw err;
// console.log('It\'s saved!');
// });
// write back to database
var doc = { cities : body};
database.openDb(database.onDbOpen, doc);
}).on('error', function(e){
console.log("Got error: " + e.message);
});
});
}catch(err){
throw err;
}
}, 2700000);