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