]>
Commit | Line | Data |
---|---|---|
27d56b54 C |
1 | // FIXME: https://github.com/nodejs/node/pull/16853 |
2 | require('tls').DEFAULT_ECDH_CURVE = 'auto' | |
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' |
62689b94 | 11 | import { remove } from 'fs-extra' |
fa27f076 | 12 | import { sha256 } from '../helpers/core-utils' |
e8a739e8 | 13 | import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl' |
8d2be0ed | 14 | import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getServerCredentials } from './cli' |
8704acf4 | 15 | |
1a12f66d C |
16 | type UserInfo = { |
17 | username: string | |
18 | password: string | |
19 | } | |
8704acf4 RK |
20 | |
21 | const processOptions = { | |
22 | cwd: __dirname, | |
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') | |
61b3e146 | 35 | .option('-t, --target-url <targetUrl>', 'Video target URL') |
61b3e146 | 36 | .option('-v, --verbose', 'Verbose mode') |
a7fea183 C |
37 | .parse(process.argv) |
38 | ||
8d2be0ed C |
39 | getServerCredentials(command) |
40 | .then(({ url, username, password }) => { | |
41 | if (!program[ 'targetUrl' ]) { | |
42 | console.error('--targetUrl field is required.') | |
e8a739e8 | 43 | |
8d2be0ed C |
44 | process.exit(-1) |
45 | } | |
066fc8ba | 46 | |
8d2be0ed C |
47 | removeEndSlashes(url) |
48 | removeEndSlashes(program[ 'targetUrl' ]) | |
ab4dbe36 | 49 | |
8d2be0ed | 50 | const user = { username, password } |
8a2db2e8 | 51 | |
8d2be0ed C |
52 | run(url, user) |
53 | .catch(err => { | |
54 | console.error(err) | |
55 | process.exit(-1) | |
56 | }) | |
57 | }) | |
a7fea183 | 58 | |
1a12f66d | 59 | async function run (url: string, user: UserInfo) { |
e2b9d0ca JL |
60 | if (!user.password) { |
61 | user.password = await promptPassword() | |
066fc8ba | 62 | } |
8a2db2e8 | 63 | |
8704acf4 RK |
64 | const youtubeDL = await safeGetYoutubeDL() |
65 | ||
5f26c735 | 66 | const options = [ '-j', '--flat-playlist', '--playlist-reverse' ] |
2b4dd7e2 | 67 | youtubeDL.getInfo(program[ 'targetUrl' ], options, processOptions, async (err, info) => { |
f5b611f9 | 68 | if (err) { |
5c5638a0 O |
69 | console.log(err.message) |
70 | process.exit(1) | |
f5b611f9 | 71 | } |
a7fea183 | 72 | |
61b3e146 | 73 | let infoArray: any[] |
a7fea183 | 74 | |
61b3e146 C |
75 | // Normalize utf8 fields |
76 | if (Array.isArray(info) === true) { | |
77 | infoArray = info.map(i => normalizeObject(i)) | |
78 | } else { | |
79 | infoArray = [ normalizeObject(info) ] | |
80 | } | |
81 | console.log('Will download and upload %d videos.\n', infoArray.length) | |
a7fea183 | 82 | |
61b3e146 | 83 | for (const info of infoArray) { |
1a12f66d C |
84 | await processVideo({ |
85 | cwd: processOptions.cwd, | |
86 | url, | |
87 | user, | |
88 | youtubeInfo: info | |
89 | }) | |
a7fea183 C |
90 | } |
91 | ||
2b4dd7e2 | 92 | console.log('Video/s for user %s imported: %s', program[ 'username' ], program[ 'targetUrl' ]) |
a7fea183 C |
93 | process.exit(0) |
94 | }) | |
95 | } | |
96 | ||
1a12f66d C |
97 | function processVideo (parameters: { |
98 | cwd: string, | |
99 | url: string, | |
100 | user: { username: string, password: string }, | |
101 | youtubeInfo: any | |
102 | }) { | |
103 | const { youtubeInfo, cwd, url, user } = parameters | |
104 | ||
a7fea183 | 105 | return new Promise(async res => { |
1a12f66d | 106 | if (program[ 'verbose' ]) console.log('Fetching object.', youtubeInfo) |
61b3e146 | 107 | |
1a12f66d | 108 | const videoInfo = await fetchObject(youtubeInfo) |
2b4dd7e2 | 109 | if (program[ 'verbose' ]) console.log('Fetched object.', videoInfo) |
61b3e146 | 110 | |
8704acf4 | 111 | const result = await searchVideoWithSort(url, videoInfo.title, '-match') |
e7872038 C |
112 | |
113 | console.log('############################################################\n') | |
114 | ||
61b3e146 C |
115 | if (result.body.data.find(v => v.name === videoInfo.title)) { |
116 | console.log('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title) | |
a7fea183 C |
117 | return res() |
118 | } | |
119 | ||
fa27f076 | 120 | const path = join(cwd, sha256(videoInfo.url) + '.mp4') |
a7fea183 | 121 | |
61b3e146 | 122 | console.log('Downloading video "%s"...', videoInfo.title) |
a7fea183 | 123 | |
61b3e146 | 124 | const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] |
f97d2992 | 125 | try { |
8704acf4 | 126 | const youtubeDL = await safeGetYoutubeDL() |
f97d2992 | 127 | youtubeDL.exec(videoInfo.url, options, processOptions, async (err, output) => { |
128 | if (err) { | |
129 | console.error(err) | |
130 | return res() | |
131 | } | |
132 | ||
133 | console.log(output.join('\n')) | |
1a12f66d C |
134 | await uploadVideoOnPeerTube({ |
135 | cwd, | |
136 | url, | |
137 | user, | |
138 | videoInfo: normalizeObject(videoInfo), | |
139 | videoPath: path | |
140 | }) | |
f97d2992 | 141 | return res() |
142 | }) | |
143 | } catch (err) { | |
144 | console.log(err.message) | |
61b3e146 | 145 | return res() |
f97d2992 | 146 | } |
a7fea183 C |
147 | }) |
148 | } | |
149 | ||
1a12f66d C |
150 | async function uploadVideoOnPeerTube (parameters: { |
151 | videoInfo: any, | |
152 | videoPath: string, | |
153 | cwd: string, | |
154 | url: string, | |
155 | user: { username: string; password: string } | |
156 | }) { | |
157 | const { videoInfo, videoPath, cwd, url, user } = parameters | |
158 | ||
8704acf4 | 159 | const category = await getCategory(videoInfo.categories, url) |
a7fea183 | 160 | const licence = getLicence(videoInfo.license) |
34cbef8c C |
161 | let tags = [] |
162 | if (Array.isArray(videoInfo.tags)) { | |
02988fdc | 163 | tags = videoInfo.tags |
2b4dd7e2 C |
164 | .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) |
165 | .map(t => t.normalize()) | |
166 | .slice(0, 5) | |
34cbef8c | 167 | } |
a7fea183 | 168 | |
1d791a26 C |
169 | let thumbnailfile |
170 | if (videoInfo.thumbnail) { | |
fa27f076 | 171 | thumbnailfile = join(cwd, sha256(videoInfo.thumbnail) + '.jpg') |
1d791a26 C |
172 | |
173 | await doRequestAndSaveToFile({ | |
174 | method: 'GET', | |
175 | uri: videoInfo.thumbnail | |
176 | }, thumbnailfile) | |
177 | } | |
178 | ||
c74c9be9 | 179 | const originallyPublishedAt = buildOriginallyPublishedAt(videoInfo) |
84929846 | 180 | |
1205823f | 181 | const defaultAttributes = { |
45b8a42c RK |
182 | name: truncate(videoInfo.title, { |
183 | 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max, | |
184 | 'separator': /,? +/, | |
185 | 'omission': ' […]' | |
186 | }), | |
a7fea183 C |
187 | category, |
188 | licence, | |
a41e183c | 189 | nsfw: isNSFW(videoInfo), |
1205823f C |
190 | description: videoInfo.description, |
191 | tags | |
a7fea183 C |
192 | } |
193 | ||
1205823f C |
194 | const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes) |
195 | ||
196 | Object.assign(videoAttributes, { | |
197 | originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, | |
198 | thumbnailfile, | |
199 | previewfile: thumbnailfile, | |
200 | fixture: videoPath | |
201 | }) | |
1a12f66d | 202 | |
a7fea183 | 203 | console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) |
1a12f66d C |
204 | |
205 | let accessToken = await getAccessTokenOrDie(url, user) | |
206 | ||
71578f31 | 207 | try { |
8704acf4 | 208 | await uploadVideo(url, accessToken, videoAttributes) |
61b3e146 | 209 | } catch (err) { |
b6fe1f98 | 210 | if (err.message.indexOf('401') !== -1) { |
61b3e146 C |
211 | console.log('Got 401 Unauthorized, token may have expired, renewing token and retry.') |
212 | ||
1a12f66d | 213 | accessToken = await getAccessTokenOrDie(url, user) |
61b3e146 | 214 | |
8704acf4 | 215 | await uploadVideo(url, accessToken, videoAttributes) |
61b3e146 | 216 | } else { |
5c5638a0 O |
217 | console.log(err.message) |
218 | process.exit(1) | |
71578f31 L |
219 | } |
220 | } | |
1d791a26 | 221 | |
62689b94 C |
222 | await remove(videoPath) |
223 | if (thumbnailfile) await remove(thumbnailfile) | |
1d791a26 | 224 | |
a7fea183 C |
225 | console.log('Uploaded video "%s"!\n', videoAttributes.name) |
226 | } | |
227 | ||
1a12f66d C |
228 | /* ---------------------------------------------------------- */ |
229 | ||
8704acf4 | 230 | async function getCategory (categories: string[], url: string) { |
61b3e146 C |
231 | if (!categories) return undefined |
232 | ||
2b4dd7e2 | 233 | const categoryString = categories[ 0 ] |
a7fea183 C |
234 | |
235 | if (categoryString === 'News & Politics') return 11 | |
236 | ||
8704acf4 | 237 | const res = await getVideoCategories(url) |
a7fea183 C |
238 | const categoriesServer = res.body |
239 | ||
240 | for (const key of Object.keys(categoriesServer)) { | |
2b4dd7e2 | 241 | const categoryServer = categoriesServer[ key ] |
a7fea183 C |
242 | if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10) |
243 | } | |
244 | ||
245 | return undefined | |
246 | } | |
247 | ||
248 | function getLicence (licence: string) { | |
61b3e146 C |
249 | if (!licence) return undefined |
250 | ||
a7fea183 C |
251 | if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1 |
252 | ||
253 | return undefined | |
254 | } | |
e7872038 C |
255 | |
256 | function normalizeObject (obj: any) { | |
257 | const newObj: any = {} | |
258 | ||
259 | for (const key of Object.keys(obj)) { | |
260 | // Deprecated key | |
261 | if (key === 'resolution') continue | |
262 | ||
2b4dd7e2 | 263 | const value = obj[ key ] |
e7872038 C |
264 | |
265 | if (typeof value === 'string') { | |
2b4dd7e2 | 266 | newObj[ key ] = value.normalize() |
e7872038 | 267 | } else { |
2b4dd7e2 | 268 | newObj[ key ] = value |
e7872038 C |
269 | } |
270 | } | |
271 | ||
272 | return newObj | |
273 | } | |
61b3e146 C |
274 | |
275 | function fetchObject (info: any) { | |
276 | const url = buildUrl(info) | |
277 | ||
278 | return new Promise<any>(async (res, rej) => { | |
8704acf4 | 279 | const youtubeDL = await safeGetYoutubeDL() |
61b3e146 C |
280 | youtubeDL.getInfo(url, undefined, processOptions, async (err, videoInfo) => { |
281 | if (err) return rej(err) | |
282 | ||
283 | const videoInfoWithUrl = Object.assign(videoInfo, { url }) | |
284 | return res(normalizeObject(videoInfoWithUrl)) | |
285 | }) | |
286 | }) | |
287 | } | |
288 | ||
289 | function buildUrl (info: any) { | |
a41e183c C |
290 | const webpageUrl = info.webpage_url as string |
291 | if (webpageUrl && webpageUrl.match(/^https?:\/\//)) return webpageUrl | |
292 | ||
61b3e146 | 293 | const url = info.url as string |
a41e183c | 294 | if (url && url.match(/^https?:\/\//)) return url |
61b3e146 C |
295 | |
296 | // It seems youtube-dl does not return the video url | |
297 | return 'https://www.youtube.com/watch?v=' + info.id | |
298 | } | |
a41e183c C |
299 | |
300 | function isNSFW (info: any) { | |
1a12f66d | 301 | return info.age_limit && info.age_limit >= 16 |
a41e183c | 302 | } |
ab4dbe36 H |
303 | |
304 | function removeEndSlashes (url: string) { | |
305 | while (url.endsWith('/')) { | |
306 | url.slice(0, -1) | |
307 | } | |
308 | } | |
1a12f66d C |
309 | |
310 | async function promptPassword () { | |
311 | return new Promise<string>((res, rej) => { | |
312 | prompt.start() | |
313 | const schema = { | |
314 | properties: { | |
315 | password: { | |
316 | hidden: true, | |
317 | required: true | |
318 | } | |
319 | } | |
320 | } | |
321 | prompt.get(schema, function (err, result) { | |
322 | if (err) { | |
323 | return rej(err) | |
324 | } | |
325 | return res(result.password) | |
326 | }) | |
327 | }) | |
328 | } | |
329 | ||
330 | async function getAccessTokenOrDie (url: string, user: UserInfo) { | |
331 | const resClient = await getClient(url) | |
332 | const client = { | |
333 | id: resClient.body.client_id, | |
334 | secret: resClient.body.client_secret | |
335 | } | |
336 | ||
337 | try { | |
338 | const res = await login(url, client, user) | |
339 | return res.body.access_token | |
340 | } catch (err) { | |
341 | console.error('Cannot authenticate. Please check your username/password.') | |
342 | process.exit(-1) | |
343 | } | |
344 | } |