-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
66 lines (59 loc) · 2.64 KB
/
build.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
const fetch = require('node-fetch');
const fs = require('fs');
async function initBuild() {
let timestampBegin = process.hrtime();
let timestampEnd;
const baseUrl = 'https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL3.003/2000.02.11/';
let files = [];
try {
let res = await fetch(baseUrl);
if(res.status === 200) {
const regex = /href=\"(.*?)\"/gi;
let html = await res.text();
let matches = html.match(regex);
// Retrieve files from each page
for(let i = 0; i < matches.length; i++) {
let match = matches[i];
let ini = 'href=\"'.length;
let end = match.indexOf('\"', ini);
if(end === -1) continue;
let pageUrl = match.substring(ini, end);
if(pageUrl.startsWith(baseUrl)) {
process.stdout.write(`\nGet links from ${pageUrl} ... `);
let pageRes = await fetch(pageUrl);
if(pageRes.status === 200) {
process.stdout.write('OK');
process.stdout.write('\nParsing HTML ... ');
let pageHtml = await pageRes.text();
let pageMatches = pageHtml.match(regex);
let fileCount = 0;
for(let j = 0; j < pageMatches.length; j++) {
let pageMatch = pageMatches[j];
let ini = 'href=\"'.length;
let end = pageMatch.indexOf('.SRTMGL3.hgt.zip\"', ini);
if(end === -1) continue;
let fileUrl = pageMatch.substring(ini, end);
const file = fileUrl.replace('http://', 'https://').replace(baseUrl, '');
files.push(file);
fileCount++;
}
process.stdout.write(`${fileCount} files found`);
}
}
}
}
} catch(err) {
process.stdout.write(' FATAL ERROR');
console.error(err);
return;
}
// Write file
let result = 'module.exports = ' + JSON.stringify(files, null, 4) + ';';
let filename = 'srtm-db.js';
process.stdout.write('\nGenerating file ./src/'+filename+' ... ');
fs.writeFileSync('./src/'+filename, result);
process.stdout.write('OK');
timestampEnd = process.hrtime();
process.stdout.write('\nSRTM DB generated in ' + (timestampEnd[0] - timestampBegin[0]) + 's ' + (Math.abs(timestampEnd[1] - timestampBegin[1]) / 1000000) + 'ms\n');
}
initBuild();