ytdl-core
Advanced tools
Comparing version 0.6.0 to 0.6.1
@@ -5,2 +5,3 @@ var PassThrough = require('stream').PassThrough; | ||
var util = require('./util'); | ||
var cache = require('./cache'); | ||
@@ -10,2 +11,3 @@ module.exports = ytdl; | ||
ytdl.downloadFromInfo = downloadFromInfo; | ||
ytdl.cache = cache; | ||
@@ -12,0 +14,0 @@ /** |
@@ -5,2 +5,3 @@ var fs = require('fs'); | ||
var request = require('./request'); | ||
var cache = require('./cache'); | ||
@@ -18,35 +19,42 @@ | ||
if (info.formats[0] && info.formats[0].s) { | ||
html5playerfile = 'http:' + html5playerfile; | ||
request(html5playerfile, function(err, body) { | ||
if (err) return callback(err); | ||
var key, cachedTokens; | ||
var rs = /html5player-([a-zA-Z0-9\-_]+)(?:\.js|\/)/.exec(html5playerfile); | ||
if (rs) { | ||
key = rs[1]; | ||
cachedTokens = cache.get(key); | ||
} else { | ||
console.warn('could not extract html5player key:', html5playerfile); | ||
} | ||
if (cachedTokens) { | ||
gotTokens(cachedTokens, info, debug, callback); | ||
} else { | ||
html5playerfile = 'http:' + html5playerfile; | ||
request(html5playerfile, function(err, body) { | ||
if (err) return callback(err); | ||
var tokens = exports.extractActions(body); | ||
if (!tokens || !tokens.length) { | ||
if (debug) { | ||
var splits = html5playerfile.split('/'); | ||
var name = splits[splits.length - 2].slice(12); | ||
var filename = name + '.js'; | ||
var filepath = path.resolve( | ||
__dirname, '../test/files/html5player/' + filename); | ||
fs.writeFile(filepath, body); | ||
var html5player = require('../test/html5player.json'); | ||
if (!html5player[name]) { | ||
html5player[name] = []; | ||
fs.writeFile( | ||
path.resolve(__dirname, '../test/html5player.json'), | ||
JSON.stringify(html5player, null, 2)); | ||
var tokens = exports.extractActions(body); | ||
if (!tokens || !tokens.length) { | ||
if (debug) { | ||
var filename = key + '.js'; | ||
var filepath = path.resolve( | ||
__dirname, '../test/files/html5player/' + filename); | ||
fs.writeFile(filepath, body); | ||
var html5player = require('../test/html5player.json'); | ||
if (!html5player[name]) { | ||
html5player[key] = []; | ||
fs.writeFile( | ||
path.resolve(__dirname, '../test/html5player.json'), | ||
JSON.stringify(html5player, null, 2)); | ||
} | ||
} | ||
callback( | ||
new Error('Could not extract signature deciphering actions')); | ||
return; | ||
} | ||
callback(new Error('could not extract signature deciphering actions')); | ||
return; | ||
} | ||
info.formats.forEach(function(format) { | ||
var sig = exports.decipher(tokens, format.s); | ||
format.url = exports.getDownloadURL(format, sig, debug); | ||
cache.set(key, tokens); | ||
gotTokens(tokens, info, debug, callback); | ||
}); | ||
} | ||
callback(null, info); | ||
}); | ||
} else { | ||
@@ -61,3 +69,21 @@ info.formats.forEach(function(format) { | ||
/** | ||
* Called when tokens for html5playerfile have been retrieved. | ||
* Either from a request or the cache. | ||
* | ||
* @param {Array.String} tokens | ||
* @param {Object} info | ||
* @param {Boolean} debug | ||
* @param {Function(Error, String)} callback | ||
*/ | ||
function gotTokens(tokens, info, debug, callback) { | ||
info.formats.forEach(function(format) { | ||
var sig = exports.decipher(tokens, format.s); | ||
format.url = exports.getDownloadURL(format, sig, debug); | ||
}); | ||
callback(null, info); | ||
} | ||
/** | ||
@@ -64,0 +90,0 @@ * @param {Object} format |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"repository": { | ||
@@ -18,3 +18,3 @@ "type": "git", | ||
"scripts": { | ||
"test": "mocha -R spec -t 16000 test/*-test.js" | ||
"test": "mocha -t 16000 test/*-test.js" | ||
}, | ||
@@ -21,0 +21,0 @@ "dependencies": { |
@@ -1,5 +0,7 @@ | ||
# node-ytdl-core [![Build Status](https://secure.travis-ci.org/fent/node-ytdl-core.png)](http://travis-ci.org/fent/node-ytdl-core) | ||
# node-ytdl-core | ||
Yet another youtube downloading module. This time written with only Javascript and a more node-friendly streaming interface. | ||
[![Build Status](https://secure.travis-ci.org/fent/node-ytdl-core.png)](http://travis-ci.org/fent/node-ytdl-core) [![Dependency Status](https://gemnasium.com/fent/node-ytdl-core.svg)](https://gemnasium.com/fent/node-ytdl-core) | ||
For a CLI version of this, check out [ytdl](https://github.com/fent/node-ytdl) and [pully](https://github.com/JimmyBoh/pully). | ||
@@ -6,0 +8,0 @@ |
@@ -78,2 +78,16 @@ var assert = require('assert-diff'); | ||
}); | ||
describe('hit the same video twice', function() { | ||
it('Gets html5player tokens from cache', function(done) { | ||
var scope1 = nock(YT_HOST) | ||
.get(VIDEO_PATH + id) | ||
.replyWithFile(200, watch); | ||
ytdl.getInfo(url, { downloadURL: true }, function(err, info) { | ||
if (err) return done(err); | ||
assert.ok(info); | ||
scope1.done(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -80,0 +94,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6300426
36
26224
86