Last active
August 29, 2015 14:01
-
-
Save tmathews/a90206e3063e767a0ca8 to your computer and use it in GitHub Desktop.
Download all mp3 links on a html page.
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 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