diff --git a/README.md b/README.md index 29d9372..69cc7d1 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,13 @@ dl.on('error', function(err) { // called when youtube-dl finishes dl.on('end', function(data) { console.log('\nDownload finished!'); - console.log('Filename: ' + data.filename); - console.log('Size: ' + data.size); - console.log('Time Taken: ' + data.timeTaken); - console.log('Time Taken in ms: ' + data.timeTakenms); - console.log('Average Speed: ' + data.averageSpeed); - console.log('Average Speed in Bytes: ' + data.averageSpeedBytes); + console.log('ID:', data.id); + console.log('Filename:', data.filename); + console.log('Size:', data.size); + console.log('Time Taken:', data.timeTaken); + console.log('Time Taken in ms:', + data.timeTakenms); + console.log('Average Speed:', data.averageSpeed); + console.log('Average Speed in Bytes:', data.averageSpeedBytes); }); ``` @@ -48,16 +49,19 @@ dl.on('end', function(data) { This example can be found in the *example* folder, and will produce an output that looks like the following when ran. Download started - Video size: 918.31k - 00:00 100.0% at 206.12k/s - Download finished! - Filename: 90AiXO1pAiA.mp4 - Size: 918.31k - Time Taken: 7 seconds, 27 ms - Time Taken in ms: 7027 - Average Speed: 333.74KB/s - Average Speed in Bytes: 341750.78 + filename: lol-90AiXO1pAiA.mp4 + size: 918.57KiB + + 00:00 100.0% at 2.00MiB/s + Download finished! + ID: 90AiXO1pAiA + Filename: lol-90AiXO1pAiA.mp4 + Size: 918.57KiB + Time Taken: 2 seconds, 178 ms + Time Taken in ms: 2178 + Average Speed: 211.24B/s + Average Speed in Bytes: 211.24 ## Getting video information @@ -85,11 +89,14 @@ youtubedl.info('http://www.youtube.com/watch?v=WKsjaOqDXgg', Running that will produce something like + id: WKsjaOqDXgg title: Ace Rimmer to the Rescue - url: http://v2.lscache2.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0hPSFFQVF9FSkNOOV9JSlhJ&fexp=904410%2C907048%2C910100&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&signature=4093330AC1A5B0CAF8709A0416A4B593A75BB892.21F2F12C418003492D9877E1570DC7AEE6DBEEBA&expire=1303156800&key=yt1&ip=0.0.0.0&factor=1.25&id=58ab2368ea835e08 - thumbnail: http://i4.ytimg.com/vi/WKsjaOqDXgg/default.jpg + url: http://r5---sn-p5qlsn7e.c.youtube.com/videoplayback?ms=au&ip=160.79.125.18&cp=U0hWTFVQVl9FTENONl9NSlpDOjgtU1VsODlkVmRH&id=58ab2368ea835e08&source=youtube&expire=1377558202&factor=1.25&key=yt1&ipbits=8&mt=1377534150&itag=34&sver=3&upn=-rGWz2vYpN4&fexp=912306%2C927900%2C919395%2C926518%2C936203%2C913819%2C929117%2C929121%2C929906%2C929907%2C929922%2C929127%2C929129%2C929131%2C929930%2C925726%2C925720%2C925722%2C925718%2C929917%2C906945%2C929919%2C929933%2C912521%2C932306%2C913428%2C904830%2C919373%2C930803%2C908536%2C904122%2C938701%2C936308%2C909549%2C900816%2C912711%2C904494%2C904497%2C900375%2C906001&sparams=algorithm%2Cburst%2Ccp%2Cfactor%2Cid%2Cip%2Cipbits%2Citag%2Csource%2Cupn%2Cexpire&mv=m&burst=40&algorithm=throttle-factor&signature=ABD3A847684AD9B39331E567568D3FA0DCFA4776.7895521E130A042FB3625A17242CE3C02A4460B7&ratebypass=yes + thumbnail: https://i1.ytimg.com/vi/WKsjaOqDXgg/hqdefault.jpg description: An old Red Dwarf eposide where Ace Rimmer saves the Princess Bonjella. - filename: WKsjaOqDXgg.webm + filename: Ace Rimmer to the Rescue-WKsjaOqDXgg.flv + itag: 34 + resolution: 360x640 For more usage info on youtube-dl and the arguments you can pass to it, do `youtube-dl -h` or go to the [youtube-dl documentation][]. diff --git a/example/download.js b/example/download.js index 2604aa0..585c771 100644 --- a/example/download.js +++ b/example/download.js @@ -28,10 +28,11 @@ dl.on('error', function(err) { // Called when youtube-dl finishes. dl.on('end', function(data) { console.log('\n\nDownload finished!'); - console.log('Filename: ' + data.filename); - console.log('Size: ' + data.size); - console.log('Time Taken: ' + data.timeTaken); - console.log('Time Taken in ms: ' + data.timeTakenms); - console.log('Average Speed: ' + data.averageSpeed); - console.log('Average Speed in Bytes: ' + data.averageSpeedBytes); + console.log('ID:', data.id); + console.log('Filename:', data.filename); + console.log('Size:', data.size); + console.log('Time Taken:', data.timeTaken); + console.log('Time Taken in ms:', + data.timeTakenms); + console.log('Average Speed:', data.averageSpeed); + console.log('Average Speed in Bytes:', data.averageSpeedBytes); }); diff --git a/example/npm-debug.log b/example/npm-debug.log new file mode 100644 index 0000000..4f3a772 --- /dev/null +++ b/example/npm-debug.log @@ -0,0 +1,18 @@ +0 info it worked if it ends with ok +1 verbose cli [ 'node', '/usr/local/bin/npm', 'test' ] +2 info using npm@1.3.8 +3 info using node@v0.10.17 +4 error Error: ENOENT, open '/Users/roly/code/node/modules/published/youtube-dl/example/package.json' +5 error If you need help, you may report this log at: +5 error +5 error or email it to: +5 error +6 error System Darwin 11.4.2 +7 error command "node" "/usr/local/bin/npm" "test" +8 error cwd /Users/roly/code/node/modules/published/youtube-dl/example +9 error node -v v0.10.17 +10 error npm -v 1.3.8 +11 error path /Users/roly/code/node/modules/published/youtube-dl/example/package.json +12 error code ENOENT +13 error errno 34 +14 verbose exit [ 34, true ] diff --git a/lib/youtube-dl.js b/lib/youtube-dl.js index 5e8dba1..1d99426 100644 --- a/lib/youtube-dl.js +++ b/lib/youtube-dl.js @@ -3,6 +3,7 @@ var spawn = require('child_process').spawn , EventEmitter = require('events').EventEmitter , fs = require('fs') , path = require('path') + , url = require('url') , split = require('event-stream').split ; @@ -119,7 +120,7 @@ var getHumanTime = function(ms) { var regex = /(\d+\.\d)% of (\d+\.\d+\w+) at\s+([^\s]+) ETA ((\d|-)+:(\d|-)+)/; // Main download function. -exports.download = function(url, dest, args) { +exports.download = function(urladdr, dest, args) { // Setup settings. dest = dest || process.cwd(); if (args == null) { @@ -127,7 +128,14 @@ exports.download = function(url, dest, args) { } else { args = parseOpts(args); } - args.push(url); + args.push(urladdr); + + // Get ID from urladdr. + var query = url.parse(urladdr, true).query; + if (!query.v) { + throw new Error('Video URL must contain a video ID.'); + } + var id = query.v; // Call youtube-dl. var youtubedl = spawn(file, args, { cwd: dest }); @@ -193,7 +201,8 @@ exports.download = function(url, dest, args) { var timeTaken = Date.now() - start; emitter.emit('end', { - filename : filename + id : id + , filename : filename , size : size , timeTakenms : timeTaken , timeTaken : getHumanTime(timeTaken) diff --git a/test/download.js b/test/download.js index d10af75..6f405c7 100644 --- a/test/download.js +++ b/test/download.js @@ -30,25 +30,17 @@ vows.describe('download').addBatch({ if (err) throw err; assert.isObject(progress); - assert.include(progress, 'percent'); assert.isString(progress.percent); - assert.include(progress, 'speed'); assert.isString(progress.speed); - assert.include(progress, 'eta'); assert.isString(progress.eta); assert.isObject(data); - assert.include(data, 'filename'); + assert.equal(data.id, '90AiXO1pAiA'); assert.equal(data.filename, 'lol-90AiXO1pAiA.flv'); - assert.include(data, 'size'); assert.equal(data.size, '1.26MiB'); - assert.include(data, 'timeTakenms'); assert.isNumber(data.timeTakenms); - assert.include(data, 'timeTaken'); assert.isString(data.timeTaken); - assert.include(data, 'averageSpeedBytes'); assert.isNumber(data.averageSpeedBytes); - assert.include(data, 'averageSpeed'); assert.isString(data.averageSpeed); }, diff --git a/test/info.js b/test/info.js index 816fc7b..51abb73 100644 --- a/test/info.js +++ b/test/info.js @@ -2,6 +2,7 @@ var vows = require('vows') , ytdl = require('..') , assert = require('assert') , video = 'http://www.youtube.com/watch?v=90AiXO1pAiA' + ; vows.describe('info').addBatch({ @@ -13,16 +14,14 @@ vows.describe('info').addBatch({ 'info returned': function(err, info) { assert.isNull(err); assert.isObject(info); - assert.include(info, 'title'); + assert.isString(info.id); assert.isString(info.title); - assert.include(info, 'url'); assert.isString(info.url); - assert.include(info, 'thumbnail'); assert.isString(info.thumbnail); - assert.include(info, 'description'); assert.isString(info.description); - assert.include(info, 'filename'); assert.isString(info.filename); + assert.isString(info.itag); + assert.isString(info.resolution); } } }).export(module);