From ce32426be941164d041526e4748b4c83cdc6bf1c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 2 Aug 2018 16:02:51 +0200 Subject: Auto update youtube-dl --- server.ts | 2 + server/initializers/constants.ts | 3 +- .../lib/schedulers/youtube-dl-update-scheduler.ts | 72 ++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 server/lib/schedulers/youtube-dl-update-scheduler.ts diff --git a/server.ts b/server.ts index 0e6f787a6..9aaa64dbf 100644 --- a/server.ts +++ b/server.ts @@ -89,6 +89,7 @@ import { Redis } from './server/lib/redis' import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler' import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' +import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler' // ----------- Command line ----------- @@ -186,6 +187,7 @@ async function startApplication () { BadActorFollowScheduler.Instance.enable() RemoveOldJobsScheduler.Instance.enable() UpdateVideosScheduler.Instance.enable() + YoutubeDlUpdateScheduler.Instance.enable() // Redis initialization Redis.Instance.init() diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index fdd772d84..cc363d4f2 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -108,7 +108,8 @@ const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2 // 2 days let SCHEDULER_INTERVALS_MS = { badActorFollow: 60000 * 60, // 1 hour removeOldJobs: 60000 * 60, // 1 hour - updateVideos: 60000 // 1 minute + updateVideos: 60000, // 1 minute + youtubeDLUpdate: 60000 * 60 * 24 // 1 day } // --------------------------------------------------------------------------- diff --git a/server/lib/schedulers/youtube-dl-update-scheduler.ts b/server/lib/schedulers/youtube-dl-update-scheduler.ts new file mode 100644 index 000000000..b736f17ee --- /dev/null +++ b/server/lib/schedulers/youtube-dl-update-scheduler.ts @@ -0,0 +1,72 @@ +// Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js +// Use rewrote it to avoid sync calls + +import { AbstractScheduler } from './abstract-scheduler' +import { SCHEDULER_INTERVALS_MS } from '../../initializers' +import { logger } from '../../helpers/logger' +import * as request from 'request' +import { createWriteStream, writeFile } from 'fs' +import { join } from 'path' +import { root } from '../../helpers/core-utils' + +export class YoutubeDlUpdateScheduler extends AbstractScheduler { + + private static instance: AbstractScheduler + + protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.youtubeDLUpdate + + private constructor () { + super() + } + + async execute () { + 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' + + request.get(url, { followRedirect: false }, (err, res) => { + if (err) { + logger.error('Cannot update youtube-dl.', { err }) + return + } + + if (res.statusCode !== 302) { + logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', res.statusCode) + return + } + + const url = res.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', res => { + if (res.statusCode !== 200) { + logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', res.statusCode) + return + } + + downloadFile.pipe(createWriteStream(bin, { mode: 493 })) + }) + + downloadFile.on('error', err => logger.error('youtube-dl update error.', { err })) + + 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 + } + + logger.info('youtube-dl updated to version %s.', newVersion) + }) + }) + + }) + } + + static get Instance () { + return this.instance || (this.instance = new this()) + } +} -- cgit v1.2.3