Skip to content

Instantly share code, notes, and snippets.

@cobysy
Created August 5, 2018 22:30
Show Gist options
  • Save cobysy/5f1c074fd4ced58703c7e0b2bb0ec34a to your computer and use it in GitHub Desktop.
Save cobysy/5f1c074fd4ced58703c7e0b2bb0ec34a to your computer and use it in GitHub Desktop.
download JMdict_e.gz with node.js
const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
function wget(source, { onStart, onProgress } = {}) {
return new Promise(function(y, n) {
const output = path.basename(url.parse(source).pathname);
const req = http.request(source,
function(res) {
if (res.statusCode === 200) {
const fileSize = Number.isInteger(res.headers['content-length'] - 0) ?
parseInt(res.headers['content-length']) :
0;
let downloadedSize = 0;
/**
* Create write stream
*/
var writeStream = fs.createWriteStream(output, {
flags: 'w+',
encoding: 'binary'
});
res.pipe(writeStream);
if (onStart) {
onStart(res.headers);
}
res.on('data', function(chunk) {
downloadedSize += chunk.length;
if (onProgress) {
onProgress({
fileSize,
downloadedSize,
percentage: fileSize > 0 ? downloadedSize / fileSize : 0
});
}
});
res.on('error', function(err) {
writeStream.end();
n(err);
});
writeStream.on('finish', function() {
writeStream.end();
req.end('finished');
y({ headers: res.headers, fileSize });
});
} else {
n('Server responded with unhandled status: ' + res.statusCode);
}
}
);
req.end('done');
req.on('error', err => n(err));
});
}
wget('http://ftp.monash.edu.au/pub/nihongo/JMdict_e.gz', {
onStart: headers => {
console.log(headers['content-type']);
},
onProgress: progress => {
console.log('downloaded', progress.percentage * 100, '%');
}
})
.then(result => {
//done();
})
.catch(err => {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment