-
Notifications
You must be signed in to change notification settings - Fork 1
/
final.js
78 lines (66 loc) · 2.65 KB
/
final.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
const axios = require('axios');
var fs = require('fs');
limit = 300
countryCode = 'IN'
var arr = [];
let writeStream = fs.createWriteStream('output.txt');
//URL to GET supported cities in a country
url = `https://api.openaq.org/v1/cities?&limit=${limit}&country=${countryCode}`
axios.get(url).then(response => {
x = response.data.results
x.forEach(element => {
arr.push(element)
});
var itr = 0;
function update() {
//URL to GET data from each station in a city
url = `https://api.openaq.org/v1/latest?city=${arr[itr].city}`
axios
.get(url)
.then(function (response) {
var data = {
Location: "",
CO: "",
SO2: "",
O3: "",
NO2: "",
PM25: "",
PM10: ""
}
for (let index = 0; index < response.data.results.length;) {
const element = response.data.results[index];
if(element.measurements[0].lastUpdated.includes('2017') ||element.measurements[0].lastUpdated.includes('2018') || element.measurements[0].lastUpdated.includes('2016') )
{index++;}
else {
element.measurements.forEach(element => {
if (element.parameter == 'co') { data.CO = element.value }
if (element.parameter == 'so2') { data.SO2 = element.value }
if (element.parameter == 'o3') { data.O3 = element.value }
if (element.parameter == 'no2') { data.NO2 = element.value }
if (element.parameter == 'pm25') { data.PM25 = element.value }
if (element.parameter == 'pm10') { data.PM10 = element.value }
});
data.Location = element.location;
var jsonData = JSON.stringify(data)
writeStream.write(jsonData + ',\n');
index++;
}
}
})
.catch(function (error) {
console.log(error);
})
itr++;
}
var i = 0, howManyTimes = arr.length;
function loop() {
update()
i++;
if (i < howManyTimes) {
setTimeout(loop, 10);
}
}
loop();
}).catch(function (error) {
console.log(error);
});