aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/schedulers/youtube-dl-update-scheduler.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-19 16:24:24 +0200
committerChocobozzz <me@florianbigard.com>2018-09-19 16:24:24 +0200
commit606c946e74211c4123b16087288902226306198d (patch)
treec4dc467d6a4b571f5076915a734058e42d72b77d /server/lib/schedulers/youtube-dl-update-scheduler.ts
parentf9f899b9f803eb5159a67781f10649a0cf040677 (diff)
downloadPeerTube-606c946e74211c4123b16087288902226306198d.tar.gz
PeerTube-606c946e74211c4123b16087288902226306198d.tar.zst
PeerTube-606c946e74211c4123b16087288902226306198d.zip
Move youtubeDL upgrader in helpers/
Diffstat (limited to 'server/lib/schedulers/youtube-dl-update-scheduler.ts')
-rw-r--r--server/lib/schedulers/youtube-dl-update-scheduler.ts60
1 files changed, 4 insertions, 56 deletions
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 @@
1// Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js 1
2// We rewrote it to avoid sync calls
3 2
4import { AbstractScheduler } from './abstract-scheduler' 3import { AbstractScheduler } from './abstract-scheduler'
5import { SCHEDULER_INTERVALS_MS } from '../../initializers' 4import { SCHEDULER_INTERVALS_MS } from '../../initializers'
@@ -8,6 +7,7 @@ import * as request from 'request'
8import { createWriteStream, ensureDir, writeFile } from 'fs-extra' 7import { createWriteStream, ensureDir, writeFile } from 'fs-extra'
9import { join } from 'path' 8import { join } from 'path'
10import { root } from '../../helpers/core-utils' 9import { root } from '../../helpers/core-utils'
10import { updateYoutubeDLBinary } from '../../helpers/youtube-dl'
11 11
12export class YoutubeDlUpdateScheduler extends AbstractScheduler { 12export class YoutubeDlUpdateScheduler extends AbstractScheduler {
13 13
@@ -19,60 +19,8 @@ export class YoutubeDlUpdateScheduler extends AbstractScheduler {
19 super() 19 super()
20 } 20 }
21 21
22 async execute () { 22 execute () {
23 logger.info('Updating youtubeDL binary.') 23 return updateYoutubeDLBinary()
24
25 const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin')
26 const bin = join(binDirectory, 'youtube-dl')
27 const detailsPath = join(binDirectory, 'details')
28 const url = 'https://yt-dl.org/downloads/latest/youtube-dl'
29
30 await ensureDir(binDirectory)
31
32 return new Promise(res => {
33 request.get(url, { followRedirect: false }, (err, result) => {
34 if (err) {
35 logger.error('Cannot update youtube-dl.', { err })
36 return res()
37 }
38
39 if (result.statusCode !== 302) {
40 logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode)
41 return res()
42 }
43
44 const url = result.headers.location
45 const downloadFile = request.get(url)
46 const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ]
47
48 downloadFile.on('response', result => {
49 if (result.statusCode !== 200) {
50 logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode)
51 return res()
52 }
53
54 downloadFile.pipe(createWriteStream(bin, { mode: 493 }))
55 })
56
57 downloadFile.on('error', err => {
58 logger.error('youtube-dl update error.', { err })
59 return res()
60 })
61
62 downloadFile.on('end', () => {
63 const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' })
64 writeFile(detailsPath, details, { encoding: 'utf8' }, err => {
65 if (err) {
66 logger.error('youtube-dl update error: cannot write details.', { err })
67 return res()
68 }
69
70 logger.info('youtube-dl updated to version %s.', newVersion)
71 return res()
72 })
73 })
74 })
75 })
76 } 24 }
77 25
78 static get Instance () { 26 static get Instance () {