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