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