aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/initializers/constants.ts3
-rw-r--r--server/lib/schedulers/youtube-dl-update-scheduler.ts72
2 files changed, 74 insertions, 1 deletions
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
108let SCHEDULER_INTERVALS_MS = { 108let SCHEDULER_INTERVALS_MS = {
109 badActorFollow: 60000 * 60, // 1 hour 109 badActorFollow: 60000 * 60, // 1 hour
110 removeOldJobs: 60000 * 60, // 1 hour 110 removeOldJobs: 60000 * 60, // 1 hour
111 updateVideos: 60000 // 1 minute 111 updateVideos: 60000, // 1 minute
112 youtubeDLUpdate: 60000 * 60 * 24 // 1 day
112} 113}
113 114
114// --------------------------------------------------------------------------- 115// ---------------------------------------------------------------------------
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 @@
1// Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js
2// Use rewrote it to avoid sync calls
3
4import { AbstractScheduler } from './abstract-scheduler'
5import { SCHEDULER_INTERVALS_MS } from '../../initializers'
6import { logger } from '../../helpers/logger'
7import * as request from 'request'
8import { createWriteStream, writeFile } from 'fs'
9import { join } from 'path'
10import { root } from '../../helpers/core-utils'
11
12export class YoutubeDlUpdateScheduler extends AbstractScheduler {
13
14 private static instance: AbstractScheduler
15
16 protected schedulerIntervalMs = SCHEDULER_INTERVALS_MS.youtubeDLUpdate
17
18 private constructor () {
19 super()
20 }
21
22 async execute () {
23 const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin')
24 const bin = join(binDirectory, 'youtube-dl')
25 const detailsPath = join(binDirectory, 'details')
26 const url = 'https://yt-dl.org/downloads/latest/youtube-dl'
27
28 request.get(url, { followRedirect: false }, (err, res) => {
29 if (err) {
30 logger.error('Cannot update youtube-dl.', { err })
31 return
32 }
33
34 if (res.statusCode !== 302) {
35 logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', res.statusCode)
36 return
37 }
38
39 const url = res.headers.location
40 const downloadFile = request.get(url)
41 const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[1]
42
43 downloadFile.on('response', res => {
44 if (res.statusCode !== 200) {
45 logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', res.statusCode)
46 return
47 }
48
49 downloadFile.pipe(createWriteStream(bin, { mode: 493 }))
50 })
51
52 downloadFile.on('error', err => logger.error('youtube-dl update error.', { err }))
53
54 downloadFile.on('end', () => {
55 const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' })
56 writeFile(detailsPath, details, { encoding: 'utf8' }, err => {
57 if (err) {
58 logger.error('youtube-dl update error: cannot write details.', { err })
59 return
60 }
61
62 logger.info('youtube-dl updated to version %s.', newVersion)
63 })
64 })
65
66 })
67 }
68
69 static get Instance () {
70 return this.instance || (this.instance = new this())
71 }
72}