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

Commit

Permalink
refactor: remove dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed May 22, 2019
1 parent 5ce37dd commit 1a74f06
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 112 deletions.
7 changes: 6 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Arguments we dont want users to use with youtube-dl
// because they will break the module.
var badArgs = [
const badArgs = [
'-h',
'--help',
'-v',
Expand Down Expand Up @@ -85,3 +85,8 @@ exports.isString = str => typeof str === 'string'
* @return {Boolean}
*/
exports.has = (arr, value) => arr && arr.indexOf(value) > -1

exports.isYouTubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\//

exports.isWin =
process.platform === 'win32' || process.env.NODE_PLATFORM === 'windows'
180 changes: 69 additions & 111 deletions lib/youtube-dl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ const http = require('http')
const url = require('url')
const fs = require('fs')

const util = require('./util')
const {
isYouTubeRegex,
isWin,
formatDuration,
has,
isString
} = require('./util')

const detailsPath = path.join(__dirname, '..', 'bin/details')

const TEN_MEGABYTES = 1000 * 1000 * 10

const execFileOpts = { maxBuffer: TEN_MEGABYTES }

const detailsPath = path.join(__dirname, '..', 'bin/details')

let ytdlBinary = (() => {
if (fs.existsSync(detailsPath)) {
const details = JSON.parse(fs.readFileSync(detailsPath))
Expand All @@ -34,20 +40,14 @@ let ytdlBinary = (() => {
}
})()

const isWin = !!(
process.platform === 'win32' || process.env.NODE_PLATFORM === 'windows'
)

const isYouTubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\//

function youtubeDl (args, options, callback) {
function youtubeDl (args, options, cb) {
execFile(ytdlBinary, args, { ...execFileOpts, ...options }, function done (
err,
stdout,
stderr
) {
if (err) return callback(err)
return callback(null, stdout.trim().split(/\r?\n/))
if (err) return cb(err)
return cb(null, stdout.trim().split(/\r?\n/))
})
}

Expand All @@ -63,9 +63,10 @@ function processData (data, options, stream) {
const item = !data.length ? data : data.shift()

// fix for pause/resume downloads
const headers = { Host: url.parse(item.url).hostname }

Object.assign(headers, data.http_headers)
const headers = Object.assign(
{ Host: url.parse(item.url).hostname },
data.http_headers
)

if (options && options.start > 0) {
headers.Range = 'bytes=' + options.start + '-'
Expand Down Expand Up @@ -110,7 +111,7 @@ const ytdl = (module.exports = function (videoUrl, args, options) {
writable: false
})

if (!util.isString(videoUrl)) {
if (!isString(videoUrl)) {
processData(videoUrl, options, stream)
return stream
}
Expand All @@ -123,39 +124,27 @@ const ytdl = (module.exports = function (videoUrl, args, options) {
})

/**
* Calls youtube-dl with some arguments and the `callback`
* Calls youtube-dl with some arguments and the `cb`
* gets called with the output.
*
* @param {String|Array.<String>}
* @param {Array.<String>} args
* @param {Array.<String>} args2
* @param {Object} options
* @param {Function(!Error, String)} callback
* @param {Function(!Error, String)} cb
*/
function call (urls, args1, args2, options = {}, callback) {
function call (urls, args1, args2, options = {}, cb) {
let args = args1
if (args2) args = args.concat(args2)

// set encoding on windows to support unicode titles
if (isWin) {
// check if encoding is already set
let hasEncoding = false
for (let i = 0; i < args.length; i++) {
if (args[i] === '--encoding') {
hasEncoding = true
break
}
}
if (!hasEncoding) {
args.push('--encoding')
args.push('utf8')
}
// check if encoding is already set
if (isWin && !args.includes('--encoding')) {
args.push('--encoding')
args.push('utf8')
}

if (urls !== null) {
if (util.isString(urls)) {
urls = [urls]
}
if (isString(urls)) urls = [urls]

for (let i = 0; i < urls.length; i++) {
const video = urls[i]
Expand All @@ -180,20 +169,20 @@ function call (urls, args1, args2, options = {}, callback) {
}
}

return youtubeDl(args, options, callback)
return youtubeDl(args, options, cb)
}

/**
* Calls youtube-dl with some arguments and the `callback`
* Calls youtube-dl with some arguments and the `cb`
* gets called with the output.
*
* @param {String} url
* @param {Array.<String>} args
* @param {Object} options
* @param {Function(!Error, String)} callback
* @param {Function(!Error, String)} cb
*/
ytdl.exec = function exec (url, args, options, callback) {
return call(url, [], args, options, callback)
ytdl.exec = function exec (url, args, options, cb) {
return call(url, [], args, options, cb)
}

/**
Expand All @@ -202,39 +191,17 @@ ytdl.exec = function exec (url, args, options, callback) {
*/
function parseInfo (data) {
// youtube-dl might return just an url as a string when using the "-g" or "--get-url" flag
if (util.isString(data) && data.startsWith('http')) {
if (isString(data) && data.startsWith('http')) {
data = JSON.stringify({ url: data })
}

const info = JSON.parse(data)

// Add and process some entries to keep backwards compatibility
Object.defineProperty(info, 'filename', {
get: function get () {
console.warn('`info.filename` is deprecated, use `info._filename`')
return info._filename
}
})
Object.defineProperty(info, 'itag', {
get: function get () {
console.warn('`info.itag` is deprecated, use `info.format_id`')
return info.format_id
}
})
Object.defineProperty(info, 'resolution', {
get: function get () {
console.warn('`info.resolution` is deprecated, use `info.format`')
return info.format.split(' - ')[1]
}
})

info._duration_raw = info.duration
info._duration_hms = info.duration
? hms.fromS(info.duration, 'hh:mm:ss')
: info.duration
info.duration = info.duration
? util.formatDuration(info.duration)
: info.duration
info.duration = info.duration ? formatDuration(info.duration) : info.duration

return info
}
Expand Down Expand Up @@ -263,22 +230,22 @@ ytdl.getYtdlBinary = function getYtdlBinary () {
* @param {String} url
* @param {Array.<String>} args
* @param {Object} options
* @param {Function(!Error, Object)} callback
* @param {Function(!Error, Object)} cb
*/
ytdl.getInfo = function getInfo (url, args, options, callback) {
ytdl.getInfo = function getInfo (url, args, options, cb) {
if (typeof options === 'function') {
callback = options
cb = options
options = {}
} else if (typeof args === 'function') {
callback = args
cb = args
options = {}
args = []
}
const defaultArgs = ['--dump-json']
if (
!args ||
(!util.has(args, '-f') &&
!util.has(args, '--format') &&
(!has(args, '-f') &&
!has(args, '--format') &&
args.every(function (a) {
return a.indexOf('--format=') !== 0
}))
Expand All @@ -288,22 +255,22 @@ ytdl.getInfo = function getInfo (url, args, options, callback) {
}

call(url, defaultArgs, args, options, function done (err, data) {
if (err) return callback(err)
if (err) return cb(err)
let info

// If using the "-g" or "--get-url" flag youtube-dl will return just a string (the URL to the video) which messes up the parsing
// This fixes this behaviour
if (util.has(args, '-g') || util.has(args, '--get-url')) {
if (has(args, '-g') || has(args, '--get-url')) {
if (Array.isArray(data) && data.length >= 2) data.splice(0, 1)
}

try {
info = data.map(parseInfo)
} catch (err) {
return callback(err)
return cb(err)
}

return callback(null, info.length === 1 ? info[0] : info)
return cb(null, info.length === 1 ? info[0] : info)
})
}

Expand All @@ -315,41 +282,34 @@ ytdl.getInfo = function getInfo (url, args, options, callback) {
* {String} lang
* {String} format
* {String} cwd
* @param {Function(!Error, Object)} callback
* @param {Function(!Error, Object)} cb
*/
ytdl.getSubs = function getSubs (url, options, callback) {
ytdl.getSubs = function getSubs (url, options, cb) {
if (typeof options === 'function') {
callback = options
cb = options
options = {}
}

const args = ['--skip-download']
args.push('--write' + (options.auto ? '-auto' : '') + '-sub')
if (options.all) {
args.push('--all-subs')
}
if (options.lang) {
args.push('--sub-lang=' + options.lang)
}
if (options.format) {
args.push('--sub-format=' + options.format)
}
if (!options.warrning) {
args.push('--no-warnings')
}
if (options.all) args.push('--all-subs')
if (options.lang) args.push('--sub-lang=' + options.lang)
if (options.format) args.push('--sub-format=' + options.format)
if (!options.warrning) args.push('--no-warnings')

call(url, args, [], { cwd: options.cwd }, function (err, data) {
if (err) return callback(err)
if (err) return cb(err)

const files = []

for (let i = 0, len = data.length; i < len; i++) {
const line = data[i]
if (line.indexOf('[info] Writing video subtitles to: ') === 0) {
files.push(line.slice(35))
}
}

return callback(null, files)
return cb(null, files)
})
}

Expand All @@ -358,58 +318,56 @@ ytdl.getSubs = function getSubs (url, options, callback) {
* @param {Object} options
* {Boolean} all
* {String} cwd
* @param {Function(!Error, Object)} callback
* @param {Function(!Error, Object)} cb
*/
ytdl.getThumbs = function getThumbs (url, options, callback) {
ytdl.getThumbs = function getThumbs (url, options, cb) {
if (typeof options === 'function') {
callback = options
cb = options
options = {}
}

const args = ['--skip-download']

if (options.all) {
args.push('--write-all-thumbnails')
} else {
args.push('--write-thumbnail')
}
if (options.all) args.push('--write-all-thumbnails')
else args.push('--write-thumbnail')

if (!options.warrning) {
args.push('--no-warnings')
}
if (!options.warrning) args.push('--no-warnings')

call(url, args, [], { cwd: options.cwd }, function (err, data) {
if (err) return callback(err)
if (err) return cb(err)

const files = []

for (let i = 0, len = data.length; i < len; i++) {
const line = data[i]
const info = 'Writing thumbnail to: '
if (util.has(line, info)) {
if (has(line, info)) {
files.push(line.slice(line.indexOf(info) + info.length))
}
}
return callback(null, files)

return cb(null, files)
})
}

/**
* @param {!Boolean} descriptions
* @param {!Object} options
* @param {Function(!Error, Object)} callback
* @param {Function(!Error, Object)} cb
*/
ytdl.getExtractors = function getExtractors (descriptions, options, callback) {
ytdl.getExtractors = function getExtractors (descriptions, options, cb) {
if (typeof options === 'function') {
callback = options
cb = options
options = {}
} else if (typeof descriptions === 'function') {
callback = descriptions
cb = descriptions
options = {}
descriptions = false
}

const args = descriptions
? ['--extractor-descriptions']
: ['--list-extractors']
return call(null, args, null, options, callback)

return call(null, args, null, options, cb)
}

0 comments on commit 1a74f06

Please sign in to comment.