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

Commit

Permalink
Add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
David Smusz authored and David Smusz committed Jan 16, 2020
1 parent e02a280 commit 32b358b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ youtubedl.getInfo([url1, url2], function(err, info) {
})
```

### Using a proxy

``` js
const youtubedl = require('youtube-dl')

const video = youtubedl('http://www.youtube.com/watch?v=90AiXO1pAiA',
// Optional arguments passed to youtube-dl.
['--proxy', 'http://ip:port'],
```
### Downloading subtitles
``` js
Expand Down
12 changes: 7 additions & 5 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function youtubeDl (args, options, cb) {
* @param {Object} stream
*/

function processData (data, options, stream) {
function processData (data, args, options, stream) {
const item = !data.length ? data : data.shift()

// fix for pause/resume downloads
Expand All @@ -47,13 +47,15 @@ function processData (data, options, stream) {
headers.Range = 'bytes=' + options.start + '-' + options.end
} else {
headers.Range = 'bytes=0-' + item.filesize
}
}

const proxyArgIndex = args.indexOf('--proxy')
const req = request({
url: item.url,
headers: headers,
ecdhCurve: 'auto',
timeout: 30000
timeout: 30000,
...(proxyArgIndex > -1 && { proxy: args[proxyArgIndex + 1] })
})

req.on('response', function response (res) {
Expand Down Expand Up @@ -94,12 +96,12 @@ const ytdl = (module.exports = function (videoUrl, args, options) {
})

if (!isString(videoUrl)) {
processData(videoUrl, options, stream)
processData(videoUrl, args, options, stream)
return stream
}

ytdl.getInfo(videoUrl, args, options, function getInfo (err, data) {
return err ? stream.emit('error', err) : processData(data, options, stream)
return err ? stream.emit('error', err) : processData(data, args, options, stream)
})

return stream
Expand Down

0 comments on commit 32b358b

Please sign in to comment.