-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathloadCache.js
More file actions
53 lines (45 loc) · 1.2 KB
/
loadCache.js
File metadata and controls
53 lines (45 loc) · 1.2 KB
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
require("babel/register");
var fs = require('fs'),
path = require('path'),
api = require("./src/api.js");
require('isomorphic-fetch');
var lst = [];
function pages() {
['state','county','basin'].forEach(function (geom) {
api.seasons.forEach(function (_,season) {
api.elems.forEach(function (def,element) {
if (def.gYr) lst.push({geom:geom,season:season,element:element});
})
})
})
return lst.values();
}
var iter = pages();
function getter() {
var n = iter.next();
if (n.done) return;
const p = n.value;
const reqParams = api.buildQuery(p, {});
console.log('start',p.geom, p.element, p.season);
fetch('http://data.nrcc.rcc-acis.org/GridData',{
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(reqParams)
})
.then(function(res) {
if (res.status >= 400) {
throw new Error("Bad response from server");
}
return res.text();
})
.then(function(res) {
var fName = path.join('cache',p.geom,[p.element,p.season].join('_'));
fs.writeFile(fName,res+'\n',getter);
console.log('finish',p.geom, p.element, p.season);
// getter();
});
};
getter();