From 606c946e74211c4123b16087288902226306198d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 19 Sep 2018 16:24:24 +0200 Subject: [PATCH] Move youtubeDL upgrader in helpers/ --- server/helpers/youtube-dl.ts | 69 ++++++++++++++++++- .../schedulers/youtube-dl-update-scheduler.ts | 60 ++-------------- 2 files changed, 70 insertions(+), 59 deletions(-) diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts index 6738090f3..db2bddf78 100644 --- a/server/helpers/youtube-dl.ts +++ b/server/helpers/youtube-dl.ts @@ -2,7 +2,11 @@ import { truncate } from 'lodash' import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' import { logger } from './logger' import { generateVideoTmpPath } from './utils' -import { YoutubeDlUpdateScheduler } from '../lib/schedulers/youtube-dl-update-scheduler' +import { join } from 'path' +import { root } from './core-utils' +import { ensureDir, writeFile } from 'fs-extra' +import * as request from 'request' +import { createWriteStream } from 'fs' export type YoutubeDLInfo = { name?: string @@ -40,7 +44,7 @@ function downloadYoutubeDLVideo (url: string) { return new Promise(async (res, rej) => { const youtubeDL = await safeGetYoutubeDL() - youtubeDL.exec(url, options, async (err, output) => { + youtubeDL.exec(url, options, err => { if (err) return rej(err) return res(path) @@ -48,9 +52,68 @@ function downloadYoutubeDLVideo (url: string) { }) } +// Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js +// We rewrote it to avoid sync calls +async function updateYoutubeDLBinary () { + logger.info('Updating youtubeDL binary.') + + const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin') + const bin = join(binDirectory, 'youtube-dl') + const detailsPath = join(binDirectory, 'details') + const url = 'https://yt-dl.org/downloads/latest/youtube-dl' + + await ensureDir(binDirectory) + + return new Promise(res => { + request.get(url, { followRedirect: false }, (err, result) => { + if (err) { + logger.error('Cannot update youtube-dl.', { err }) + return res() + } + + if (result.statusCode !== 302) { + logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode) + return res() + } + + const url = result.headers.location + const downloadFile = request.get(url) + const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ] + + downloadFile.on('response', result => { + if (result.statusCode !== 200) { + logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode) + return res() + } + + downloadFile.pipe(createWriteStream(bin, { mode: 493 })) + }) + + downloadFile.on('error', err => { + logger.error('youtube-dl update error.', { err }) + return res() + }) + + downloadFile.on('end', () => { + const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' }) + writeFile(detailsPath, details, { encoding: 'utf8' }, err => { + if (err) { + logger.error('youtube-dl update error: cannot write details.', { err }) + return res() + } + + logger.info('youtube-dl updated to version %s.', newVersion) + return res() + }) + }) + }) + }) +} + // --------------------------------------------------------------------------- export { + updateYoutubeDLBinary, downloadYoutubeDLVideo, getYoutubeDLInfo } @@ -64,7 +127,7 @@ async function safeGetYoutubeDL () { youtubeDL = require('youtube-dl') } catch (e) { // Download binary - await YoutubeDlUpdateScheduler.Instance.execute() + await updateYoutubeDLBinary() youtubeDL = require('youtube-dl') } diff --git a/server/lib/schedulers/youtube-dl-update-scheduler.ts b/server/lib/schedulers/youtube-dl-update-scheduler.ts index faadb4334..2fc8950fe 100644 --- a/server/lib/schedulers/youtube-dl-update-scheduler.ts +++ b/server/lib/schedulers/youtube-dl-update-scheduler.ts @@ -1,5 +1,4 @@ -// Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js -// We rewrote it to avoid sync calls + import { AbstractScheduler } from './abstract-scheduler' import { SCHEDULER_INTERVALS_MS } from '../../initializers' @@ -8,6 +7,7 @@ import * as request from 'request' import { createWriteStream, ensureDir, writeFile } from 'fs-extra' import { join } from 'path' import { root } from '../../helpers/core-utils' +import { updateYoutubeDLBinary } from '../../helpers/youtube-dl' export class YoutubeDlUpdateScheduler extends AbstractScheduler { @@ -19,60 +19,8 @@ export class YoutubeDlUpdateScheduler extends AbstractScheduler { super() } - async execute () { - logger.info('Updating youtubeDL binary.') - - const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin') - const bin = join(binDirectory, 'youtube-dl') - const detailsPath = join(binDirectory, 'details') - const url = 'https://yt-dl.org/downloads/latest/youtube-dl' - - await ensureDir(binDirectory) - - return new Promise(res => { - request.get(url, { followRedirect: false }, (err, result) => { - if (err) { - logger.error('Cannot update youtube-dl.', { err }) - return res() - } - - if (result.statusCode !== 302) { - logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode) - return res() - } - - const url = result.headers.location - const downloadFile = request.get(url) - const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ] - - downloadFile.on('response', result => { - if (result.statusCode !== 200) { - logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode) - return res() - } - - downloadFile.pipe(createWriteStream(bin, { mode: 493 })) - }) - - downloadFile.on('error', err => { - logger.error('youtube-dl update error.', { err }) - return res() - }) - - downloadFile.on('end', () => { - const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' }) - writeFile(detailsPath, details, { encoding: 'utf8' }, err => { - if (err) { - logger.error('youtube-dl update error: cannot write details.', { err }) - return res() - } - - logger.info('youtube-dl updated to version %s.', newVersion) - return res() - }) - }) - }) - }) + execute () { + return updateYoutubeDLBinary() } static get Instance () { -- 2.25.1