Skip to content

Instantly share code, notes, and snippets.

@tmathews
Last active August 29, 2015 14:01
Show Gist options
  • Save tmathews/a90206e3063e767a0ca8 to your computer and use it in GitHub Desktop.
Save tmathews/a90206e3063e767a0ca8 to your computer and use it in GitHub Desktop.
Download all mp3 links on a html page.
var http = require("http");
var fs = require("fs");
var url = "http://f.starfox-online.net/sf64/voiceclips/";
var request = http.get(url, function ( response ) {
var body = "";
response.on("data", function ( chunk ) {
body += chunk;
});
response.on("end", function () {
parse(body);
});
});
function parse ( body ) {
var urls = [];
var matches = body.match(/href=".*\.mp3"/gm);
for ( var i in matches ) {
urls.push(matches[i].replace(/href="(.*\.mp3)"/, function ( string, a ) { return a; }));
}
downloadUrls(urls);
}
function downloadUrls ( urls ) {
if ( urls.length ) {
var link = urls.shift();
var file = fs.createWriteStream(link);
var request = http.get(url + link, function ( response ) {
response.pipe(file);
response.once("end", function () {
downloadUrls(urls);
});
});
console.log("Downloading:", link);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment