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