Ensure youtubedl binary exists in ydl helper

pull/926/head
Chocobozzz 2018-08-16 09:45:51 +02:00
parent 8569a870e4
commit 4f1f6f0383
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 59 additions and 34 deletions

View File

@ -1,8 +1,8 @@
import * as youtubeDL from 'youtube-dl'
import { truncate } from 'lodash' import { truncate } from 'lodash'
import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers'
import { logger } from './logger' import { logger } from './logger'
import { generateVideoTmpPath } from './utils' import { generateVideoTmpPath } from './utils'
import { YoutubeDlUpdateScheduler } from '../lib/schedulers/youtube-dl-update-scheduler'
export type YoutubeDLInfo = { export type YoutubeDLInfo = {
name?: string name?: string
@ -15,9 +15,10 @@ export type YoutubeDLInfo = {
} }
function getYoutubeDLInfo (url: string): Promise<YoutubeDLInfo> { function getYoutubeDLInfo (url: string): Promise<YoutubeDLInfo> {
return new Promise<YoutubeDLInfo>((res, rej) => { return new Promise<YoutubeDLInfo>(async (res, rej) => {
const options = [ '-j', '--flat-playlist' ] const options = [ '-j', '--flat-playlist' ]
const youtubeDL = await safeGetYoutubeDL()
youtubeDL.getInfo(url, options, (err, info) => { youtubeDL.getInfo(url, options, (err, info) => {
if (err) return rej(err) if (err) return rej(err)
@ -35,7 +36,8 @@ function downloadYoutubeDLVideo (url: string) {
const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
return new Promise<string>((res, rej) => { return new Promise<string>(async (res, rej) => {
const youtubeDL = await safeGetYoutubeDL()
youtubeDL.exec(url, options, async (err, output) => { youtubeDL.exec(url, options, async (err, output) => {
if (err) return rej(err) if (err) return rej(err)
@ -53,6 +55,20 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
async function safeGetYoutubeDL () {
let youtubeDL
try {
youtubeDL = require('youtube-dl')
} catch (e) {
// Download binary
await YoutubeDlUpdateScheduler.Instance.execute()
youtubeDL = require('youtube-dl')
}
return youtubeDL
}
function normalizeObject (obj: any) { function normalizeObject (obj: any) {
const newObj: any = {} const newObj: any = {}

View File

@ -14,5 +14,5 @@ export abstract class AbstractScheduler {
clearInterval(this.interval) clearInterval(this.interval)
} }
protected abstract execute () abstract execute ()
} }

View File

@ -7,7 +7,7 @@ import { logger } from '../../helpers/logger'
import * as request from 'request' import * as request from 'request'
import { createWriteStream, writeFile } from 'fs' import { createWriteStream, writeFile } from 'fs'
import { join } from 'path' import { join } from 'path'
import { root } from '../../helpers/core-utils' import { mkdirpPromise, root } from '../../helpers/core-utils'
export class YoutubeDlUpdateScheduler extends AbstractScheduler { export class YoutubeDlUpdateScheduler extends AbstractScheduler {
@ -20,49 +20,58 @@ export class YoutubeDlUpdateScheduler extends AbstractScheduler {
} }
async execute () { async execute () {
logger.info('Updating youtubeDL binary.')
const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin') const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin')
const bin = join(binDirectory, 'youtube-dl') const bin = join(binDirectory, 'youtube-dl')
const detailsPath = join(binDirectory, 'details') const detailsPath = join(binDirectory, 'details')
const url = 'https://yt-dl.org/downloads/latest/youtube-dl' const url = 'https://yt-dl.org/downloads/latest/youtube-dl'
request.get(url, { followRedirect: false }, (err, res) => { await mkdirpPromise(binDirectory)
return new Promise(res => {
request.get(url, { followRedirect: false }, (err, result) => {
if (err) { if (err) {
logger.error('Cannot update youtube-dl.', { err }) logger.error('Cannot update youtube-dl.', { err })
return return res()
} }
if (res.statusCode !== 302) { if (result.statusCode !== 302) {
logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', res.statusCode) logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode)
return return res()
} }
const url = res.headers.location const url = result.headers.location
const downloadFile = request.get(url) const downloadFile = request.get(url)
const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[1] const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ]
downloadFile.on('response', res => { downloadFile.on('response', result => {
if (res.statusCode !== 200) { if (result.statusCode !== 200) {
logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', res.statusCode) logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode)
return return res()
} }
downloadFile.pipe(createWriteStream(bin, { mode: 493 })) downloadFile.pipe(createWriteStream(bin, { mode: 493 }))
}) })
downloadFile.on('error', err => logger.error('youtube-dl update error.', { err })) downloadFile.on('error', err => {
logger.error('youtube-dl update error.', { err })
return res()
})
downloadFile.on('end', () => { downloadFile.on('end', () => {
const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' }) const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' })
writeFile(detailsPath, details, { encoding: 'utf8' }, err => { writeFile(detailsPath, details, { encoding: 'utf8' }, err => {
if (err) { if (err) {
logger.error('youtube-dl update error: cannot write details.', { err }) logger.error('youtube-dl update error: cannot write details.', { err })
return return res()
} }
logger.info('youtube-dl updated to version %s.', newVersion) logger.info('youtube-dl updated to version %s.', newVersion)
return res()
})
}) })
}) })
}) })
} }