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