Skip to content

Commit

Permalink
Merge pull request #2 from microlinkhq/next
Browse files Browse the repository at this point in the history
feat: download a binary from `YOUTUBE_DL_HOST`
  • Loading branch information
Kikobeats authored Mar 2, 2021
2 parents 20a885a + d6fa5fc commit 1aa1e23
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
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

0 comments on commit 1aa1e23

Please sign in to comment.