-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
286 lines (245 loc) · 8.18 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const download = require('images-downloader').images,
fs = require('fs'),
fse = require('fs-extra');
request = require('request-promise'),
jsonfile = require('jsonfile');
const defaultModTime = "Sat, 1 Sep 2018 01:12:51 GMT";
const lakes = ["ontario", "erie", "huron", "superior", "michigan"];
// const lakes = ["ontario"];
let imageMeta = {},
errors = 0;
try {
imageMeta = jsonfile.readFileSync(`images/imageMeta.json`);
} catch (err) {
console.log("imageMeta.json not found. creating file & initializing empty imagemeta");
fse.ensureFileSync("images/imageMeta.json");
imageMeta = constructFullImageList();
}
function constructImageList(lake) {
let waveDest = `images/${lake}/waves`,
windDest = `images/${lake}/wind`,
waveStem = "/" + lake[0] + "wv",
windStem = "/" + lake[0] + "wn",
baseURL = "http://www.glerl.noaa.gov/res/glcfs/",
cast="",
meta={};
fse.ensureDirSync(waveDest);
fse.ensureDirSync(windDest);
// ugly, finicky name management
for (var i = -48; i < 121; i++){
var offset = i;
if(offset > 9){
offset = "+"+offset;
cast="fcast";
}else if(offset > 0 && offset < 10){
offset = "+0"+offset;
cast="fcast";
}else if(offset < 0 && offset > -10){
offset = "-0"+(i*(-1));
cast="ncast";
}else if(offset == 0){
offset = "-00";
//i=1;
cast="ncast";
} else {
cast = "ncast";
}
// add wach wave and wind to to list
let wave = {name: waveStem + offset,
fullURL: baseURL + cast + waveStem + offset + ".gif",
fullPath: waveDest + waveStem + offset + ".gif"},
wind = {name: windStem + offset,
fullURL: baseURL + cast + windStem + offset + ".gif",
fullPath: windDest + windStem + offset + ".gif"};
meta[wave.name] = {lastModified : defaultModTime,
path: wave.fullPath,
url: wave.fullURL};
meta[wind.name] = {lastModified : defaultModTime,
path: wind.fullPath,
url: wind.fullURL};
}
return meta;
}
// iterate through the lakes and then add all to
// as single object, which can be written to meta
function constructFullImageList() {
let result = {};
for (let l of lakes) {
Object.assign(result, constructImageList(l));
}
writeImageMetaToFile("images/imageMeta.json", result);
return result;
}
// stupid utility function
function writeImageMetaToFile(file, obj) {
jsonfile.writeFile(file, obj, function (err) {
if (err) console.error(err);
});
}
// takes an image name and returns a modified version of its meta
// if successful, otherwise returns an error object
async function getImage(imageName) {
let singleMeta = imageMeta[imageName];
let path = singleMeta.path,
modTime = singleMeta.lastModified,
url = singleMeta.url;
let attempt = 0;
let options = {url: singleMeta.url,
method: "GET",
resolveWithFullResponse: true,
headers:{'If-Modified-Since': modTime,
'fulldownloadpath':path},
encoding: null,
simple: false,
testoptions: "testingnow"
};
setTimeout(console.log, 1000, `waiting for a sec to get ${path}`)
await request(options)
.then(function(response){
if (response.statusCode == 200) {
console.log(`resolved ${url} successfully!`);
console.log(`attempting to write to ${path}`);
let thisStream = fs.createWriteStream(path);
thisStream.write(response.body);
thisStream.end();
thisStream.on('finish', () => {
console.log(`wrote all data to ${path}`);
});
singleMeta.lastModified = response.headers['last-modified'];
imageMeta[imageName] = singleMeta;
} else if (response.statusCode == 304) {
console.log (`${imageName} is unchanged and does not need to be downloaded.`);
} else {
console.log(`doesn't look good. ${imageName} returned ${response.statusCode}`);
errors += 1;
}
})
.catch (function (err) {
console.log(`oops, error on try number ${attempt}! ${err}`);
// console.log(err.message);
if (err.message == "Error: read ECONNRESET") {
console.log(`taking a break before retrying ${imageName}`);
setTimeout(getImage, 2000, imageName);
}
errors += 1;
});
return singleMeta;
}
// this is obsolete; we might use it in the future if this becomes a webapp.
async function getLakeImages(lake) {
let options = {
url: "http://www.glerl.noaa.gov/res/glcfs/fcast/owv+01.gif",
method: "GET",
resolveWithFullResponse: true,
headers:{'If-Modified-Since': imageMeta["/owv+01"]["lastModified"]},
simple: false
};
request(options)
.then(function(results) {
console.log(results.statusCode);
if (results.statusCode == 200 ) {
let allPromises = [];
for (let i in imageMeta) {
if (i[1] == lake[0]) {
allPromises.push (getImage(i));
}
}
Promise.all(allPromises)
.then(function (values) {
console.dir(imageMeta);
console.log(`there were ${errors} errors loading the files.`);
jsonfile.writeFile('images/imageMeta.json', imageMeta, function (err) {
console.log(`unable to write to images/imageMeta.json: ${err}`);
});
});
} else if (results.statusCode == 304 ) {
console.log(`images don't appear to have changed, aborting download`);
} else {
console.log(`huh, something went wrong: ${results.statusCode} ${results.statusMessage}`);
}
})
.catch(function(error) {
console.log(`Error! ${error}`);
});
}
async function allLakes () {
let options = {
url: "http://www.glerl.noaa.gov/res/glcfs/fcast/owv+01.gif",
method: "GET",
resolveWithFullResponse: true,
headers:{'If-Modified-Since': imageMeta["/owv+01"].lastModified},
simple: false
};
request(options)
.then(function(results) {
console.log(results.statusCode);
if (results.statusCode == 200 ) {
let allPromises = [];
for (let i in imageMeta) {
allPromises.push (getImage(i));
}
Promise.all(allPromises)
.then(function (values) {
console.dir(imageMeta);
console.log(`there were ${errors} errors loading the files.`);
jsonfile.writeFile('images/imageMeta.json', imageMeta, function (err) {
console.log(`unable to write to images/imageMeta.json: ${err}`);
});
});
} else if (results.statusCode == 304 ) {
console.log(`images don't appear to have changed, aborting download`);
} else {
console.log(`huh, something went wrong: ${results.statusCode} ${results.statusMessage}`);
}
})
.catch(function(error) {
console.log(`Error! ${error}`);
});
}
/**
*
* @param {} lake and object with properties name, slug, assets?
*/
function buildLakeTab (lake) {
}
/**
*
* @param {} lakes list of lake objects
*/
function buildNav (lakes) {
let navWrapper = {
open: `<ul class="nav nav-tabs" id="myTab" role="tablist">`,
close: `</ul>`
};
let navHTML = navWrapper.open;
for (let l of lakes) {
}
}
/**
*
*
**/
function buildIndexHTML( ) {
// assemble navs
let navsHTML = buildNav(lakes);
// assemble tabs
let tabsWrapper = {
open: `<div class="tab-content" id="myTabContent">`,
close: `</div>`
};
let tabsHTML = tabsWrapper.open;
for (let l of lakes) {
tabsHTML += buildLakeTab(l);
}
tabsHTML += tabsWrapper.close;
}
// for (let lake of lakes) {
// getLakeImages(lake);
// }
// testImage = { lastModified: 'Sat, 1 Sep 2018 01:12:51 GMT',
// path: 'images/michigan/wind/mwn+120.gif',
// url: 'http://www.glerl.noaa.gov/res/glcfs/fcast/mwn+120.gif' };
// testImage = "/mwn+120";
// console.log(getImage(testImage));
// console.log(constructFullImageList());
allLakes();