-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_prepare.js
59 lines (43 loc) · 1.49 KB
/
file_prepare.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
const indexer = require('./indexer');
const dateToFetch= '20180117';
const ZIP_DIR = `./samples`;
const AdmZip = require('adm-zip'),
http = require('http'),
fs = require('fs');
const url = `http://data.gdeltproject.org/events/${dateToFetch}.export.CSV.zip`,
zipName = `./${ZIP_DIR}/${dateToFetch}.export.CSV.zip`;
fileDownloadedName = `${ZIP_DIR}/${dateToFetch}.export.CSV`;
function unzipFile(){
const zip = new AdmZip(zipName);
console.log("Start Decompressing Zip!");
zip.extractAllTo(ZIP_DIR, true);
fs.unlink(zipName, function(err){
if (err) throw err;
console.log("Done Decompressing Zip!");
});
}
const download = function(url, dest, cb) {
if (!fs.existsSync(ZIP_DIR)){
fs.mkdirSync(ZIP_DIR);
}
var file = fs.createWriteStream(dest);
console.log("START DOWNLOADING "+url);
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
console.log("DONE DOWNLOADING "+url);
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};
download(url,zipName,afterDownload);
function afterDownload(err,res){
unzipFile(); // Blocking
console.log('Waiting...');
setTimeout(()=>{
indexer.indexGdeltFile(fileDownloadedName);
},6000)
}