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

Commit 8168e99

Browse files
committed
feat: use custom url for binary download
1 parent 6131a9a commit 8168e99

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,9 @@ Youtube-dl looks for certain environment variables to aid its operations. If You
399399
400400
- **YOUTUBE\_DL\_DOWNLOAD\_HOST** - overwrite URL prefix that is used to download the binary file of Youtube-dl. Note: this includes protocol and might even include path prefix. Defaults to `https://yt-dl.org/downloads/latest/youtube-dl`.
401401

402+
- **YOUTUBE_DL_DIRECT_BINARY_DOWNLOAD_URL** - download the youtube-dl binary from the given download url, completely ignoring the `**YOUTUBE\_DL\_DOWNLOAD\_HOST**` option, while performing no lookup for retrieving the latest version of the binary. This is useful if you are hosting the youtube-dl binary on your own S3 bucket. The platform specific extension will be appended.
403+
E.g. the value `https://xxxxx.s3.xxxxxx.amazonaws.com/2020.11.12-youtube-dl` will result in downloading `https://xxxxx.s3.xxxxxx.amazonaws.com/2020.11.12-youtube-dl.exe` on windows.
404+
402405
### Tests
403406

404407
Tests are written with [vows](http://vowsjs.org/)

lib/downloader.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const path = require('path')
66
const util = require('util')
77
const fs = require('fs')
88

9+
const DIRECT_BINARY_DOWNLOAD_URL =
10+
process.env.YOUTUBE_DL_DIRECT_BINARY_DOWNLOAD_URL
11+
912
const ENDPOINT =
1013
process.env.YOUTUBE_DL_DOWNLOAD_HOST ||
1114
'https://youtube-dl-binary.vercel.app/'
@@ -72,7 +75,7 @@ function downloader (binDir, callback) {
7275

7376
createBase(binDir)
7477

75-
// handle overwritin
78+
// handle overwriting
7679
if (fs.existsSync(filePath)) {
7780
if (!isOverwrite) {
7881
return callback('File exists')
@@ -85,6 +88,30 @@ function downloader (binDir, callback) {
8588
}
8689
}
8790

91+
if (DIRECT_BINARY_DOWNLOAD_URL !== undefined) {
92+
const binaryUrl = DIRECT_BINARY_DOWNLOAD_URL + isWin ? '.exe' : ''
93+
94+
const downloadFile = request.get(binaryUrl)
95+
const outputStream = fs.createWriteStream(filePath, { mode: 493 })
96+
outputStream.on('close', function end () {
97+
callback(null, 'Downloaded youtube-dl from custom mirror.')
98+
fs.writeFileSync(
99+
defaultPath,
100+
JSON.stringify({
101+
version: 'custom-mirror-version',
102+
path: binDir ? filePath : binDir,
103+
exec: exec('youtube-dl')
104+
}),
105+
'utf8'
106+
)
107+
})
108+
downloadFile.pipe(outputStream)
109+
downloadFile.on('error', function error (err) {
110+
callback(err)
111+
})
112+
return
113+
}
114+
88115
download(
89116
`${ENDPOINT}?platform=${isWin ? 'windows' : 'linux'}`,
90117
function error (err, newVersion) {

0 commit comments

Comments
 (0)