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

Commit

Permalink
add id to download info
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Aug 26, 2013
1 parent c5fc896 commit eb92288
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 41 deletions.
43 changes: 25 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,33 @@ 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);
});
```


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

Expand Down Expand Up @@ -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][].
Expand Down
13 changes: 7 additions & 6 deletions example/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
18 changes: 18 additions & 0 deletions example/npm-debug.log
Original file line number Diff line number Diff line change
@@ -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 [email protected]
3 info using [email protected]
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 <http://github.com/isaacs/npm/issues>
5 error or email it to:
5 error <[email protected]>
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 ]
15 changes: 12 additions & 3 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand Down Expand Up @@ -119,15 +120,22 @@ 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) {
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 });
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 1 addition & 9 deletions test/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},

Expand Down
9 changes: 4 additions & 5 deletions test/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);

0 comments on commit eb92288

Please sign in to comment.