Created
April 10, 2017 13:25
-
-
Save unknownuser88/981f9a3e0e39c037aabb8790e60c49dd to your computer and use it in GitHub Desktop.
node wget downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var url = require('url'); | |
var exec = require('child_process').exec; | |
var spawn = require('child_process').spawn; | |
// Function to download file using wget | |
var download_file_wget = function(file_url, download_dir, file_name_to_save, onProgress, cb) { | |
// extract the file name | |
var file_name = url.parse(file_url).pathname.split('/').pop(); | |
// excute wget using child_process' spawn function | |
var child = spawn('wget', ['-c', '--timeout=300', '--retry-connrefused', '--tries=100', '--waitretry=10', '-O', download_dir + file_name_to_save, file_url]); | |
child.stderr.on('data', function(e) { | |
var stdout = e.toString(); | |
var p = stdout.match(/([0-9]+?\%)+/g); | |
if (p && p.length > 0) { | |
var percent = parseInt(p[0].replace('%', '')); | |
onProgress && onProgress(percent); | |
} | |
}); | |
// add an 'end' event listener to close the writeable stream | |
child.stdout.on('end', function(data) { | |
console.log('endddd'); | |
child.kill(); | |
}); | |
// when the spawn child process exits, check if there were any errors and close the writeable stream | |
child.on('exit', function(code) { | |
if (code != 0) { | |
console.log('Failed: ' + code); | |
cb && cb(new Error(code)); | |
}else{ | |
cb && cb(null, download_dir + file_name_to_save ); | |
} | |
}); | |
}; | |
module.exports = download_file_wget; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment