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