aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/helpers/youtube-dl.ts69
-rw-r--r--server/lib/schedulers/youtube-dl-update-scheduler.ts60
2 files changed, 70 insertions, 59 deletions
diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts
index 6738090f3..db2bddf78 100644
--- a/server/helpers/youtube-dl.ts
+++ b/server/helpers/youtube-dl.ts
@@ -2,7 +2,11 @@ import { truncate } from 'lodash'
2import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers' 2import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers'
3import { logger } from './logger' 3import { logger } from './logger'
4import { generateVideoTmpPath } from './utils' 4import { generateVideoTmpPath } from './utils'
5import { YoutubeDlUpdateScheduler } from '../lib/schedulers/youtube-dl-update-scheduler' 5import { join } from 'path'
6import { root } from './core-utils'
7import { ensureDir, writeFile } from 'fs-extra'
8import * as request from 'request'
9import { createWriteStream } from 'fs'
6 10
7export type YoutubeDLInfo = { 11export type YoutubeDLInfo = {
8 name?: string 12 name?: string
@@ -40,7 +44,7 @@ function downloadYoutubeDLVideo (url: string) {
40 44
41 return new Promise<string>(async (res, rej) => { 45 return new Promise<string>(async (res, rej) => {
42 const youtubeDL = await safeGetYoutubeDL() 46 const youtubeDL = await safeGetYoutubeDL()
43 youtubeDL.exec(url, options, async (err, output) => { 47 youtubeDL.exec(url, options, err => {
44 if (err) return rej(err) 48 if (err) return rej(err)
45 49
46 return res(path) 50 return res(path)
@@ -48,9 +52,68 @@ function downloadYoutubeDLVideo (url: string) {
48 }) 52 })
49} 53}
50 54
55// Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js
56// We rewrote it to avoid sync calls
57async function updateYoutubeDLBinary () {
58 logger.info('Updating youtubeDL binary.')
59
60 const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin')
61 const bin = join(binDirectory, 'youtube-dl')
62 const detailsPath = join(binDirectory, 'details')
63 const url = 'https://yt-dl.org/downloads/latest/youtube-dl'
64
65 await ensureDir(binDirectory)
66
67 return new Promise(res => {
68 request.get(url, { followRedirect: false }, (err, result) => {
69 if (err) {
70 logger.error('Cannot update youtube-dl.', { err })
71 return res()
72 }
73
74 if (result.statusCode !== 302) {
75 logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode)
76 return res()
77 }
78
79 const url = result.headers.location
80 const downloadFile = request.get(url)
81 const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ]
82
83 downloadFile.on('response', result => {
84 if (result.statusCode !== 200) {
85 logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode)
86 return res()
87 }
88
89 downloadFile.pipe(createWriteStream(bin, { mode: 493 }))
90 })
91
92 downloadFile.on('error', err => {
93 logger.error('youtube-dl update error.', { err })
94 return res()
95 })
96
97 downloadFile.on('end', () => {
98 const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' })
99 writeFile(detailsPath, details, { encoding: 'utf8' }, err => {
100 if (err) {
101 logger.error('youtube-dl update error: cannot write details.', { err })
102 return res()
103 }
104
105 logger.info('youtube-dl updated to version %s.', newVersion)
106 return res()
107 })
108 })
109 })
110 })
111}
112
51// --------------------------------------------------------------------------- 113// ---------------------------------------------------------------------------
52 114
53export { 115export {
116 updateYoutubeDLBinary,
54 downloadYoutubeDLVideo, 117 downloadYoutubeDLVideo,
55 getYoutubeDLInfo 118 getYoutubeDLInfo
56} 119}
@@ -64,7 +127,7 @@ async function safeGetYoutubeDL () {
64 youtubeDL = require('youtube-dl') 127 youtubeDL = require('youtube-dl')
65 } catch (e) { 128 } catch (e) {
66 // Download binary 129 // Download binary
67 await YoutubeDlUpdateScheduler.Instance.execute() 130 await updateYoutubeDLBinary()
68 youtubeDL = require('youtube-dl') 131 youtubeDL = require('youtube-dl')
69 } 132 }
70 133
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 () {