Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

Commit

Permalink
fix getting playlist info
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Feb 13, 2015
1 parent d2c53ae commit cdb37fe
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
29 changes: 29 additions & 0 deletions example/playlist.js
Original file line number Diff line number Diff line change
@@ -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 + '%');
}
});
26 changes: 10 additions & 16 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,10 @@ function call(video, args1, args2, options, callback) {


/**
* Filters youtube info data and returns reformated object.
*
* @param {Array.<String>} 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
Expand Down Expand Up @@ -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);
});
};

Expand Down
21 changes: 18 additions & 3 deletions test/getInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@ 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);
assert.equal(info.height, 360);
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';
Expand All @@ -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');
}
Expand All @@ -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');
Expand Down

0 comments on commit cdb37fe

Please sign in to comment.