aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/peertube-import-videos.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-04-25 13:55:28 +0200
committerChocobozzz <me@florianbigard.com>2019-04-25 13:55:28 +0200
commit2b4dd7e26d93c2d9bef4f365cb03c511eff4ca8f (patch)
tree5fe2982e0db8c1ccb661d5aa9c25b02c12590401 /server/tools/peertube-import-videos.ts
parent47f6409bb8bc49a50027b9579bb651c0506b6912 (diff)
downloadPeerTube-2b4dd7e26d93c2d9bef4f365cb03c511eff4ca8f.tar.gz
PeerTube-2b4dd7e26d93c2d9bef4f365cb03c511eff4ca8f.tar.zst
PeerTube-2b4dd7e26d93c2d9bef4f365cb03c511eff4ca8f.zip
Fix optional privacy in upload endpoint
Diffstat (limited to 'server/tools/peertube-import-videos.ts')
-rw-r--r--server/tools/peertube-import-videos.ts84
1 files changed, 33 insertions, 51 deletions
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts
index 024f640a4..9a366dbbd 100644
--- a/server/tools/peertube-import-videos.ts
+++ b/server/tools/peertube-import-videos.ts
@@ -12,7 +12,7 @@ import * as prompt from 'prompt'
12import { remove } from 'fs-extra' 12import { remove } from 'fs-extra'
13import { sha256 } from '../helpers/core-utils' 13import { sha256 } from '../helpers/core-utils'
14import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl' 14import { buildOriginallyPublishedAt, safeGetYoutubeDL } from '../helpers/youtube-dl'
15import { getSettings, netrc } from './cli' 15import { getNetrc, getRemoteObjectOrDie, getSettings } from './cli'
16 16
17let accessToken: string 17let accessToken: string
18let client: { id: string, secret: string } 18let client: { id: string, secret: string }
@@ -32,48 +32,30 @@ program
32 .option('-v, --verbose', 'Verbose mode') 32 .option('-v, --verbose', 'Verbose mode')
33 .parse(process.argv) 33 .parse(process.argv)
34 34
35getSettings() 35Promise.all([ getSettings(), getNetrc() ])
36.then(settings => { 36 .then(([ settings, netrc ]) => {
37 if ((!program['url'] || !program['username'] || !program['targetUrl']) && settings.remotes.length === 0) { 37 const { url, username, password } = getRemoteObjectOrDie(program, settings)
38 if (!program['url']) console.error('--url field is required.')
39 if (!program['username']) console.error('--username field is required.')
40 if (!program['targetUrl']) console.error('--targetUrl field is required.')
41 38
42 process.exit(-1) 39 if (!program[ 'targetUrl' ]) {
43 } 40 console.error('--targetUrl field is required.')
44
45 if ((!program[ 'url' ] || !program[ 'username' ]) && settings.remotes.length > 0) {
46 if (!program[ 'url' ]) {
47 program[ 'url' ] = settings.default !== -1
48 ? settings.remotes[ settings.default ]
49 : settings.remotes[ 0 ]
50 }
51
52 if (!program['username']) program['username'] = netrc.machines[program['url']].login
53 if (!program['password']) program['password'] = netrc.machines[program['url']].password
54 }
55 41
56 if ( 42 process.exit(-1)
57 !program['targetUrl'] 43 }
58 ) {
59 if (!program['targetUrl']) console.error('--targetUrl field is required.')
60 process.exit(-1)
61 }
62 44
63 removeEndSlashes(program['url']) 45 removeEndSlashes(url)
64 removeEndSlashes(program['targetUrl']) 46 removeEndSlashes(program[ 'targetUrl' ])
65 47
66 const user = { 48 const user = {
67 username: program['username'], 49 username: username,
68 password: program['password'] 50 password: password
69 } 51 }
70 52
71 run(user, program['url']) 53 run(user, url)
72 .catch(err => { 54 .catch(err => {
73 console.error(err) 55 console.error(err)
74 process.exit(-1) 56 process.exit(-1)
75 }) 57 })
76}) 58 })
77 59
78async function promptPassword () { 60async function promptPassword () {
79 return new Promise((res, rej) => { 61 return new Promise((res, rej) => {
@@ -116,7 +98,7 @@ async function run (user, url: string) {
116 const youtubeDL = await safeGetYoutubeDL() 98 const youtubeDL = await safeGetYoutubeDL()
117 99
118 const options = [ '-j', '--flat-playlist', '--playlist-reverse' ] 100 const options = [ '-j', '--flat-playlist', '--playlist-reverse' ]
119 youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => { 101 youtubeDL.getInfo(program[ 'targetUrl' ], options, processOptions, async (err, info) => {
120 if (err) { 102 if (err) {
121 console.log(err.message) 103 console.log(err.message)
122 process.exit(1) 104 process.exit(1)
@@ -133,20 +115,20 @@ async function run (user, url: string) {
133 console.log('Will download and upload %d videos.\n', infoArray.length) 115 console.log('Will download and upload %d videos.\n', infoArray.length)
134 116
135 for (const info of infoArray) { 117 for (const info of infoArray) {
136 await processVideo(info, program['language'], processOptions.cwd, url, user) 118 await processVideo(info, program[ 'language' ], processOptions.cwd, url, user)
137 } 119 }
138 120
139 console.log('Video/s for user %s imported: %s', program['username'], program['targetUrl']) 121 console.log('Video/s for user %s imported: %s', program[ 'username' ], program[ 'targetUrl' ])
140 process.exit(0) 122 process.exit(0)
141 }) 123 })
142} 124}
143 125
144function processVideo (info: any, languageCode: string, cwd: string, url: string, user) { 126function processVideo (info: any, languageCode: string, cwd: string, url: string, user) {
145 return new Promise(async res => { 127 return new Promise(async res => {
146 if (program['verbose']) console.log('Fetching object.', info) 128 if (program[ 'verbose' ]) console.log('Fetching object.', info)
147 129
148 const videoInfo = await fetchObject(info) 130 const videoInfo = await fetchObject(info)
149 if (program['verbose']) console.log('Fetched object.', videoInfo) 131 if (program[ 'verbose' ]) console.log('Fetched object.', videoInfo)
150 132
151 const result = await searchVideoWithSort(url, videoInfo.title, '-match') 133 const result = await searchVideoWithSort(url, videoInfo.title, '-match')
152 134
@@ -187,9 +169,9 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
187 let tags = [] 169 let tags = []
188 if (Array.isArray(videoInfo.tags)) { 170 if (Array.isArray(videoInfo.tags)) {
189 tags = videoInfo.tags 171 tags = videoInfo.tags
190 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min) 172 .filter(t => t.length < CONSTRAINTS_FIELDS.VIDEOS.TAG.max && t.length > CONSTRAINTS_FIELDS.VIDEOS.TAG.min)
191 .map(t => t.normalize()) 173 .map(t => t.normalize())
192 .slice(0, 5) 174 .slice(0, 5)
193 } 175 }
194 176
195 let thumbnailfile 177 let thumbnailfile
@@ -253,7 +235,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
253async function getCategory (categories: string[], url: string) { 235async function getCategory (categories: string[], url: string) {
254 if (!categories) return undefined 236 if (!categories) return undefined
255 237
256 const categoryString = categories[0] 238 const categoryString = categories[ 0 ]
257 239
258 if (categoryString === 'News & Politics') return 11 240 if (categoryString === 'News & Politics') return 11
259 241
@@ -261,7 +243,7 @@ async function getCategory (categories: string[], url: string) {
261 const categoriesServer = res.body 243 const categoriesServer = res.body
262 244
263 for (const key of Object.keys(categoriesServer)) { 245 for (const key of Object.keys(categoriesServer)) {
264 const categoryServer = categoriesServer[key] 246 const categoryServer = categoriesServer[ key ]
265 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10) 247 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
266 } 248 }
267 249
@@ -285,12 +267,12 @@ function normalizeObject (obj: any) {
285 // Deprecated key 267 // Deprecated key
286 if (key === 'resolution') continue 268 if (key === 'resolution') continue
287 269
288 const value = obj[key] 270 const value = obj[ key ]
289 271
290 if (typeof value === 'string') { 272 if (typeof value === 'string') {
291 newObj[key] = value.normalize() 273 newObj[ key ] = value.normalize()
292 } else { 274 } else {
293 newObj[key] = value 275 newObj[ key ] = value
294 } 276 }
295 } 277 }
296 278