Skip to content

Commit

Permalink
feat: add YOUTUBE_DL_HOST as binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 2, 2021
1 parent b82103c commit 2d723da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
2 changes: 2 additions & 0 deletions 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",
"get-stream": "~6.0.0",
"got": "~11.8.1",
"is-unix": "~1.0.0",
"mkdirp": "~1.0.4",
"p-event": "~4.2.0",
"p-reflect": "~2.1.0"
},
"devDependencies": {
Expand Down
41 changes: 22 additions & 19 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,25 @@ 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)
mkdirp(YOUTUBE_DL_DIR)
.then(() => getBinary(YOUTUBE_DL_HOST))
.then(buffer => fs.writeFile(YOUTUBE_DL_PATH, buffer, { mode: 493 }))
.then(message => message && console.log(message))
.catch(err => console.error(err.message || err))

0 comments on commit 2d723da

Please sign in to comment.