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