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