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

Commit

Permalink
Merge branch 'master' into replacerequest
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/downloader.js
#	package.json
  • Loading branch information
Kikobeats committed Feb 18, 2021
2 parents dba5ff4 + e557188 commit 00abf7b
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 127 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
node_js:
- 12
- 10
- lts/*
notifications:
email:
on_success: change
Expand Down
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
## 3.3.0 (2021-02-18)

* ci: only lts ([cfa8056](https://github.com/przemyslawpluta/node-youtube-dl/commit/cfa8056))
* refactor: avoid url.pase ([b9633c7](https://github.com/przemyslawpluta/node-youtube-dl/commit/b9633c7))
* refactor: remove unnecessary code ([54f5234](https://github.com/przemyslawpluta/node-youtube-dl/commit/54f5234))
* refactor: tweak style ([51c619e](https://github.com/przemyslawpluta/node-youtube-dl/commit/51c619e))
* removes details file ([60afa91](https://github.com/przemyslawpluta/node-youtube-dl/commit/60afa91))



## 3.2.0 (2021-02-18)

* fix: get binary url from github releases ([173c5fa](https://github.com/przemyslawpluta/node-youtube-dl/commit/173c5fa))



## 3.1.0 (2021-01-10)

* build: add contributors ([c01efab](https://github.com/przemyslawpluta/node-youtube-dl/commit/c01efab))
* build: remove git add from git pre hooks ([8f779b7](https://github.com/przemyslawpluta/node-youtube-dl/commit/8f779b7))
* build: update dependencies ([ea20196](https://github.com/przemyslawpluta/node-youtube-dl/commit/ea20196))
* ci: update node builds ([9b87bad](https://github.com/przemyslawpluta/node-youtube-dl/commit/9b87bad))
* Execa function may not pass stdout in callback function if run it with the `{ stdio: 'inherit' }` op ([a85b449](https://github.com/przemyslawpluta/node-youtube-dl/commit/a85b449))
* fix #320: webp and not jpeg ([bae7c99](https://github.com/przemyslawpluta/node-youtube-dl/commit/bae7c99)), closes [#320](https://github.com/przemyslawpluta/node-youtube-dl/issues/320)
* handles overwriting ([a3551d1](https://github.com/przemyslawpluta/node-youtube-dl/commit/a3551d1))
* promise version of downloader ([e11831a](https://github.com/przemyslawpluta/node-youtube-dl/commit/e11831a))
* promise when there's no argument ([d6133fc](https://github.com/przemyslawpluta/node-youtube-dl/commit/d6133fc))
* README documentation for #271 ([2ab626e](https://github.com/przemyslawpluta/node-youtube-dl/commit/2ab626e)), closes [#271](https://github.com/przemyslawpluta/node-youtube-dl/issues/271)



## <small>3.0.2 (2020-01-31)</small>

* fix: don't fail on videos when ytdl returns filesize null ([8f6235b](https://github.com/przemyslawpluta/node-youtube-dl/commit/8f6235b))
Expand Down
99 changes: 39 additions & 60 deletions lib/downloader.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,47 @@
'use strict'

const stream = require('stream')
const {promisify} = require('util');
const { promisify } = require('util')
const got = require('got')
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 pipeline = promisify(stream.pipeline);
const pipeline = promisify(stream.pipeline)

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

const isWin = flags.includes('--platform=windows') || require('./util').isWin
const isOverwrite = flags.includes('--overwrite')

const getVersion = str =>
/releases\/download\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(str)[1]

// First, look for the download link.
let dir, filePath
let dir
let 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
let newVersion

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

got.get(url, { followRedirect: false })
.then(function(res) {
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
newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(
url
)[1]

return pipeline(
got.stream(url),
fs.createWriteStream(filePath, { mode: 493 })
);
}).then(function(res) {
callback(status, newVersion)
})
.catch(function(err) {
return callback(err)
got
.get(url)
.then(async res => {
const binaryUrl = res.body

await pipeline(
got.stream(binaryUrl),
fs.createWriteStream(filePath, { mode: 493 })
)

return binaryUrl
})
.then(binaryUrl => callback(null, getVersion(binaryUrl)))
.catch(callback)
}

const exec = path => (isWin ? path + '.exe' : path)
Expand All @@ -71,8 +59,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 @@ -81,31 +68,23 @@ function downloader (binDir, callback) {
// handle overwritin
if (fs.existsSync(filePath)) {
if (!isOverwrite) {
return callback("File exists");
return callback(new Error('File exists'))
}
else {
try {
fs.unlinkSync(filePath);
}
catch (e) {
callback(e);
}

try {
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)
return callback(null, 'Downloaded youtube-dl ' + newVersion)
}
)
}

module.exports = downloader
20 changes: 0 additions & 20 deletions lib/get-binary.js

This file was deleted.

23 changes: 15 additions & 8 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const universalify = require('universalify')
const streamify = require('streamify')
const got = require('got')
const hms = require('hh-mm-ss')
const { URL } = require('url')
const path = require('path')
const http = require('http')
const url = require('url')

const {
isYouTubeRegex,
Expand All @@ -15,14 +16,20 @@ const {
isString
} = require('./util')

let ytdlBinary = require('./get-binary')()
let ytdlBinary = path.resolve(__dirname, '..', 'bin', 'youtube-dl')

const execa = universalify.fromPromise(require('execa'))

function youtubeDl (args, options, cb) {
return execa(ytdlBinary, args, options, function done (err, output) {
return execa(ytdl.getYtdlBinary(), args, options, function done (
err,
output
) {
if (err) return cb(err)
return cb(null, output.stdout ? output.stdout.trim().split(/\r?\n/) : undefined)
return cb(
null,
output.stdout ? output.stdout.trim().split(/\r?\n/) : undefined
)
})
}

Expand All @@ -39,7 +46,7 @@ function processData (data, args, options, stream) {

// fix for pause/resume downloads
const headers = Object.assign(
{ Host: url.parse(item.url).hostname },
{ Host: new URL(item.url).hostname },
data.http_headers
)

Expand Down Expand Up @@ -136,13 +143,13 @@ function call (urls, args1, args2, options = {}, cb) {
const video = urls[i]
if (isYouTubeRegex.test(video)) {
// Get possible IDs.
const details = url.parse(video, true)
let id = details.query.v || ''
const videoUrlObj = new URL(video)
let id = videoUrlObj.searchParams.get('v') || ''
if (id) {
args.push('http://www.youtube.com/watch?v=' + id)
} else {
// Get possible IDs for youtu.be from urladdr.
id = details.pathname.slice(1).replace(/^v\//, '')
id = videoUrlObj.pathname.slice(1).replace(/^v\//, '')
if (id) {
args.push(video)
args.unshift('-i')
Expand Down
Loading

0 comments on commit 00abf7b

Please sign in to comment.