aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools
diff options
context:
space:
mode:
Diffstat (limited to 'server/tools')
-rw-r--r--server/tools/cli.ts72
-rw-r--r--server/tools/peertube-auth.ts16
-rw-r--r--server/tools/peertube-get-access-token.ts2
-rw-r--r--server/tools/peertube-import-videos.ts106
-rw-r--r--server/tools/peertube-upload.ts98
5 files changed, 138 insertions, 156 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts
index 108c44218..59e9fcfc4 100644
--- a/server/tools/cli.ts
+++ b/server/tools/cli.ts
@@ -3,51 +3,83 @@ const netrc = require('netrc-parser').default
3 3
4const version = require('../../../package.json').version 4const version = require('../../../package.json').version
5 5
6let settings = {
7 remotes: [],
8 default: 0
9}
10
11interface Settings { 6interface Settings {
12 remotes: any[], 7 remotes: any[],
13 default: number 8 default: number
14} 9}
15 10
16async function getSettings () { 11function getSettings () {
17 return new Promise<Settings>((res, rej) => { 12 return new Promise<Settings>((res, rej) => {
18 let settings = { 13 const defaultSettings = {
19 remotes: [], 14 remotes: [],
20 default: 0 15 default: 0
21 } as Settings 16 }
17
22 config.read((err, data) => { 18 config.read((err, data) => {
23 if (err) { 19 if (err) return rej(err)
24 return rej(err) 20
25 } 21 return res(Object.keys(data).length === 0 ? defaultSettings : data)
26 return res(Object.keys(data).length === 0 ? settings : data)
27 }) 22 })
28 }) 23 })
29} 24}
30 25
31async function writeSettings (settings) { 26async function getNetrc () {
27 await netrc.load()
28
29 return netrc
30}
31
32function writeSettings (settings) {
32 return new Promise((res, rej) => { 33 return new Promise((res, rej) => {
33 config.write(settings, function (err) { 34 config.write(settings, function (err) {
34 if (err) { 35 if (err) return rej(err)
35 return rej(err) 36
36 }
37 return res() 37 return res()
38 }) 38 })
39 }) 39 })
40} 40}
41 41
42netrc.loadSync() 42function getRemoteObjectOrDie (program: any, settings: Settings) {
43 if (!program['url'] || !program['username'] || !program['password']) {
44 // No remote and we don't have program parameters: throw
45 if (settings.remotes.length === 0) {
46 if (!program[ 'url' ]) console.error('--url field is required.')
47 if (!program[ 'username' ]) console.error('--username field is required.')
48 if (!program[ 'password' ]) console.error('--password field is required.')
49
50 return process.exit(-1)
51 }
52
53 let url: string = program['url']
54 let username: string = program['username']
55 let password: string = program['password']
56
57 if (!url) {
58 url = settings.default !== -1
59 ? settings.remotes[settings.default]
60 : settings.remotes[0]
61 }
62
63 if (!username) username = netrc.machines[url].login
64 if (!password) password = netrc.machines[url].password
65
66 return { url, username, password }
67 }
68
69 return {
70 url: program[ 'url' ],
71 username: program[ 'username' ],
72 password: program[ 'password' ]
73 }
74}
43 75
44// --------------------------------------------------------------------------- 76// ---------------------------------------------------------------------------
45 77
46export { 78export {
47 version, 79 version,
48 config, 80 config,
49 settings,
50 getSettings, 81 getSettings,
51 writeSettings, 82 getNetrc,
52 netrc 83 getRemoteObjectOrDie,
84 writeSettings
53} 85}
diff --git a/server/tools/peertube-auth.ts b/server/tools/peertube-auth.ts
index a962944a4..8bc3d332c 100644
--- a/server/tools/peertube-auth.ts
+++ b/server/tools/peertube-auth.ts
@@ -1,22 +1,25 @@
1import * as program from 'commander' 1import * as program from 'commander'
2import * as prompt from 'prompt' 2import * as prompt from 'prompt'
3const Table = require('cli-table') 3import { getSettings, writeSettings, getNetrc } from './cli'
4import { getSettings, writeSettings, netrc } from './cli'
5import { isHostValid } from '../helpers/custom-validators/servers' 4import { isHostValid } from '../helpers/custom-validators/servers'
6import { isUserUsernameValid } from '../helpers/custom-validators/users' 5import { isUserUsernameValid } from '../helpers/custom-validators/users'
7 6
7const Table = require('cli-table')
8
8async function delInstance (url: string) { 9async function delInstance (url: string) {
9 const settings = await getSettings() 10 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
10 11
11 settings.remotes.splice(settings.remotes.indexOf(url)) 12 settings.remotes.splice(settings.remotes.indexOf(url))
12 await writeSettings(settings) 13 await writeSettings(settings)
13 14
14 delete netrc.machines[url] 15 delete netrc.machines[url]
16
15 await netrc.save() 17 await netrc.save()
16} 18}
17 19
18async function setInstance (url: string, username: string, password: string) { 20async function setInstance (url: string, username: string, password: string) {
19 const settings = await getSettings() 21 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
22
20 if (settings.remotes.indexOf(url) === -1) { 23 if (settings.remotes.indexOf(url) === -1) {
21 settings.remotes.push(url) 24 settings.remotes.push(url)
22 } 25 }
@@ -82,12 +85,13 @@ program
82 .command('list') 85 .command('list')
83 .description('lists registered remote instances') 86 .description('lists registered remote instances')
84 .action(async () => { 87 .action(async () => {
85 const settings = await getSettings() 88 const [ settings, netrc ] = await Promise.all([ getSettings(), getNetrc() ])
89
86 const table = new Table({ 90 const table = new Table({
87 head: ['instance', 'login'], 91 head: ['instance', 'login'],
88 colWidths: [30, 30] 92 colWidths: [30, 30]
89 }) 93 })
90 netrc.loadSync() 94
91 settings.remotes.forEach(element => { 95 settings.remotes.forEach(element => {
92 table.push([ 96 table.push([
93 element, 97 element,
diff --git a/server/tools/peertube-get-access-token.ts b/server/tools/peertube-get-access-token.ts
index a68665f5b..85660de2c 100644
--- a/server/tools/peertube-get-access-token.ts
+++ b/server/tools/peertube-get-access-token.ts
@@ -6,7 +6,7 @@ import {
6 Server, 6 Server,
7 Client, 7 Client,
8 User 8 User
9} from '../../shared/utils' 9} from '../../shared/extra-utils'
10 10
11program 11program
12 .option('-u, --url <url>', 'Server url') 12 .option('-u, --url <url>', 'Server url')
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts
index f50aafc35..9a366dbbd 100644
--- a/server/tools/peertube-import-videos.ts
+++ b/server/tools/peertube-import-videos.ts
@@ -5,14 +5,14 @@ import * as program from 'commander'
5import { join } from 'path' 5import { join } from 'path'
6import { VideoPrivacy } from '../../shared/models/videos' 6import { VideoPrivacy } from '../../shared/models/videos'
7import { doRequestAndSaveToFile } from '../helpers/requests' 7import { doRequestAndSaveToFile } from '../helpers/requests'
8import { CONSTRAINTS_FIELDS } from '../initializers' 8import { CONSTRAINTS_FIELDS } from '../initializers/constants'
9import { getClient, getVideoCategories, login, searchVideoWithSort, uploadVideo } from '../../shared/utils/index' 9import { getClient, getVideoCategories, login, searchVideoWithSort, uploadVideo } from '../../shared/extra-utils/index'
10import { truncate } from 'lodash' 10import { truncate } from 'lodash'
11import * as prompt from 'prompt' 11import * 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 { 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,58 +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 ( 37 const { url, username, password } = getRemoteObjectOrDie(program, settings)
38 (!program['url'] ||
39 !program['username'] ||
40 !program['password']) &&
41 (settings.remotes.length === 0)
42 ) {
43 if (!program['url']) console.error('--url field is required.')
44 if (!program['username']) console.error('--username field is required.')
45 if (!program['password']) console.error('--password field is required.')
46 if (!program['targetUrl']) console.error('--targetUrl field is required.')
47 process.exit(-1)
48 }
49
50 if (
51 (!program['url'] ||
52 !program['username'] ||
53 !program['password']) &&
54 (settings.remotes.length > 0)
55 ) {
56 if (!program['url']) {
57 program['url'] = (settings.default !== -1) ?
58 settings.remotes[settings.default] :
59 settings.remotes[0]
60 }
61 38
62 if (!program['username']) program['username'] = netrc.machines[program['url']].login 39 if (!program[ 'targetUrl' ]) {
63 if (!program['password']) program['password'] = netrc.machines[program['url']].password 40 console.error('--targetUrl field is required.')
64 }
65 41
66 if ( 42 process.exit(-1)
67 !program['targetUrl'] 43 }
68 ) {
69 if (!program['targetUrl']) console.error('--targetUrl field is required.')
70 process.exit(-1)
71 }
72 44
73 removeEndSlashes(program['url']) 45 removeEndSlashes(url)
74 removeEndSlashes(program['targetUrl']) 46 removeEndSlashes(program[ 'targetUrl' ])
75 47
76 const user = { 48 const user = {
77 username: program['username'], 49 username: username,
78 password: program['password'] 50 password: password
79 } 51 }
80 52
81 run(user, program['url']) 53 run(user, url)
82 .catch(err => { 54 .catch(err => {
83 console.error(err) 55 console.error(err)
84 process.exit(-1) 56 process.exit(-1)
85 }) 57 })
86}) 58 })
87 59
88async function promptPassword () { 60async function promptPassword () {
89 return new Promise((res, rej) => { 61 return new Promise((res, rej) => {
@@ -126,7 +98,7 @@ async function run (user, url: string) {
126 const youtubeDL = await safeGetYoutubeDL() 98 const youtubeDL = await safeGetYoutubeDL()
127 99
128 const options = [ '-j', '--flat-playlist', '--playlist-reverse' ] 100 const options = [ '-j', '--flat-playlist', '--playlist-reverse' ]
129 youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => { 101 youtubeDL.getInfo(program[ 'targetUrl' ], options, processOptions, async (err, info) => {
130 if (err) { 102 if (err) {
131 console.log(err.message) 103 console.log(err.message)
132 process.exit(1) 104 process.exit(1)
@@ -143,20 +115,20 @@ async function run (user, url: string) {
143 console.log('Will download and upload %d videos.\n', infoArray.length) 115 console.log('Will download and upload %d videos.\n', infoArray.length)
144 116
145 for (const info of infoArray) { 117 for (const info of infoArray) {
146 await processVideo(info, program['language'], processOptions.cwd, url, user) 118 await processVideo(info, program[ 'language' ], processOptions.cwd, url, user)
147 } 119 }
148 120
149 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' ])
150 process.exit(0) 122 process.exit(0)
151 }) 123 })
152} 124}
153 125
154function processVideo (info: any, languageCode: string, cwd: string, url: string, user) { 126function processVideo (info: any, languageCode: string, cwd: string, url: string, user) {
155 return new Promise(async res => { 127 return new Promise(async res => {
156 if (program['verbose']) console.log('Fetching object.', info) 128 if (program[ 'verbose' ]) console.log('Fetching object.', info)
157 129
158 const videoInfo = await fetchObject(info) 130 const videoInfo = await fetchObject(info)
159 if (program['verbose']) console.log('Fetched object.', videoInfo) 131 if (program[ 'verbose' ]) console.log('Fetched object.', videoInfo)
160 132
161 const result = await searchVideoWithSort(url, videoInfo.title, '-match') 133 const result = await searchVideoWithSort(url, videoInfo.title, '-match')
162 134
@@ -197,9 +169,9 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
197 let tags = [] 169 let tags = []
198 if (Array.isArray(videoInfo.tags)) { 170 if (Array.isArray(videoInfo.tags)) {
199 tags = videoInfo.tags 171 tags = videoInfo.tags
200 .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)
201 .map(t => t.normalize()) 173 .map(t => t.normalize())
202 .slice(0, 5) 174 .slice(0, 5)
203 } 175 }
204 176
205 let thumbnailfile 177 let thumbnailfile
@@ -212,6 +184,8 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
212 }, thumbnailfile) 184 }, thumbnailfile)
213 } 185 }
214 186
187 const originallyPublishedAt = buildOriginallyPublishedAt(videoInfo)
188
215 const videoAttributes = { 189 const videoAttributes = {
216 name: truncate(videoInfo.title, { 190 name: truncate(videoInfo.title, {
217 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max, 191 'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
@@ -224,13 +198,15 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
224 nsfw: isNSFW(videoInfo), 198 nsfw: isNSFW(videoInfo),
225 waitTranscoding: true, 199 waitTranscoding: true,
226 commentsEnabled: true, 200 commentsEnabled: true,
201 downloadEnabled: true,
227 description: videoInfo.description || undefined, 202 description: videoInfo.description || undefined,
228 support: undefined, 203 support: undefined,
229 tags, 204 tags,
230 privacy: VideoPrivacy.PUBLIC, 205 privacy: VideoPrivacy.PUBLIC,
231 fixture: videoPath, 206 fixture: videoPath,
232 thumbnailfile, 207 thumbnailfile,
233 previewfile: thumbnailfile 208 previewfile: thumbnailfile,
209 originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null
234 } 210 }
235 211
236 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) 212 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)
@@ -259,7 +235,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
259async function getCategory (categories: string[], url: string) { 235async function getCategory (categories: string[], url: string) {
260 if (!categories) return undefined 236 if (!categories) return undefined
261 237
262 const categoryString = categories[0] 238 const categoryString = categories[ 0 ]
263 239
264 if (categoryString === 'News & Politics') return 11 240 if (categoryString === 'News & Politics') return 11
265 241
@@ -267,7 +243,7 @@ async function getCategory (categories: string[], url: string) {
267 const categoriesServer = res.body 243 const categoriesServer = res.body
268 244
269 for (const key of Object.keys(categoriesServer)) { 245 for (const key of Object.keys(categoriesServer)) {
270 const categoryServer = categoriesServer[key] 246 const categoryServer = categoriesServer[ key ]
271 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10) 247 if (categoryString.toLowerCase() === categoryServer.toLowerCase()) return parseInt(key, 10)
272 } 248 }
273 249
@@ -291,12 +267,12 @@ function normalizeObject (obj: any) {
291 // Deprecated key 267 // Deprecated key
292 if (key === 'resolution') continue 268 if (key === 'resolution') continue
293 269
294 const value = obj[key] 270 const value = obj[ key ]
295 271
296 if (typeof value === 'string') { 272 if (typeof value === 'string') {
297 newObj[key] = value.normalize() 273 newObj[ key ] = value.normalize()
298 } else { 274 } else {
299 newObj[key] = value 275 newObj[ key ] = value
300 } 276 }
301 } 277 }
302 278
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts
index cc7bd9b4c..687f2e60b 100644
--- a/server/tools/peertube-upload.ts
+++ b/server/tools/peertube-upload.ts
@@ -1,10 +1,10 @@
1import * as program from 'commander' 1import * as program from 'commander'
2import { access, constants } from 'fs-extra' 2import { access, constants } from 'fs-extra'
3import { isAbsolute } from 'path' 3import { isAbsolute } from 'path'
4import { getClient, login } from '../../shared/utils' 4import { getClient, login } from '../../shared/extra-utils'
5import { uploadVideo } from '../../shared/utils/' 5import { uploadVideo } from '../../shared/extra-utils/'
6import { VideoPrivacy } from '../../shared/models/videos' 6import { VideoPrivacy } from '../../shared/models/videos'
7import { netrc, getSettings } from './cli' 7import { getRemoteObjectOrDie, getSettings } from './cli'
8 8
9program 9program
10 .name('upload') 10 .name('upload')
@@ -26,48 +26,15 @@ program
26 .option('-f, --file <file>', 'Video absolute file path') 26 .option('-f, --file <file>', 'Video absolute file path')
27 .parse(process.argv) 27 .parse(process.argv)
28 28
29if (!program['tags']) program['tags'] = []
30if (!program['nsfw']) program['nsfw'] = false
31if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
32if (!program['commentsEnabled']) program['commentsEnabled'] = false
33
34getSettings() 29getSettings()
35 .then(settings => { 30 .then(settings => {
36 if ( 31 const { url, username, password } = getRemoteObjectOrDie(program, settings)
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['videoName']) console.error('--video-name field is required.')
46 if (!program['file']) console.error('--file field is required.')
47 process.exit(-1)
48 }
49 32
50 if ( 33 if (!program['videoName'] || !program['file'] || !program['channelId']) {
51 (!program['url'] || 34 if (!program['videoName']) console.error('--video-name is required.')
52 !program['username'] || 35 if (!program['file']) console.error('--file is required.')
53 !program['password']) && 36 if (!program['channelId']) console.error('--channel-id is required.')
54 (settings.remotes.length > 0)
55 ) {
56 if (!program['url']) {
57 program['url'] = (settings.default !== -1) ?
58 settings.remotes[settings.default] :
59 settings.remotes[0]
60 }
61 if (!program['username']) program['username'] = netrc.machines[program['url']].login
62 if (!program['password']) program['password'] = netrc.machines[program['url']].password
63 }
64 37
65 if (
66 !program['videoName'] ||
67 !program['file']
68 ) {
69 if (!program['videoName']) console.error('--video-name field is required.')
70 if (!program['file']) console.error('--file field is required.')
71 process.exit(-1) 38 process.exit(-1)
72 } 39 }
73 40
@@ -76,28 +43,25 @@ getSettings()
76 process.exit(-1) 43 process.exit(-1)
77 } 44 }
78 45
79 run().catch(err => { 46 run(url, username, password).catch(err => {
80 console.error(err) 47 console.error(err)
81 process.exit(-1) 48 process.exit(-1)
82 }) 49 })
83 }) 50 })
84 51
85async function run () { 52async function run (url: string, username: string, password: string) {
86 const res = await getClient(program[ 'url' ]) 53 const resClient = await getClient(program[ 'url' ])
87 const client = { 54 const client = {
88 id: res.body.client_id, 55 id: resClient.body.client_id,
89 secret: res.body.client_secret 56 secret: resClient.body.client_secret
90 } 57 }
91 58
92 const user = { 59 const user = { username, password }
93 username: program[ 'username' ],
94 password: program[ 'password' ]
95 }
96 60
97 let accessToken: string 61 let accessToken: string
98 try { 62 try {
99 const res2 = await login(program[ 'url' ], client, user) 63 const res = await login(url, client, user)
100 accessToken = res2.body.access_token 64 accessToken = res.body.access_token
101 } catch (err) { 65 } catch (err) {
102 throw new Error('Cannot authenticate. Please check your username/password.') 66 throw new Error('Cannot authenticate. Please check your username/password.')
103 } 67 }
@@ -108,26 +72,32 @@ async function run () {
108 72
109 const videoAttributes = { 73 const videoAttributes = {
110 name: program['videoName'], 74 name: program['videoName'],
111 category: program['category'], 75 category: program['category'] || undefined,
112 channelId: program['channelId'], 76 channelId: program['channelId'],
113 licence: program['licence'], 77 licence: program['licence'] || undefined,
114 language: program['language'], 78 language: program['language'] || undefined,
115 nsfw: program['nsfw'], 79 nsfw: program['nsfw'] !== undefined ? program['nsfw'] : false,
116 description: program['videoDescription'], 80 description: program['videoDescription'] || '',
117 tags: program['tags'], 81 tags: program['tags'] || [],
118 commentsEnabled: program['commentsEnabled'], 82 commentsEnabled: program['commentsEnabled'] !== undefined ? program['commentsEnabled'] : true,
83 downloadEnabled: program['downloadEnabled'] !== undefined ? program['downloadEnabled'] : true,
119 fixture: program['file'], 84 fixture: program['file'],
120 thumbnailfile: program['thumbnail'], 85 thumbnailfile: program['thumbnail'],
121 previewfile: program['preview'], 86 previewfile: program['preview'],
122 waitTranscoding: true, 87 waitTranscoding: true,
123 privacy: program['privacy'], 88 privacy: program['privacy'] || VideoPrivacy.PUBLIC,
124 support: undefined 89 support: undefined
125 } 90 }
126 91
127 await uploadVideo(program[ 'url' ], accessToken, videoAttributes) 92 try {
128 93 await uploadVideo(url, accessToken, videoAttributes)
129 console.log(`Video ${program['videoName']} uploaded.`) 94 console.log(`Video ${program['videoName']} uploaded.`)
130 process.exit(0) 95 process.exit(0)
96 } catch (err) {
97 console.log('coucou')
98 console.error(require('util').inspect(err))
99 process.exit(-1)
100 }
131} 101}
132 102
133// ---------------------------------------------------------------------------- 103// ----------------------------------------------------------------------------