aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/youtube-dl.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-09-20 16:24:31 +0200
committerGitHub <noreply@github.com>2018-09-20 16:24:31 +0200
commit0491173a61aed66205c017e0d7e0503ea316c144 (patch)
treece6621597505f9518cfdf0981977d097c63f9fad /server/helpers/youtube-dl.ts
parent8704acf49efc770d73bf07c10468ed8c74d28a83 (diff)
parent6247b2057b792cea155a1abd9788c363ae7d2cc2 (diff)
downloadPeerTube-0491173a61aed66205c017e0d7e0503ea316c144.tar.gz
PeerTube-0491173a61aed66205c017e0d7e0503ea316c144.tar.zst
PeerTube-0491173a61aed66205c017e0d7e0503ea316c144.zip
Merge branch 'develop' into cli-wrapper
Diffstat (limited to 'server/helpers/youtube-dl.ts')
-rw-r--r--server/helpers/youtube-dl.ts69
1 files changed, 66 insertions, 3 deletions
diff --git a/server/helpers/youtube-dl.ts b/server/helpers/youtube-dl.ts
index 8b2bc1782..25e719cc3 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,6 +52,64 @@ 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
51async function safeGetYoutubeDL () { 113async function safeGetYoutubeDL () {
52 let youtubeDL 114 let youtubeDL
53 115
@@ -55,7 +117,7 @@ async function safeGetYoutubeDL () {
55 youtubeDL = require('youtube-dl') 117 youtubeDL = require('youtube-dl')
56 } catch (e) { 118 } catch (e) {
57 // Download binary 119 // Download binary
58 await YoutubeDlUpdateScheduler.Instance.execute() 120 await updateYoutubeDLBinary()
59 youtubeDL = require('youtube-dl') 121 youtubeDL = require('youtube-dl')
60 } 122 }
61 123
@@ -65,6 +127,7 @@ async function safeGetYoutubeDL () {
65// --------------------------------------------------------------------------- 127// ---------------------------------------------------------------------------
66 128
67export { 129export {
130 updateYoutubeDLBinary,
68 downloadYoutubeDLVideo, 131 downloadYoutubeDLVideo,
69 getYoutubeDLInfo, 132 getYoutubeDLInfo,
70 safeGetYoutubeDL 133 safeGetYoutubeDL