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

Commit

Permalink
fix: get binary url from github releases
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Feb 18, 2021
1 parent 1b4e5a6 commit 173c5fa
Showing 1 changed file with 37 additions and 48 deletions.
85 changes: 37 additions & 48 deletions lib/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
const request = require('request')
const mkdirp = require('mkdirp')
const path = require('path')
const fs = require('fs')
const util = require('util')
const fs = require('fs')

const ENDPOINT =
process.env.YOUTUBE_DL_DOWNLOAD_HOST ||
'https://youtube-dl-binary.vercel.app/'

const [, , ...flags] = process.argv

Expand All @@ -15,30 +19,17 @@ const isOverwrite = flags.includes('--overwrite')
let dir, filePath
const defaultBin = path.join(__dirname, '..', 'bin')
const defaultPath = path.join(defaultBin, 'details')
const url = process.env.YOUTUBE_DL_DOWNLOAD_HOST || 'https://yt-dl.org/downloads/latest/youtube-dl'

function download (url, callback) {
let status

// download the correct version of the binary based on the platform
url = exec(url)

request.get(url, { followRedirect: false }, function (err, res) {
request.get(url, function (err, res) {
if (err) return callback(err)

if (res.statusCode !== 302) {
return callback(
new Error(
'Did not get redirect for the latest version link. Status: ' +
res.statusCode
)
)
}

const url = res.headers.location
const downloadFile = request.get(url)
const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(
url
const binaryUrl = res.body
const downloadFile = request.get(binaryUrl)
const version = /releases\/download\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(
binaryUrl
)[1]

downloadFile.on('response', function response (res) {
Expand All @@ -47,13 +38,11 @@ function download (url, callback) {
return
}

const outputStream = fs.createWriteStream(filePath, { mode: 493 });
outputStream.on(
'close',
function end() {
callback(status, newVersion);
});
downloadFile.pipe(outputStream);
const outputStream = fs.createWriteStream(filePath, { mode: 493 })
outputStream.on('close', function end () {
callback(status, version)
})
downloadFile.pipe(outputStream)
})

downloadFile.on('error', function error (err) {
Expand All @@ -77,8 +66,7 @@ function downloader (binDir, callback) {
if (typeof binDir === 'function') {
callback = binDir
binDir = null
}
else if (!callback) {
} else if (!callback) {
return util.promisify(downloader)(binDir)
}

Expand All @@ -87,31 +75,32 @@ function downloader (binDir, callback) {
// handle overwritin
if (fs.existsSync(filePath)) {
if (!isOverwrite) {
return callback("File exists");
}
else {
return callback('File exists')
} else {
try {
fs.unlinkSync(filePath);
}
catch (e) {
callback(e);
fs.unlinkSync(filePath)
} catch (e) {
callback(e)
}
}
}

download(url, function error (err, newVersion) {
if (err) return callback(err)
fs.writeFileSync(
defaultPath,
JSON.stringify({
version: newVersion,
path: binDir ? filePath : binDir,
exec: exec('youtube-dl')
}),
'utf8'
)
return callback(null, 'Downloaded youtube-dl ' + newVersion)
})
download(
`${ENDPOINT}?platform=${isWin ? 'windows' : 'linux'}`,
function error (err, newVersion) {
if (err) return callback(err)
fs.writeFileSync(
defaultPath,
JSON.stringify({
version: newVersion,
path: binDir ? filePath : binDir,
exec: exec('youtube-dl')
}),
'utf8'
)
return callback(null, 'Downloaded youtube-dl ' + newVersion)
}
)
}

module.exports = downloader

0 comments on commit 173c5fa

Please sign in to comment.