diff --git a/example/playlist.js b/example/playlist.js new file mode 100644 index 0000000..42b34dd --- /dev/null +++ b/example/playlist.js @@ -0,0 +1,29 @@ +var path = require('path'); +var fs = require('fs'); +var ytdl = require('..'); + +/* jshint maxlen:false */ +var video = ytdl('https://www.youtube.com/playlist?list=PLEFA9E9D96CB7F807'); + +video.on('error', function(err) { + console.log('error 2:', err); +}); + +var size = 0; +video.on('info', function(info) { + size = info.size; + var output = path.join(__dirname + '/mp4s', size + '.mp4'); + video.pipe(fs.createWriteStream(output)); +}); + +var pos = 0; +video.on('data', function(data) { + pos += data.length; + // `size` should not be 0 here. + if (size) { + var percent = (pos / size * 100).toFixed(2); + process.stdout.cursorTo(0); + process.stdout.clearLine(1); + process.stdout.write(percent + '%'); + } +}); diff --git a/lib/youtube-dl.js b/lib/youtube-dl.js index e48b149..2721ff9 100644 --- a/lib/youtube-dl.js +++ b/lib/youtube-dl.js @@ -158,11 +158,10 @@ function call(video, args1, args2, options, callback) { /** - * Filters youtube info data and returns reformated object. - * - * @param {Array.} data + * @param {Object} data + * @returns {Object} */ -function filterData(data) { +function parseInfo(data) { var info = JSON.parse(data); // Add and process some entries to keep backwards compatibility @@ -209,19 +208,14 @@ ytdl.getInfo = function(url, args, options, callback) { call(url, defaultArgs, args, options, function(err, data) { if (err) return callback(err); - var playlist = []; - var track = []; - - data.forEach(function(row) { - playlist.push(filterData(row)); - track = []; - }); - - if (playlist.length === 1) { - return callback(null, playlist[0]); - } else { - return callback(null, playlist); + var info; + try { + info = data.map(parseInfo); + } catch (err) { + return callback(err); } + + callback(null, info.length === 1 ? info[0] : info); }); }; diff --git a/test/getInfo.js b/test/getInfo.js index 659b81c..c507122 100644 --- a/test/getInfo.js +++ b/test/getInfo.js @@ -23,7 +23,7 @@ vows.describe('getInfo').addBatch({ 'Filmed in 2003 before Youtube was invented. ' + 'This is also the original I find it hilarious that there ' + 'are copycat videos!'); - assert.equal(info.filename, 'lol-90AiXO1pAiA.mp4'); + assert.equal(info._filename, 'lol-90AiXO1pAiA.mp4'); assert.equal(info.format, '18 - 640x360'); assert.equal(info.duration, '12'); assert.equal(info.width, 640); @@ -31,6 +31,21 @@ vows.describe('getInfo').addBatch({ assert.isArray(info.formats); } }, + 'from a youtube playlist': { + 'topic': function() { + var pl = 'https://www.youtube.com/playlist?list=PLEFA9E9D96CB7F807'; + ytdl.getInfo(pl, this.callback); + }, + + 'info returned': function(err, info) { + assert.isNull(err); + assert.isArray(info); + assert.ok(info.length); + info.forEach(function(videoInfo) { + assert.isString(videoInfo.url); + }); + } + }, 'from a soundcloud track': { 'topic': function() { var video = 'https://soundcloud.com/erasedtapes/kiasmos-bent'; @@ -44,7 +59,7 @@ vows.describe('getInfo').addBatch({ assert.isString(info.url); assert.isString(info.thumbnail); assert.isString(info.description); - assert.equal(info.filename, 'Kiasmos - Bent-147055755.mp3'); + assert.equal(info._filename, 'Kiasmos - Bent-147055755.mp3'); assert.equal(info.format, 'http_mp3_128_url - audio only'); assert.equal(info.duration, '5:45'); } @@ -66,7 +81,7 @@ vows.describe('getInfo').addBatch({ 'Video for the song "Good Friends, Bad Habits" from the album ' + 'New Leaves. Directed by Joe Wigdahl. Purchase the album here: ' + 'hobbledehoyrecords.com/store'); - assert.equal(info.filename, + assert.equal(info._filename, 'OWEN - good friends, bad habits-6586873.mp4'); assert.equal(info.format, 'h264-sd - 480x272'); assert.equal(info.duration, '3:55');