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

handle downloads of videos from youtube playlist #24

Merged
merged 6 commits into from
Apr 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 59 additions & 17 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ var ytdl = module.exports = function(url, args) {
return;
}

var req = request(data.url);
var item = (!data.length) ? data : data.shift();

var req = request(item.url);
req.on('response', function(res) {
if (res.statusCode !== 200) {
stream.emit('error', new Error('status code ' + res.statusCode));
return;
}

data.size = parseInt(res.headers['content-length'], 10);
stream.emit('info', data);
item.size = parseInt(res.headers['content-length'], 10);
stream.emit('info', item);
});
stream.resolve(req);
});
Expand Down Expand Up @@ -88,8 +90,12 @@ function call(video, args, options, callback) {
// Get possible IDs for youtu.be from urladdr.
id = details.pathname.slice(1);
}

args.push('http://www.youtube.com/watch?v=' + id);

if (id === 'playlist') {
args.push(video);
} else {
args.push('http://www.youtube.com/watch?v=' + id);
}

var opt = [file, args, ''];

Expand All @@ -103,7 +109,30 @@ function call(video, args, options, callback) {
var data = stdout.trim().split(opt[2] + '\n');
callback(null, data);
});


}


/**
* Filters youtube info data and returns reformated object.
*
* @param {Array.<String>} data
*/
function filterData(data) {

var format = data[data.length - 1].split(' - ');

return {
title : data[0],
id : data[1],
url : data[2],
thumbnail : data[3],
description : data.slice(4, data.length - 2).join('\n'),
filename : data[data.length - 2],
itag : parseInt(format[0], 10),
resolution : format[1],
};

}


Expand Down Expand Up @@ -132,19 +161,28 @@ ytdl.getInfo = function(url, options, callback) {
call(url, args, options, function(err, data) {
if (err) return callback(err);

var format = data[data.length - 1].split(' - ');
var info = {
title : data[0],
id : data[1],
url : data[2],
thumbnail : data[3],
description : data.slice(4, data.length - 2).join('\n'),
filename : data[data.length - 2],
itag : parseInt(format[0], 10),
resolution : format[1],
var playlist = [];

var divideData = function divideData(data) {

var arr = [];

for (var i = 0; i < args.length; i++) {
arr.push(data.shift());
}

playlist.push(filterData(arr));

if (data.length) { divideData(data); }

};

callback(null, info);
if (data.length === args.length) { return callback(null, filterData(data)); }

divideData(data);

return callback(null, playlist);

});
};

Expand All @@ -165,10 +203,14 @@ ytdl.getFormats = function(url, options, callback) {
if (err) return callback(err);

var formats = [];
var status = '';

data.map(function(line) {
var result = formatsRegex.exec(line);
if (/\[info\]/.test(line)) { status = line.split(' ')[4].slice(0, -1); }
if (result) {
formats.push({
id : status,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

itag : parseInt(result[1], 10),
filetype : result[2],
resolution : result[3],
Expand Down
14 changes: 7 additions & 7 deletions test/getFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ var video = 'http://www.youtube.com/watch?v=0k2Zzkw_-0I';


var expected = [
{ itag: 171, filetype: 'webm', resolution: 'audio only' },
{ itag: 140, filetype: 'm4a', resolution: 'audio only' },
{ itag: 17, filetype: '3gp', resolution: '176x144' },
{ itag: 36, filetype: '3gp', resolution: '320x240' },
{ itag: 5, filetype: 'flv', resolution: '400x240' },
{ itag: 43, filetype: 'webm', resolution: '640x360' },
{ itag: 18, filetype: 'mp4', resolution: '640x360' }
{ id: '0k2Zzkw_-0I', itag: 171, filetype: 'webm', resolution: 'audio only' },
{ id: '0k2Zzkw_-0I', itag: 140, filetype: 'm4a', resolution: 'audio only' },
{ id: '0k2Zzkw_-0I', itag: 17, filetype: '3gp', resolution: '176x144' },
{ id: '0k2Zzkw_-0I', itag: 36, filetype: '3gp', resolution: '320x240' },
{ id: '0k2Zzkw_-0I', itag: 5, filetype: 'flv', resolution: '400x240' },
{ id: '0k2Zzkw_-0I', itag: 43, filetype: 'webm', resolution: '640x360' },
{ id: '0k2Zzkw_-0I', itag: 18, filetype: 'mp4', resolution: '640x360' }
];

vows.describe('getFormats').addBatch({
Expand Down