forked from fent/node-ytdl-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drop streamify, add downloadFromInfo
- Loading branch information
Showing
3 changed files
with
81 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,95 @@ | ||
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; | ||
|
||
/** | ||
* @param {String} link | ||
* @param {Object} options | ||
* @return {ReadableStream} | ||
*/ | ||
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); | ||
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('response', function(res) { | ||
if (res.statusCode !== 200) { | ||
stream.emit('error', new Error('status code ' + res.statusCode)); | ||
downloadFromInfoCallback(info, options, function(err, format, videoStream) { | ||
if (err) { | ||
stream.emit('error', err); | ||
return; | ||
} | ||
|
||
format.size = res.headers['content-length']; | ||
stream.emit('info', info, format); | ||
videoStream.pipe(stream); | ||
}); | ||
stream.resolve(req); | ||
}); | ||
|
||
return stream; | ||
}; | ||
} | ||
|
||
ytdl.getInfo = getInfo; | ||
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; | ||
} | ||
|
||
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; | ||
} | ||
|
||
stream.emit('format', format); | ||
videoStream.pipe(stream); | ||
}); | ||
|
||
return stream; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters