Created
September 5, 2013 15:45
-
-
Save astro/6451990 to your computer and use it in GitHub Desktop.
Reliably record an HTTP Live Stream with node.js
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 request = require('request'); | |
var joinUrl = require('url').resolve; | |
if (process.argv.length != 3) { | |
console.log("Please pass playlist URL"); | |
process.exit(1); | |
} | |
var plsUrl = process.argv[2]; | |
var INTERVAL = 5 * 1000; | |
var seen = {}; | |
function loop() { | |
request.get({ url: plsUrl, | |
encoding: 'utf8' | |
}, function(err, res, body) { | |
if (!err && res.statusCode === 200) { | |
var lines = body.split("\n"); | |
lines = lines.filter(function(line) { | |
return line && !/^#/.test(line); | |
}); | |
var urls = {}; | |
lines.forEach(function(line) { | |
url = joinUrl(plsUrl, line); | |
urls[url] = true; | |
if (!seen.hasOwnProperty(url)) { | |
download(url); | |
} else { | |
console.log("already seen", line); | |
} | |
}); | |
seen = urls; | |
setTimeout(loop, INTERVAL); | |
} else { | |
console.error(err || res.statusCode); | |
setTimeout(loop, INTERVAL); | |
} | |
}); | |
} | |
loop(); | |
function download(url) { | |
var urlParts = url.split("/"); | |
var fn = Date.now() + "-" + urlParts[urlParts.length - 1]; | |
console.log("download", url); | |
request.get(url). | |
pipe(fs.createWriteStream(fn)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment