]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-import-videos.ts
Use a class for youtube-dl
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-import-videos.ts
1 import { registerTSPaths } from '../helpers/register-ts-paths'
2 registerTSPaths()
3
4 import * as program from 'commander'
5 import { accessSync, constants } from 'fs'
6 import { remove } from 'fs-extra'
7 import { truncate } from 'lodash'
8 import { join } from 'path'
9 import * as prompt from 'prompt'
10 import { promisify } from 'util'
11 import { advancedVideosSearch, getClient, getVideoCategories, login, uploadVideo } from '../../shared/extra-utils/index'
12 import { sha256 } from '../helpers/core-utils'
13 import { doRequestAndSaveToFile } from '../helpers/requests'
14 import { CONSTRAINTS_FIELDS } from '../initializers/constants'
15 import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getLogger, getServerCredentials } from './cli'
16 import { YoutubeDL } from '@server/helpers/youtube-dl'
17
18 type UserInfo = {
19 username: string
20 password: string
21 }
22
23 const processOptions = {
24 maxBuffer: Infinity
25 }
26
27 let command = program
28 .name('import-videos')
29
30 command = buildCommonVideoOptions(command)
31
32 command
33 .option('-u, --url <url>', 'Server url')
34 .option('-U, --username <username>', 'Username')
35 .option('-p, --password <token>', 'Password')
36 .option('--target-url <targetUrl>', 'Video target URL')
37 .option('--since <since>', 'Publication date (inclusive) since which the videos can be imported (YYYY-MM-DD)', parseDate)
38 .option('--until <until>', 'Publication date (inclusive) until which the videos can be imported (YYYY-MM-DD)', parseDate)
39 .option('--first <first>', 'Process first n elements of returned playlist')
40 .option('--last <last>', 'Process last n elements of returned playlist')
41 .option('--wait-interval <waitInterval>', 'Duration between two video imports (in seconds)', convertIntoMs)
42 .option('-T, --tmpdir <tmpdir>', 'Working directory', __dirname)
43 .usage("[global options] [ -- youtube-dl options]")
44 .parse(process.argv)
45
46 const options = command.opts()
47
48 const log = getLogger(options.verbose)
49
50 getServerCredentials(command)
51 .then(({ url, username, password }) => {
52 if (!options.targetUrl) {
53 exitError('--target-url field is required.')
54 }
55
56 try {
57 accessSync(options.tmpdir, constants.R_OK | constants.W_OK)
58 } catch (e) {
59 exitError('--tmpdir %s: directory does not exist or is not accessible', options.tmpdir)
60 }
61
62 url = normalizeTargetUrl(url)
63 options.targetUrl = normalizeTargetUrl(options.targetUrl)
64
65 const user = { username, password }
66
67 run(url, user)
68 .catch(err => exitError(err))
69 })
70 .catch(err => console.error(err))
71
72 async function run (url: string, user: UserInfo) {
73 if (!user.password) {
74 user.password = await promptPassword()
75 }
76
77 const youtubeDLBinary = await YoutubeDL.safeGetYoutubeDL()
78
79 let info = await getYoutubeDLInfo(youtubeDLBinary, options.targetUrl, command.args)
80
81 if (!Array.isArray(info)) info = [ info ]
82
83 // Try to fix youtube channels upload
84 const uploadsObject = info.find(i => !i.ie_key && !i.duration && i.title === 'Uploads')
85
86 if (uploadsObject) {
87 console.log('Fixing URL to %s.', uploadsObject.url)
88
89 info = await getYoutubeDLInfo(youtubeDLBinary, uploadsObject.url, command.args)
90 }
91
92 let infoArray: any[]
93
94 infoArray = [].concat(info)
95 if (options.first) {
96 infoArray = infoArray.slice(0, options.first)
97 } else if (options.last) {
98 infoArray = infoArray.slice(-options.last)
99 }
100 // Normalize utf8 fields
101 infoArray = infoArray.map(i => normalizeObject(i))
102
103 log.info('Will download and upload %d videos.\n', infoArray.length)
104
105 for (const [ index, info ] of infoArray.entries()) {
106 try {
107 if (index > 0 && options.waitInterval) {
108 log.info("Wait for %d seconds before continuing.", options.waitInterval / 1000)
109 await new Promise(res => setTimeout(res, options.waitInterval))
110 }
111 await processVideo({
112 cwd: options.tmpdir,
113 url,
114 user,
115 youtubeInfo: info
116 })
117 } catch (err) {
118 console.error('Cannot process video.', { info, url, err })
119 }
120 }
121
122 log.info('Video/s for user %s imported: %s', user.username, options.targetUrl)
123 process.exit(0)
124 }
125
126 async function processVideo (parameters: {
127 cwd: string
128 url: string
129 user: { username: string, password: string }
130 youtubeInfo: any
131 }) {
132 const { youtubeInfo, cwd, url, user } = parameters
133 const youtubeDL = new YoutubeDL('', [])
134
135 log.debug('Fetching object.', youtubeInfo)
136
137 const videoInfo = await fetchObject(youtubeInfo)
138 log.debug('Fetched object.', videoInfo)
139
140 const originallyPublishedAt = youtubeDL.buildOriginallyPublishedAt(videoInfo)
141 if (options.since && originallyPublishedAt && originallyPublishedAt.getTime() < options.since.getTime()) {
142 log.info('Video "%s" has been published before "%s", don\'t upload it.\n',
143 videoInfo.title, formatDate(options.since))
144 return
145 }
146 if (options.until && originallyPublishedAt && originallyPublishedAt.getTime() > options.until.getTime()) {
147 log.info('Video "%s" has been published after "%s", don\'t upload it.\n',
148 videoInfo.title, formatDate(options.until))
149 return
150 }
151
152 const result = await advancedVideosSearch(url, { search: videoInfo.title, sort: '-match', searchTarget: 'local' })
153
154 log.info('############################################################\n')
155
156 if (result.body.data.find(v => v.name === videoInfo.title)) {
157 log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title)
158 return
159 }
160
161 const path = join(cwd, sha256(videoInfo.url) + '.mp4')
162
163 log.info('Downloading video "%s"...', videoInfo.title)
164
165 const youtubeDLOptions = [ '-f', youtubeDL.getYoutubeDLVideoFormat(), ...command.args, '-o', path ]
166 try {
167 const youtubeDLBinary = await YoutubeDL.safeGetYoutubeDL()
168 const youtubeDLExec = promisify(youtubeDLBinary.exec).bind(youtubeDLBinary)
169 const output = await youtubeDLExec(videoInfo.url, youtubeDLOptions, processOptions)
170 log.info(output.join('\n'))
171 await uploadVideoOnPeerTube({
172 youtubeDL,
173 cwd,
174 url,
175 user,
176 videoInfo: normalizeObject(videoInfo),
177 videoPath: path
178 })
179 } catch (err) {
180 log.error(err.message)
181 }
182 }
183
184 async function uploadVideoOnPeerTube (parameters: {
185 youtubeDL: YoutubeDL
186 videoInfo: any
187 videoPath: string
188 cwd: string
189 url: string
190 user: { username: string, password: string }
191 }) {
192 const { youtubeDL, videoInfo, videoPath, cwd, url, user } = parameters
193
194 const category = await getCategory(videoInfo.categories, url)
195 const licence = getLicence(videoInfo.license)
196 let tags = []
197 if (Array.isArray(videoInfo.tags)) {
198 tags = videoInfo.tags
199 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
200 .map(t => t.normalize())
201 .slice(0, 5)
202 }
203
204 let thumbnailfile
205 if (videoInfo.thumbnail) {
206 thumbnailfile = join(cwd, sha256(videoInfo.thumbnail) + '.jpg')
207
208 await doRequestAndSaveToFile(videoInfo.thumbnail, thumbnailfile)
209 }
210
211 const originallyPublishedAt = youtubeDL.buildOriginallyPublishedAt(videoInfo)
212
213 const defaultAttributes = {
214 name: truncate(videoInfo.title, {
215 length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
216 separator: /,? +/,
217 omission: ' […]'
218 }),
219 category,
220 licence,
221 nsfw: isNSFW(videoInfo),
222 description: videoInfo.description,
223 tags
224 }
225
226 const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes)
227
228 Object.assign(videoAttributes, {
229 originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null,
230 thumbnailfile,
231 previewfile: thumbnailfile,
232 fixture: videoPath
233 })
234
235 log.info('\nUploading on PeerTube video "%s".', videoAttributes.name)
236
237 let accessToken = await getAccessTokenOrDie(url, user)
238
239 try {
240 await uploadVideo(url, accessToken, videoAttributes)
241 } catch (err) {
242 if (err.message.indexOf('401') !== -1) {
243 log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.')
244
245 accessToken = await getAccessTokenOrDie(url, user)
246
247 await uploadVideo(url, accessToken, videoAttributes)
248 } else {
249 exitError(err.message)
250 }
251 }
252
253 await remove(videoPath)
254 if (thumbnailfile) await remove(thumbnailfile)
255
256 log.warn('Uploaded video "%s"!\n', videoAttributes.name)
257 }
258
259 /* ---------------------------------------------------------- */
260
261 async function getCategory (categories: string[], url: string) {
262 if (!categories) return undefined
263
264 const categoryString = categories[0]
265
266 if (categoryString === 'News & Politics') return 11
267
268 const res = await getVideoCategories(url)
269 const categoriesServer = res.body
270
271 for (const key of Object.keys(categoriesServer)) {
272 const categoryServer = categoriesServer[key]
273 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
274 }
275
276 return undefined
277 }
278
279 function getLicence (licence: string) {
280 if (!licence) return undefined
281
282 if (licence.includes('Creative Commons Attribution licence')) return 1
283
284 return undefined
285 }
286
287 function normalizeObject (obj: any) {
288 const newObj: any = {}
289
290 for (const key of Object.keys(obj)) {
291 // Deprecated key
292 if (key === 'resolution') continue
293
294 const value = obj[key]
295
296 if (typeof value === 'string') {
297 newObj[key] = value.normalize()
298 } else {
299 newObj[key] = value
300 }
301 }
302
303 return newObj
304 }
305
306 function fetchObject (info: any) {
307 const url = buildUrl(info)
308
309 return new Promise<any>(async (res, rej) => {
310 const youtubeDL = await YoutubeDL.safeGetYoutubeDL()
311 youtubeDL.getInfo(url, undefined, processOptions, (err, videoInfo) => {
312 if (err) return rej(err)
313
314 const videoInfoWithUrl = Object.assign(videoInfo, { url })
315 return res(normalizeObject(videoInfoWithUrl))
316 })
317 })
318 }
319
320 function buildUrl (info: any) {
321 const webpageUrl = info.webpage_url as string
322 if (webpageUrl?.match(/^https?:\/\//)) return webpageUrl
323
324 const url = info.url as string
325 if (url?.match(/^https?:\/\//)) return url
326
327 // It seems youtube-dl does not return the video url
328 return 'https://www.youtube.com/watch?v=' + info.id
329 }
330
331 function isNSFW (info: any) {
332 return info.age_limit && info.age_limit >= 16
333 }
334
335 function normalizeTargetUrl (url: string) {
336 let normalizedUrl = url.replace(/\/+$/, '')
337
338 if (!normalizedUrl.startsWith('http://') && !normalizedUrl.startsWith('https://')) {
339 normalizedUrl = 'https://' + normalizedUrl
340 }
341
342 return normalizedUrl
343 }
344
345 async function promptPassword () {
346 return new Promise<string>((res, rej) => {
347 prompt.start()
348 const schema = {
349 properties: {
350 password: {
351 hidden: true,
352 required: true
353 }
354 }
355 }
356 prompt.get(schema, function (err, result) {
357 if (err) {
358 return rej(err)
359 }
360 return res(result.password)
361 })
362 })
363 }
364
365 async function getAccessTokenOrDie (url: string, user: UserInfo) {
366 const resClient = await getClient(url)
367 const client = {
368 id: resClient.body.client_id,
369 secret: resClient.body.client_secret
370 }
371
372 try {
373 const res = await login(url, client, user)
374 return res.body.access_token
375 } catch (err) {
376 exitError('Cannot authenticate. Please check your username/password.')
377 }
378 }
379
380 function parseDate (dateAsStr: string): Date {
381 if (!/\d{4}-\d{2}-\d{2}/.test(dateAsStr)) {
382 exitError(`Invalid date passed: ${dateAsStr}. Expected format: YYYY-MM-DD. See help for usage.`)
383 }
384 const date = new Date(dateAsStr)
385 date.setHours(0, 0, 0)
386 if (isNaN(date.getTime())) {
387 exitError(`Invalid date passed: ${dateAsStr}. See help for usage.`)
388 }
389 return date
390 }
391
392 function formatDate (date: Date): string {
393 return date.toISOString().split('T')[0]
394 }
395
396 function convertIntoMs (secondsAsStr: string): number {
397 const seconds = parseInt(secondsAsStr, 10)
398 if (seconds <= 0) {
399 exitError(`Invalid duration passed: ${seconds}. Expected duration to be strictly positive and in seconds`)
400 }
401 return Math.round(seconds * 1000)
402 }
403
404 function exitError (message: string, ...meta: any[]) {
405 // use console.error instead of log.error here
406 console.error(message, ...meta)
407 process.exit(-1)
408 }
409
410 function getYoutubeDLInfo (youtubeDL: any, url: string, args: string[]) {
411 return new Promise<any>((res, rej) => {
412 const options = [ '-j', '--flat-playlist', '--playlist-reverse', ...args ]
413
414 youtubeDL.getInfo(url, options, processOptions, async (err, info) => {
415 if (err) return rej(err)
416
417 return res(info)
418 })
419 })
420 }