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