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