Created
December 25, 2014 11:35
-
-
Save zjor/7260c2fb4519d8d8c4c3 to your computer and use it in GitHub Desktop.
Downloads mp3 files from URLs listed in files.txt
This file contains 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 http = require('http'); | |
var urls = fs.readFileSync('files.txt', {encoding: 'utf8'}).split('\n'); | |
next(); | |
function next() { | |
if (urls.length > 0) { | |
download(urls.pop(), next); | |
} else { | |
console.log('Done'); | |
} | |
} | |
function download(url, callback) { | |
http.get(url, function(res) { | |
console.log('Requesting: ' + url); | |
if (302 == res.statusCode) { | |
var redirectUrl = res.headers.location; | |
console.log('Redirecting to ' + redirectUrl); | |
download(redirectUrl, callback); | |
} else if (200 == res.statusCode) { | |
var filename = getFilename(url); | |
if (!filename) { | |
console.log('Unable to parse filename. Skipped.'); | |
return; | |
} | |
var out = fs.createWriteStream(filename); | |
res.pipe(out); | |
var length = Number(res.headers['content-length']); | |
var received = 0; | |
res.on('data', function(chunk) { | |
received += chunk.length; | |
printProgress((received * 100 / length).toFixed(2)); | |
}); | |
out.on('finish', function() { | |
console.log('\nSaved to: ' + filename); | |
if (callback) { | |
callback(); | |
} | |
}); | |
} | |
}).on('error', function(e) { | |
console.log('Failed to download ' + url + ' :' + e); | |
if (callback) { | |
callback(); | |
} | |
}); | |
} | |
function getFilename(url) { | |
var re = /\/([A-Za-z0-9_\-\.]*.mp3)/; | |
if (re.test(url)) { | |
return re.exec(url)[1]; | |
} | |
} | |
function printProgress(progress) { | |
var width = process.stdout.columns - 30; | |
var done = progress * width / 100; | |
process.stdout.write('[ ' + progress + '% ]['); | |
for (var i = 0; i < done; i++) { | |
process.stdout.write('='); | |
} | |
for (var i = 0; i < width - done; i++) { | |
process.stdout.write('.'); | |
} | |
process.stdout.write(']\r'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment