]>
Commit | Line | Data |
---|---|---|
1 | // FIXME: https://github.com/nodejs/node/pull/16853 | |
2 | require('tls').DEFAULT_ECDH_CURVE = 'auto' | |
3 | ||
4 | import * as program from 'commander' | |
5 | import { join } from 'path' | |
6 | import * as youtubeDL from 'youtube-dl' | |
7 | import { VideoPrivacy } from '../../shared/models/videos' | |
8 | import { unlinkPromise } from '../helpers/core-utils' | |
9 | import { doRequestAndSaveToFile } from '../helpers/requests' | |
10 | import { CONSTRAINTS_FIELDS } from '../initializers' | |
11 | import { getClient, getVideoCategories, login, searchVideo, uploadVideo } from '../tests/utils' | |
12 | import { truncate } from 'lodash' | |
13 | ||
14 | program | |
15 | .option('-u, --url <url>', 'Server url') | |
16 | .option('-U, --username <username>', 'Username') | |
17 | .option('-p, --password <token>', 'Password') | |
18 | .option('-t, --target-url <targetUrl>', 'Video target URL') | |
19 | .option('-l, --language <languageCode>', 'Language code') | |
20 | .option('-v, --verbose', 'Verbose mode') | |
21 | .parse(process.argv) | |
22 | ||
23 | if ( | |
24 | !program['url'] || | |
25 | !program['username'] || | |
26 | !program['password'] || | |
27 | !program['targetUrl'] | |
28 | ) { | |
29 | console.error('All arguments are required.') | |
30 | process.exit(-1) | |
31 | } | |
32 | ||
33 | run().catch(err => console.error(err)) | |
34 | ||
35 | let accessToken: string | |
36 | let client: { id: string, secret: string } | |
37 | ||
38 | const user = { | |
39 | username: program['username'], | |
40 | password: program['password'] | |
41 | } | |
42 | ||
43 | const processOptions = { | |
44 | cwd: __dirname, | |
45 | maxBuffer: Infinity | |
46 | } | |
47 | ||
48 | async function run () { | |
49 | const res = await getClient(program['url']) | |
50 | client = { | |
51 | id: res.body.client_id, | |
52 | secret: res.body.client_secret | |
53 | } | |
54 | ||
55 | const res2 = await login(program['url'], client, user) | |
56 | accessToken = res2.body.access_token | |
57 | ||
58 | const options = [ '-j', '--flat-playlist', '--playlist-reverse' ] | |
59 | youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => { | |
60 | if (err) { | |
61 | console.log(err.message) | |
62 | process.exit(1) | |
63 | } | |
64 | ||
65 | let infoArray: any[] | |
66 | ||
67 | // Normalize utf8 fields | |
68 | if (Array.isArray(info) === true) { | |
69 | infoArray = info.map(i => normalizeObject(i)) | |
70 | } else { | |
71 | infoArray = [ normalizeObject(info) ] | |
72 | } | |
73 | console.log('Will download and upload %d videos.\n', infoArray.length) | |
74 | ||
75 | for (const info of infoArray) { | |
76 | await processVideo(info, program['language']) | |
77 | } | |
78 | ||
79 | console.log('I\'m finished!') | |
80 | process.exit(0) | |
81 | }) | |
82 | } | |
83 | ||
84 | function processVideo (info: any, languageCode: number) { | |
85 | return new Promise(async res => { | |
86 | if (program['verbose']) console.log('Fetching object.', info) | |
87 | ||
88 | const videoInfo = await fetchObject(info) | |
89 | if (program['verbose']) console.log('Fetched object.', videoInfo) | |
90 | ||
91 | const result = await searchVideo(program['url'], videoInfo.title) | |
92 | ||
93 | console.log('############################################################\n') | |
94 | ||
95 | if (result.body.data.find(v => v.name === videoInfo.title)) { | |
96 | console.log('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title) | |
97 | return res() | |
98 | } | |
99 | ||
100 | const path = join(__dirname, new Date().getTime() + '.mp4') | |
101 | ||
102 | console.log('Downloading video "%s"...', videoInfo.title) | |
103 | ||
104 | const options = [ '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best', '-o', path ] | |
105 | try { | |
106 | youtubeDL.exec(videoInfo.url, options, processOptions, async (err, output) => { | |
107 | if (err) { | |
108 | console.error(err) | |
109 | return res() | |
110 | } | |
111 | ||
112 | console.log(output.join('\n')) | |
113 | await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, languageCode) | |
114 | return res() | |
115 | }) | |
116 | } catch (err) { | |
117 | console.log(err.message) | |
118 | return res() | |
119 | } | |
120 | }) | |
121 | } | |
122 | ||
123 | async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, language?: number) { | |
124 | const category = await getCategory(videoInfo.categories) | |
125 | const licence = getLicence(videoInfo.license) | |
126 | let tags = [] | |
127 | if (Array.isArray(videoInfo.tags)) { | |
128 | tags = videoInfo.tags | |
129 | .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) | |
130 | .map(t => t.normalize()) | |
131 | .slice(0, 5) | |
132 | } | |
133 | ||
134 | let thumbnailfile | |
135 | if (videoInfo.thumbnail) { | |
136 | thumbnailfile = join(__dirname, 'thumbnail.jpg') | |
137 | ||
138 | await doRequestAndSaveToFile({ | |
139 | method: 'GET', | |
140 | uri: videoInfo.thumbnail | |
141 | }, thumbnailfile) | |
142 | } | |
143 | ||
144 | const videoAttributes = { | |
145 | name: truncate(videoInfo.title, { | |
146 | 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max, | |
147 | 'separator': /,? +/, | |
148 | 'omission': ' […]' | |
149 | }), | |
150 | category, | |
151 | licence, | |
152 | language, | |
153 | nsfw: isNSFW(videoInfo), | |
154 | commentsEnabled: true, | |
155 | description: videoInfo.description || undefined, | |
156 | support: undefined, | |
157 | tags, | |
158 | privacy: VideoPrivacy.PUBLIC, | |
159 | fixture: videoPath, | |
160 | thumbnailfile, | |
161 | previewfile: thumbnailfile | |
162 | } | |
163 | ||
164 | console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) | |
165 | try { | |
166 | await uploadVideo(program['url'], accessToken, videoAttributes) | |
167 | } catch (err) { | |
168 | if (err.message.indexOf('401') !== -1) { | |
169 | console.log('Got 401 Unauthorized, token may have expired, renewing token and retry.') | |
170 | ||
171 | const res = await login(program['url'], client, user) | |
172 | accessToken = res.body.access_token | |
173 | ||
174 | await uploadVideo(program['url'], accessToken, videoAttributes) | |
175 | } else { | |
176 | console.log(err.message) | |
177 | process.exit(1) | |
178 | } | |
179 | } | |
180 | ||
181 | await unlinkPromise(videoPath) | |
182 | if (thumbnailfile) { | |
183 | await unlinkPromise(thumbnailfile) | |
184 | } | |
185 | ||
186 | console.log('Uploaded video "%s"!\n', videoAttributes.name) | |
187 | } | |
188 | ||
189 | async function getCategory (categories: string[]) { | |
190 | if (!categories) return undefined | |
191 | ||
192 | const categoryString = categories[0] | |
193 | ||
194 | if (categoryString === 'News & Politics') return 11 | |
195 | ||
196 | const res = await getVideoCategories(program['url']) | |
197 | const categoriesServer = res.body | |
198 | ||
199 | for (const key of Object.keys(categoriesServer)) { | |
200 | const categoryServer = categoriesServer[key] | |
201 | if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10) | |
202 | } | |
203 | ||
204 | return undefined | |
205 | } | |
206 | ||
207 | function getLicence (licence: string) { | |
208 | if (!licence) return undefined | |
209 | ||
210 | if (licence.indexOf('Creative Commons Attribution licence') !== -1) return 1 | |
211 | ||
212 | return undefined | |
213 | } | |
214 | ||
215 | function normalizeObject (obj: any) { | |
216 | const newObj: any = {} | |
217 | ||
218 | for (const key of Object.keys(obj)) { | |
219 | // Deprecated key | |
220 | if (key === 'resolution') continue | |
221 | ||
222 | const value = obj[key] | |
223 | ||
224 | if (typeof value === 'string') { | |
225 | newObj[key] = value.normalize() | |
226 | } else { | |
227 | newObj[key] = value | |
228 | } | |
229 | } | |
230 | ||
231 | return newObj | |
232 | } | |
233 | ||
234 | function fetchObject (info: any) { | |
235 | const url = buildUrl(info) | |
236 | ||
237 | return new Promise<any>(async (res, rej) => { | |
238 | youtubeDL.getInfo(url, undefined, processOptions, async (err, videoInfo) => { | |
239 | if (err) return rej(err) | |
240 | ||
241 | const videoInfoWithUrl = Object.assign(videoInfo, { url }) | |
242 | return res(normalizeObject(videoInfoWithUrl)) | |
243 | }) | |
244 | }) | |
245 | } | |
246 | ||
247 | function buildUrl (info: any) { | |
248 | const webpageUrl = info.webpage_url as string | |
249 | if (webpageUrl && webpageUrl.match(/^https?:\/\//)) return webpageUrl | |
250 | ||
251 | const url = info.url as string | |
252 | if (url && url.match(/^https?:\/\//)) return url | |
253 | ||
254 | // It seems youtube-dl does not return the video url | |
255 | return 'https://www.youtube.com/watch?v=' + info.id | |
256 | } | |
257 | ||
258 | function isNSFW (info: any) { | |
259 | if (info.age_limit && info.age_limit >= 16) return true | |
260 | ||
261 | return false | |
262 | } |