aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-02 16:02:51 +0200
committerChocobozzz <me@florianbigard.com>2018-08-06 11:19:16 +0200
commitce32426be941164d041526e4748b4c83cdc6bf1c (patch)
treec505043886991f63a09f4499c1d84659cff236a3
parentfbad87b0472f574409f7aa3ae7f8b54927d0cdd6 (diff)
downloadPeerTube-ce32426be941164d041526e4748b4c83cdc6bf1c.tar.gz
PeerTube-ce32426be941164d041526e4748b4c83cdc6bf1c.tar.zst
PeerTube-ce32426be941164d041526e4748b4c83cdc6bf1c.zip
Auto update youtube-dl
-rw-r--r--server.ts2
-rw-r--r--server/initializers/constants.ts3
-rw-r--r--server/lib/schedulers/youtube-dl-update-scheduler.ts72
3 files changed, 76 insertions, 1 deletions
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'
89import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler' 89import { BadActorFollowScheduler } from './server/lib/schedulers/bad-actor-follow-scheduler'
90import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler' 90import { RemoveOldJobsScheduler } from './server/lib/schedulers/remove-old-jobs-scheduler'
91import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler' 91import { UpdateVideosScheduler } from './server/lib/schedulers/update-videos-scheduler'
92import { YoutubeDlUpdateScheduler } from './server/lib/schedulers/youtube-dl-update-scheduler'
92 93
93// ----------- Command line ----------- 94// ----------- Command line -----------
94 95
@@ -186,6 +187,7 @@ async function startApplication () {
186 BadActorFollowScheduler.Instance.enable() 187 BadActorFollowScheduler.Instance.enable()
187 RemoveOldJobsScheduler.Instance.enable() 188 RemoveOldJobsScheduler.Instance.enable()
188 UpdateVideosScheduler.Instance.enable() 189 UpdateVideosScheduler.Instance.enable()
190 YoutubeDlUpdateScheduler.Instance.enable()
189 191
190 // Redis initialization 192 // Redis initialization
191 Redis.Instance.init() 193 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
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}