forked from stevage/opentrees-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-gettrees.js
executable file
·65 lines (59 loc) · 2.29 KB
/
1-gettrees.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
#!/usr/bin/env node --max-old-space-size=8192
/*
Step 1:
- download file, call it something sensible on disk
- if it's a zip file, unzip it into a sensible directory name
*/
/* jshint esnext:true */
const fs = require('fs');
const colors = require('colors');
const download = require('download');
const extract = require('extract-zip');
const sources = require('./sources');
const child_process = require('child_process');
const { match, formatForSource, extensionForSource } = require('./utils');
['./data', './data/unzip', './tmp'].forEach(dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
});
let skipCount = 0;
sources.forEach(function(source) {
var urls = source.download;
if (!Array.isArray(urls)) {
urls = [urls];
}
urls.forEach(async url => {
const extension = extensionForSource(source);
var filename = 'data/' + source.id + '.' + extension;
if (!fs.existsSync(filename)) {
console.log('Downloading '.cyan + /*source.download + ' to ' + */ filename);
try {
const data = await download(url)
// .pipe(fs.createWriteStream(filename));
fs.writeFileSync(filename, data);
console.log('Downloaded '.green + filename);
if (extension === 'zip') {
console.log('Unzipping ' + filename);
try {
child_process.execSync(
`unzip -d ${process.cwd()}/data/unzip/${source.id} "${filename}"`,
{ stdio: 'inherit' }
);
console.log('Unzipped '.green + filename);
} catch (err) {
console.error('Error unzipping '.error + filename + ': ' + err);
}
}
} catch (err) {
console.error((filename + ': ').red + err);
child_process.execSync(`mv "data/${filename}" "data/${filename}.bad"`);
// remove the partially downloaded file?
};
} else {
skipCount++;
// console.log('(Skip ' + filename + ')');
}
});
});
console.log(`Skipped ${skipCount} sources that already existed on disk.`);