X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fyoutube-dl.ts;h=748c67e6c6b65b97123c38b1c3099b8d2010ffb2;hb=cc68049424c2a7fc7fb919bc39a43f169e128780;hp=8b2bc17824f511866ffc1509dc7fa8afc71b7b43;hpb=8704acf49efc770d73bf07c10468ed8c74d28a83;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts index 8b2bc1782..748c67e6c 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 @@ -14,6 +18,10 @@ export type YoutubeDLInfo = { thumbnailUrl?: string } +const processOptions = { + maxBuffer: 1024 * 1024 * 10 // 10MB +} + function getYoutubeDLInfo (url: string, opts?: string[]): Promise { return new Promise(async (res, rej) => { const options = opts || [ '-j', '--flat-playlist' ] @@ -40,7 +48,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, processOptions, err => { if (err) return rej(err) return res(path) @@ -48,6 +56,64 @@ 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() + }) + }) + }) + }) +} + async function safeGetYoutubeDL () { let youtubeDL @@ -55,7 +121,7 @@ async function safeGetYoutubeDL () { youtubeDL = require('youtube-dl') } catch (e) { // Download binary - await YoutubeDlUpdateScheduler.Instance.execute() + await updateYoutubeDLBinary() youtubeDL = require('youtube-dl') } @@ -65,6 +131,7 @@ async function safeGetYoutubeDL () { // --------------------------------------------------------------------------- export { + updateYoutubeDLBinary, downloadYoutubeDLVideo, getYoutubeDLInfo, safeGetYoutubeDL