1 import * as program from 'commander'
2 import { access, constants } from 'fs-extra'
3 import { isAbsolute } from 'path'
4 import { getClient, login } from '../../shared/utils'
5 import { uploadVideo } from '../../shared/utils/'
6 import { VideoPrivacy } from '../../shared/models/videos'
7 import { netrc, getSettings } from './cli'
11 .option('-u, --url <url>', 'Server url')
12 .option('-U, --username <username>', 'Username')
13 .option('-p, --password <token>', 'Password')
14 .option('-n, --video-name <name>', 'Video name')
15 .option('-P, --privacy <privacy_number>', 'Privacy')
16 .option('-N, --nsfw', 'Video is Not Safe For Work')
17 .option('-c, --category <category_number>', 'Category number')
18 .option('-C, --channel-id <channel_id>', 'Channel ID')
19 .option('-m, --comments-enabled', 'Enable comments')
20 .option('-l, --licence <licence_number>', 'Licence number')
21 .option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
22 .option('-d, --video-description <description>', 'Video description')
23 .option('-t, --tags <tags>', 'Video tags', list)
24 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
25 .option('-v, --preview <previewPath>', 'Preview path')
26 .option('-f, --file <file>', 'Video absolute file path')
29 if (!program['tags']) program['tags'] = []
30 if (!program['nsfw']) program['nsfw'] = false
31 if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
32 if (!program['commentsEnabled']) program['commentsEnabled'] = false
38 !program['username'] ||
39 !program['password']) &&
40 (settings.remotes.length === 0)
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.')
52 !program['username'] ||
53 !program['password']) &&
54 (settings.remotes.length > 0)
56 if (!program['url']) {
57 program['url'] = (settings.default !== -1) ?
58 settings.remotes[settings.default] :
61 if (!program['username']) program['username'] = netrc.machines[program['url']].login
62 if (!program['password']) program['password'] = netrc.machines[program['url']].password
66 !program['videoName'] ||
69 if (!program['videoName']) console.error('--video-name field is required.')
70 if (!program['file']) console.error('--file field is required.')
74 if (isAbsolute(program['file']) === false) {
75 console.error('File path should be absolute.')
85 async function run () {
86 const res = await getClient(program[ 'url' ])
88 id: res.body.client_id,
89 secret: res.body.client_secret
93 username: program[ 'username' ],
94 password: program[ 'password' ]
97 let accessToken: string
99 const res2 = await login(program[ 'url' ], client, user)
100 accessToken = res2.body.access_token
102 throw new Error('Cannot authenticate. Please check your username/password.')
105 await access(program[ 'file' ], constants.F_OK)
107 console.log('Uploading %s video...', program[ 'videoName' ])
109 const videoAttributes = {
110 name: program['videoName'],
111 category: program['category'],
112 channelId: program['channelId'],
113 licence: program['licence'],
114 language: program['language'],
115 nsfw: program['nsfw'],
116 description: program['videoDescription'],
117 tags: program['tags'],
118 commentsEnabled: program['commentsEnabled'],
119 fixture: program['file'],
120 thumbnailfile: program['thumbnail'],
121 previewfile: program['preview'],
122 waitTranscoding: true,
123 privacy: program['privacy'],
127 await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
129 console.log(`Video ${program['videoName']} uploaded.`)
133 // ----------------------------------------------------------------------------
135 function list (val) {
136 return val.split(',')