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