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

feat: use custom url for binary download #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,9 @@ Youtube-dl looks for certain environment variables to aid its operations. If You

- **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`.

- **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.
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.

### Tests

Tests are written with [vows](http://vowsjs.org/)
Expand Down
29 changes: 28 additions & 1 deletion lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const path = require('path')
const util = require('util')
const fs = require('fs')

const DIRECT_BINARY_DOWNLOAD_URL =
process.env.YOUTUBE_DL_DIRECT_BINARY_DOWNLOAD_URL

const ENDPOINT =
process.env.YOUTUBE_DL_DOWNLOAD_HOST ||
'https://youtube-dl-binary.vercel.app/'
Expand Down Expand Up @@ -72,7 +75,7 @@ function downloader (binDir, callback) {

createBase(binDir)

// handle overwritin
// handle overwriting
if (fs.existsSync(filePath)) {
if (!isOverwrite) {
return callback('File exists')
Expand All @@ -85,6 +88,30 @@ function downloader (binDir, callback) {
}
}

if (DIRECT_BINARY_DOWNLOAD_URL !== undefined) {
const binaryUrl = DIRECT_BINARY_DOWNLOAD_URL + isWin ? '.exe' : ''

const downloadFile = request.get(binaryUrl)
const outputStream = fs.createWriteStream(filePath, { mode: 493 })
outputStream.on('close', function end () {
callback(null, 'Downloaded youtube-dl from custom mirror.')
fs.writeFileSync(
defaultPath,
JSON.stringify({
version: 'custom-mirror-version',
path: binDir ? filePath : binDir,
exec: exec('youtube-dl')
}),
'utf8'
)
})
downloadFile.pipe(outputStream)
downloadFile.on('error', function error (err) {
callback(err)
})
return
}

download(
`${ENDPOINT}?platform=${isWin ? 'windows' : 'linux'}`,
function error (err, newVersion) {
Expand Down