ytdl-core
Advanced tools
Comparing version 0.1.0 to 0.2.0
var request = require('request'); | ||
var EventVat = require('eventvat'); | ||
var ytdl = {}; | ||
// Circular dependency. | ||
process.nextTick(function() { | ||
// Cache for recently looked up video infos. | ||
ytdl = require('./index'); | ||
}); | ||
/** | ||
* Makes a request and caches the body so that additional requests | ||
* to the same url don't require network IO. | ||
* | ||
* @param {Object|String} options | ||
* @param {Function} callback | ||
*/ | ||
module.exports = function(options, callback) { | ||
var url = options.url || options.uri || options; | ||
if (ytdl.cache && ytdl.cache.exists(url)) { | ||
process.nextTick(function() { | ||
callback(null, ytdl.cache.get(url)); | ||
}); | ||
} else { | ||
request(options, function(err, res, body) { | ||
if (err) return callback(err); | ||
if (res.statusCode !== 200) { | ||
return callback(new Error('status code ' + res.statusCode)); | ||
} | ||
if (ytdl.cache) { | ||
ytdl.cache.set(url, body); | ||
} | ||
callback(null, body); | ||
}); | ||
} | ||
request(options, function(err, res, body) { | ||
if (err) return callback(err); | ||
if (res.statusCode !== 200) { | ||
return callback(new Error('status code ' + res.statusCode)); | ||
} | ||
callback(null, body); | ||
}); | ||
}; | ||
// Export cache. | ||
module.exports.cache = new EventVat({ autoexpire: 30 }); |
112
lib/index.js
@@ -1,9 +0,12 @@ | ||
var http = require('http'); | ||
var streamify = require('streamify'); | ||
var request = require('request'); | ||
var _ = require('underscore'); | ||
var getInfo = require('./info'); | ||
var crequest = require('./crequest'); | ||
var util = require('./util'); | ||
var http = require('http'); | ||
var PassThrough = require('stream').PassThrough; | ||
var request = require('request'); | ||
var _ = require('underscore'); | ||
var getInfo = require('./info'); | ||
var crequest = require('./crequest'); | ||
var util = require('./util'); | ||
module.exports = ytdl; | ||
ytdl.getInfo = getInfo; | ||
ytdl.downloadFromInfo = downloadFromInfo; | ||
@@ -15,52 +18,79 @@ /** | ||
*/ | ||
var ytdl = module.exports = function ytdl(link, options) { | ||
var stream = streamify({ | ||
superCtor: http.ClientResponse, | ||
readable: true, | ||
writable: false | ||
}); | ||
function ytdl(link, options) { | ||
options = options || {}; | ||
options.downloadURL = true; | ||
var stream = new PassThrough(); | ||
getInfo(link, options, function(err, info) { | ||
if (err) { | ||
stream.emit('error', err); | ||
stream.readable = false; | ||
return; | ||
} | ||
var format = util.chooseFormat(info.formats, options); | ||
if (format instanceof Error) { | ||
stream.emit('error', format); | ||
downloadFromInfoCallback(info, options, function(err, format, videoStream) { | ||
if (err) { | ||
stream.emit('error', err); | ||
return; | ||
} | ||
stream.emit('info', info, format); | ||
videoStream.pipe(stream); | ||
}); | ||
}); | ||
return stream; | ||
} | ||
function downloadFromInfoCallback(info, options, callback) { | ||
options = options || {}; | ||
var format = util.chooseFormat(info.formats, options); | ||
if (format instanceof Error) { | ||
// The caller expects this function to be async. | ||
setImmediate(callbackWithFormatError); | ||
return; | ||
} | ||
var requestOptions = _.clone(options); | ||
requestOptions.url = format.url; | ||
if (requestOptions.range) { | ||
requestOptions.url += '&range=' + requestOptions.range; | ||
} | ||
delete requestOptions.quality; | ||
delete requestOptions.range; | ||
delete requestOptions.filter; | ||
// Start downloading the video. | ||
var req = request(requestOptions); | ||
req.on('error', callback); | ||
req.on('response', function(res) { | ||
if (res.statusCode !== 200) { | ||
callback(new Error('status code ' + res.statusCode)); | ||
return; | ||
} | ||
var requestOptions = _.clone(options); | ||
requestOptions.url = format.url; | ||
if (requestOptions.range) { | ||
requestOptions.url += '&range=' + requestOptions.range; | ||
format.size = res.headers['content-length']; | ||
callback(null, format, req); | ||
}); | ||
function callbackWithFormatError() { | ||
callback(format); | ||
} | ||
} | ||
function downloadFromInfo(info, options) { | ||
var stream = new PassThrough(); | ||
options = options || {}; | ||
downloadFromInfoCallback(info, options, function(err, format, videoStream) { | ||
if (err) { | ||
stream.emit('error', err); | ||
return; | ||
} | ||
delete requestOptions.quality; | ||
delete requestOptions.range; | ||
delete requestOptions.filter; | ||
// Start downloading the video. | ||
var req = request(requestOptions); | ||
req.on('response', function(res) { | ||
if (res.statusCode !== 200) { | ||
stream.emit('error', new Error('status code ' + res.statusCode)); | ||
return; | ||
} | ||
format.size = res.headers['content-length']; | ||
stream.emit('info', info, format); | ||
}); | ||
stream.resolve(req); | ||
stream.emit('format', format); | ||
videoStream.pipe(stream); | ||
}); | ||
return stream; | ||
}; | ||
ytdl.getInfo = getInfo; | ||
ytdl.cache = crequest.cache; | ||
} |
{ | ||
"name": "ytdl-core", | ||
"description": "Youtube video downloader in pure javascript.", | ||
"keywords": ["youtube", "video", "download"], | ||
"version": "0.1.0", | ||
"keywords": [ | ||
"youtube", | ||
"video", | ||
"download" | ||
], | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -13,9 +17,7 @@ "type": "git", | ||
"scripts": { | ||
"test": "mocha -R spec -t 2000 test/*-test.js" | ||
"test": "mocha -R spec -t 4000 test/*-test.js" | ||
}, | ||
"dependencies": { | ||
"eventvat": "~0.2.1", | ||
"jstream": "~0.2.7", | ||
"request": "~2.36.0", | ||
"streamify": "~0.2.0", | ||
"underscore": "~1.6.0" | ||
@@ -27,8 +29,9 @@ }, | ||
"stream-equal": "~0.1.0" | ||
}, | ||
"licenses": [{ | ||
"type": "MIT", | ||
"url" : "http://github.com/fent/node-ytdl-core/raw/master/LICENSE" | ||
}] | ||
"licenses": [ | ||
{ | ||
"type": "MIT", | ||
"url": "http://github.com/fent/node-ytdl-core/raw/master/LICENSE" | ||
} | ||
] | ||
} |
@@ -37,3 +37,3 @@ # node-ytdl-core [![Build Status](https://secure.travis-ci.org/fent/node-ytdl-core.png)](http://travis-ci.org/fent/node-ytdl-core) | ||
### Event: 'info' | ||
#### Event: 'info' | ||
* `Object` - Info. | ||
@@ -52,10 +52,17 @@ * `Object` - Format. | ||
### ytdl.cache | ||
### ytdl.downloadFromInfo(info, options) | ||
A [memory cache](https://github.com/hij1nx/EventVat) is used to store information about recently retrieved videos. This is used to prevent double requests on videos that you want to retrieve the info of, and then download. | ||
Once you have received metadata from a video with the `getInfo` function, | ||
you may pass that `info`, along with other `options` to `downloadFromInfo`. | ||
The returned readable stream emits these additional events: | ||
#### Event: 'format' | ||
* `Object` - Format. | ||
Emitted when a format metadata has been chosen. `format.size` will also be available. | ||
# Install | ||
npm install ytdl | ||
npm install ytdl-core | ||
@@ -62,0 +69,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
4038452
3
77
16888
- Removedeventvat@~0.2.1
- Removedstreamify@~0.2.0
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedarraybuffer.prototype.slice@1.0.3(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removeddata-view-buffer@1.0.1(transitive)
- Removeddata-view-byte-length@1.0.1(transitive)
- Removeddata-view-byte-offset@1.0.0(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removedes-abstract@1.23.5(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedes-set-tostringtag@2.0.3(transitive)
- Removedes-to-primitive@1.2.1(transitive)
- Removedeventemitter2@6.4.9(transitive)
- Removedeventvat@0.2.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunction.prototype.name@1.1.6(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedget-symbol-description@1.0.2(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhashish@0.0.4(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-data-view@1.0.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-negative-zero@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedis-weakref@1.0.2(transitive)
- Removedisarray@2.0.5(transitive)
- Removedobject-inspect@1.13.3(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedregexp.prototype.flags@1.5.3(transitive)
- Removedsafe-array-concat@1.1.2(transitive)
- Removedsafe-regex-test@1.0.3(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedstreamify@0.2.9(transitive)
- Removedstring.prototype.trim@1.2.9(transitive)
- Removedstring.prototype.trimend@1.0.8(transitive)
- Removedstring.prototype.trimstart@1.0.8(transitive)
- Removedtraverse@0.6.10(transitive)
- Removedtyped-array-buffer@1.0.2(transitive)
- Removedtyped-array-byte-length@1.0.1(transitive)
- Removedtyped-array-byte-offset@1.0.2(transitive)
- Removedtyped-array-length@1.0.6(transitive)
- Removedtypedarray.prototype.slice@1.0.3(transitive)
- Removedunbox-primitive@1.0.2(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)