Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: download a binary from YOUTUBE_DL_HOST #2

Merged
merged 5 commits into from
Mar 2, 2021
Merged
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
Binary file removed bin/youtube-dl
Binary file not shown.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
"bin-version-check-cli": "~2.0.0",
"dargs": "~7.0.0",
"execa": "~5.0.0",
"got": "~11.8.1",
"get-stream": "~6.0.0",
"got": "~11.8.2",
"is-unix": "~1.0.0",
"mkdirp": "~1.0.4",
"p-event": "~4.2.0",
"p-reflect": "~2.1.0"
},
"devDependencies": {
Expand Down
41 changes: 21 additions & 20 deletions scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
'use strict'

const { promisify } = require('util')
const stream = require('stream')
const getStream = require('get-stream')
const fs = require('fs/promises')
const pEvent = require('p-event')
const mkdirp = require('mkdirp')

const got = require('got')
const fs = require('fs')

const pipeline = promisify(stream.pipeline)
const BINARY_CONTENT_TYPES = [
'binary/octet-stream',
'application/octet-stream',
'application/x-binary'
]

const {
YOUTUBE_DL_PATH,
Expand All @@ -15,27 +20,23 @@ const {
YOUTUBE_DL_FILENAME
} = require('../src/constants')

const getBinaryUrl = async endpoint => {
const [{ assets }] = await got(endpoint, {
responseType: 'json',
resolveBodyOnly: true
})
const getBinary = async url => {
const stream = got.stream(url)
const response = await pEvent(stream, 'response')
const contentType = response.headers['content-type']

if (BINARY_CONTENT_TYPES.includes(contentType)) {
return getStream(stream, { encoding: 'buffer' })
}

const [{ assets }] = JSON.parse(await getStream(stream))
const { browser_download_url: downloadUrl } = assets.find(
({ name }) => name === YOUTUBE_DL_FILENAME
)
return downloadUrl
}

const main = async url => {
await mkdirp(YOUTUBE_DL_DIR)
return pipeline(
got.stream(url),
fs.createWriteStream(YOUTUBE_DL_PATH, { mode: 493 })
)
return got(downloadUrl).buffer()
}

getBinaryUrl(YOUTUBE_DL_HOST)
.then(main)
.then(message => message && console.log(message))
Promise.all([getBinary(YOUTUBE_DL_HOST), mkdirp(YOUTUBE_DL_DIR)])
.then(([buffer]) => fs.writeFile(YOUTUBE_DL_PATH, buffer, { mode: 493 }))
.catch(err => console.error(err.message || err))
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const PLATFORM_WIN = 'windows'
const PLATFORM_UNIX = 'unix'

const YOUTUBE_DL_HOST =
process.env.YOUTUBE_DL_YOUTUBE_DL_HOST ||
process.env.YOUTUBE_DL_HOST ||
'https://api.github.com/repos/ytdl-org/youtube-dl/releases?per_page=1'

const YOUTUBE_DL_DIR =
Expand Down