Add player mode in watch/embed urls
[oweals/peertube.git] / server / helpers / youtube-dl.ts
1 import { truncate } from 'lodash'
2 import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers'
3 import { logger } from './logger'
4 import { generateVideoImportTmpPath } from './utils'
5 import { join } from 'path'
6 import { root } from './core-utils'
7 import { ensureDir, writeFile, remove } from 'fs-extra'
8 import * as request from 'request'
9 import { createWriteStream } from 'fs'
10
11 export type YoutubeDLInfo = {
12   name?: string
13   description?: string
14   category?: number
15   licence?: number
16   nsfw?: boolean
17   tags?: string[]
18   thumbnailUrl?: string
19 }
20
21 const processOptions = {
22   maxBuffer: 1024 * 1024 * 10 // 10MB
23 }
24
25 function getYoutubeDLInfo (url: string, opts?: string[]): Promise<YoutubeDLInfo> {
26   return new Promise<YoutubeDLInfo>(async (res, rej) => {
27     const args = opts || [ '-j', '--flat-playlist' ]
28
29     const youtubeDL = await safeGetYoutubeDL()
30     youtubeDL.getInfo(url, args, processOptions, (err, info) => {
31       if (err) return rej(err)
32       if (info.is_live === true) return rej(new Error('Cannot download a live streaming.'))
33
34       const obj = buildVideoInfo(normalizeObject(info))
35       if (obj.name && obj.name.length < CONSTRAINTS_FIELDS.VIDEOS.NAME.min) obj.name += ' video'
36
37       return res(obj)
38     })
39   })
40 }
41
42 function downloadYoutubeDLVideo (url: string, timeout: number) {
43   const path = generateVideoImportTmpPath(url)
44   let timer
45
46   logger.info('Importing youtubeDL video %s', url)
47
48   const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ]
49
50   return new Promise<string>(async (res, rej) => {
51     const youtubeDL = await safeGetYoutubeDL()
52     youtubeDL.exec(url, options, processOptions, err => {
53       clearTimeout(timer)
54
55       if (err) {
56         remove(path)
57           .catch(err => logger.error('Cannot delete path on YoutubeDL error.', { err }))
58
59         return rej(err)
60       }
61
62       return res(path)
63     })
64
65     timer = setTimeout(async () => {
66       await remove(path)
67
68       return rej(new Error('YoutubeDL download timeout.'))
69     }, timeout)
70   })
71 }
72
73 // Thanks: https://github.com/przemyslawpluta/node-youtube-dl/blob/master/lib/downloader.js
74 // We rewrote it to avoid sync calls
75 async function updateYoutubeDLBinary () {
76   logger.info('Updating youtubeDL binary.')
77
78   const binDirectory = join(root(), 'node_modules', 'youtube-dl', 'bin')
79   const bin = join(binDirectory, 'youtube-dl')
80   const detailsPath = join(binDirectory, 'details')
81   const url = 'https://yt-dl.org/downloads/latest/youtube-dl'
82
83   await ensureDir(binDirectory)
84
85   return new Promise(res => {
86     request.get(url, { followRedirect: false }, (err, result) => {
87       if (err) {
88         logger.error('Cannot update youtube-dl.', { err })
89         return res()
90       }
91
92       if (result.statusCode !== 302) {
93         logger.error('youtube-dl update error: did not get redirect for the latest version link. Status %d', result.statusCode)
94         return res()
95       }
96
97       const url = result.headers.location
98       const downloadFile = request.get(url)
99       const newVersion = /yt-dl\.org\/downloads\/(\d{4}\.\d\d\.\d\d(\.\d)?)\/youtube-dl/.exec(url)[ 1 ]
100
101       downloadFile.on('response', result => {
102         if (result.statusCode !== 200) {
103           logger.error('Cannot update youtube-dl: new version response is not 200, it\'s %d.', result.statusCode)
104           return res()
105         }
106
107         downloadFile.pipe(createWriteStream(bin, { mode: 493 }))
108       })
109
110       downloadFile.on('error', err => {
111         logger.error('youtube-dl update error.', { err })
112         return res()
113       })
114
115       downloadFile.on('end', () => {
116         const details = JSON.stringify({ version: newVersion, path: bin, exec: 'youtube-dl' })
117         writeFile(detailsPath, details, { encoding: 'utf8' }, err => {
118           if (err) {
119             logger.error('youtube-dl update error: cannot write details.', { err })
120             return res()
121           }
122
123           logger.info('youtube-dl updated to version %s.', newVersion)
124           return res()
125         })
126       })
127     })
128   })
129 }
130
131 async function safeGetYoutubeDL () {
132   let youtubeDL
133
134   try {
135     youtubeDL = require('youtube-dl')
136   } catch (e) {
137     // Download binary
138     await updateYoutubeDLBinary()
139     youtubeDL = require('youtube-dl')
140   }
141
142   return youtubeDL
143 }
144
145 // ---------------------------------------------------------------------------
146
147 export {
148   updateYoutubeDLBinary,
149   downloadYoutubeDLVideo,
150   getYoutubeDLInfo,
151   safeGetYoutubeDL
152 }
153
154 // ---------------------------------------------------------------------------
155
156 function normalizeObject (obj: any) {
157   const newObj: any = {}
158
159   for (const key of Object.keys(obj)) {
160     // Deprecated key
161     if (key === 'resolution') continue
162
163     const value = obj[key]
164
165     if (typeof value === 'string') {
166       newObj[key] = value.normalize()
167     } else {
168       newObj[key] = value
169     }
170   }
171
172   return newObj
173 }
174
175 function buildVideoInfo (obj: any) {
176   return {
177     name: titleTruncation(obj.title),
178     description: descriptionTruncation(obj.description),
179     category: getCategory(obj.categories),
180     licence: getLicence(obj.license),
181     nsfw: isNSFW(obj),
182     tags: getTags(obj.tags),
183     thumbnailUrl: obj.thumbnail || undefined
184   }
185 }
186
187 function titleTruncation (title: string) {
188   return truncate(title, {
189     'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
190     'separator': /,? +/,
191     'omission': ' […]'
192   })
193 }
194
195 function descriptionTruncation (description: string) {
196   if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
197
198   return truncate(description, {
199     'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
200     'separator': /,? +/,
201     'omission': ' […]'
202   })
203 }
204
205 function isNSFW (info: any) {
206   return info.age_limit && info.age_limit >= 16
207 }
208
209 function getTags (tags: any) {
210   if (Array.isArray(tags) === false) return []
211
212   return tags
213     .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
214     .map(t => t.normalize())
215     .slice(0, 5)
216 }
217
218 function getLicence (licence: string) {
219   if (!licence) return undefined
220
221   if (licence.indexOf('Creative Commons Attribution') !== -1) return 1
222
223   return undefined
224 }
225
226 function getCategory (categories: string[]) {
227   if (!categories) return undefined
228
229   const categoryString = categories[0]
230   if (!categoryString || typeof categoryString !== 'string') return undefined
231
232   if (categoryString === 'News & Politics') return 11
233
234   for (const key of Object.keys(VIDEO_CATEGORIES)) {
235     const category = VIDEO_CATEGORIES[key]
236     if (categoryString.toLowerCase() === category.toLowerCase()) return parseInt(key, 10)
237   }
238
239   return undefined
240 }