2017-09-07 17:58:09 +02:00
|
|
|
import * as program from 'commander'
|
2018-08-27 13:28:49 +02:00
|
|
|
import { access, constants } from 'fs-extra'
|
2017-09-07 17:58:09 +02:00
|
|
|
import { isAbsolute } from 'path'
|
2019-04-15 15:26:15 +02:00
|
|
|
import { getClient, login } from '../../shared/extra-utils'
|
|
|
|
import { uploadVideo } from '../../shared/extra-utils/'
|
2018-04-04 10:47:02 +02:00
|
|
|
import { VideoPrivacy } from '../../shared/models/videos'
|
2018-09-13 14:27:44 +02:00
|
|
|
import { netrc, getSettings } from './cli'
|
2017-09-07 17:58:09 +02:00
|
|
|
|
|
|
|
program
|
2018-09-13 14:27:44 +02:00
|
|
|
.name('upload')
|
2017-09-07 17:58:09 +02:00
|
|
|
.option('-u, --url <url>', 'Server url')
|
2018-02-12 12:48:58 +01:00
|
|
|
.option('-U, --username <username>', 'Username')
|
|
|
|
.option('-p, --password <token>', 'Password')
|
|
|
|
.option('-n, --video-name <name>', 'Video name')
|
2018-09-13 14:27:44 +02:00
|
|
|
.option('-P, --privacy <privacy_number>', 'Privacy')
|
2017-09-07 17:58:09 +02:00
|
|
|
.option('-N, --nsfw', 'Video is Not Safe For Work')
|
2018-09-13 14:27:44 +02:00
|
|
|
.option('-c, --category <category_number>', 'Category number')
|
2018-09-28 21:33:48 +02:00
|
|
|
.option('-C, --channel-id <channel_id>', 'Channel ID')
|
2018-02-12 12:48:58 +01:00
|
|
|
.option('-m, --comments-enabled', 'Enable comments')
|
2018-09-13 14:27:44 +02:00
|
|
|
.option('-l, --licence <licence_number>', 'Licence number')
|
|
|
|
.option('-L, --language <language_code>', 'Language ISO 639 code (fr or en...)')
|
2018-02-12 12:48:58 +01:00
|
|
|
.option('-d, --video-description <description>', 'Video description')
|
2017-09-07 17:58:09 +02:00
|
|
|
.option('-t, --tags <tags>', 'Video tags', list)
|
2018-02-15 14:46:26 +01:00
|
|
|
.option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
|
2018-02-15 18:40:24 +01:00
|
|
|
.option('-v, --preview <previewPath>', 'Preview path')
|
2017-09-07 17:58:09 +02:00
|
|
|
.option('-f, --file <file>', 'Video absolute file path')
|
|
|
|
.parse(process.argv)
|
|
|
|
|
|
|
|
if (!program['tags']) program['tags'] = []
|
|
|
|
if (!program['nsfw']) program['nsfw'] = false
|
2018-04-04 10:47:02 +02:00
|
|
|
if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC
|
2018-02-12 12:48:58 +01:00
|
|
|
if (!program['commentsEnabled']) program['commentsEnabled'] = false
|
2018-10-08 14:45:22 +02:00
|
|
|
if (!program['downloadEnabled']) program['downloadEnabled'] = true
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2018-09-13 14:27:44 +02:00
|
|
|
getSettings()
|
|
|
|
.then(settings => {
|
|
|
|
if (
|
|
|
|
(!program['url'] ||
|
|
|
|
!program['username'] ||
|
|
|
|
!program['password']) &&
|
|
|
|
(settings.remotes.length === 0)
|
|
|
|
) {
|
|
|
|
if (!program['url']) console.error('--url field is required.')
|
|
|
|
if (!program['username']) console.error('--username field is required.')
|
|
|
|
if (!program['password']) console.error('--password field is required.')
|
|
|
|
if (!program['videoName']) console.error('--video-name field is required.')
|
|
|
|
if (!program['file']) console.error('--file field is required.')
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
(!program['url'] ||
|
|
|
|
!program['username'] ||
|
|
|
|
!program['password']) &&
|
|
|
|
(settings.remotes.length > 0)
|
|
|
|
) {
|
|
|
|
if (!program['url']) {
|
|
|
|
program['url'] = (settings.default !== -1) ?
|
|
|
|
settings.remotes[settings.default] :
|
|
|
|
settings.remotes[0]
|
|
|
|
}
|
|
|
|
if (!program['username']) program['username'] = netrc.machines[program['url']].login
|
|
|
|
if (!program['password']) program['password'] = netrc.machines[program['url']].password
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!program['videoName'] ||
|
|
|
|
!program['file']
|
|
|
|
) {
|
|
|
|
if (!program['videoName']) console.error('--video-name field is required.')
|
|
|
|
if (!program['file']) console.error('--file field is required.')
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isAbsolute(program['file']) === false) {
|
|
|
|
console.error('File path should be absolute.')
|
|
|
|
process.exit(-1)
|
|
|
|
}
|
|
|
|
|
2018-09-26 15:16:48 +02:00
|
|
|
run().catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(-1)
|
|
|
|
})
|
2018-09-13 14:27:44 +02:00
|
|
|
})
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2018-02-12 12:48:58 +01:00
|
|
|
async function run () {
|
|
|
|
const res = await getClient(program[ 'url' ])
|
|
|
|
const client = {
|
|
|
|
id: res.body.client_id,
|
|
|
|
secret: res.body.client_secret
|
|
|
|
}
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2018-02-12 12:48:58 +01:00
|
|
|
const user = {
|
|
|
|
username: program[ 'username' ],
|
|
|
|
password: program[ 'password' ]
|
|
|
|
}
|
|
|
|
|
2018-09-26 15:16:48 +02:00
|
|
|
let accessToken: string
|
|
|
|
try {
|
|
|
|
const res2 = await login(program[ 'url' ], client, user)
|
|
|
|
accessToken = res2.body.access_token
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error('Cannot authenticate. Please check your username/password.')
|
|
|
|
}
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2018-08-27 16:23:34 +02:00
|
|
|
await access(program[ 'file' ], constants.F_OK)
|
2018-02-12 12:48:58 +01:00
|
|
|
|
|
|
|
console.log('Uploading %s video...', program[ 'videoName' ])
|
2017-09-07 17:58:09 +02:00
|
|
|
|
2018-09-29 02:49:33 +02:00
|
|
|
const videoAttributes = {
|
2018-02-12 12:48:58 +01:00
|
|
|
name: program['videoName'],
|
|
|
|
category: program['category'],
|
2018-09-29 02:49:33 +02:00
|
|
|
channelId: program['channelId'],
|
2018-02-12 12:48:58 +01:00
|
|
|
licence: program['licence'],
|
|
|
|
language: program['language'],
|
|
|
|
nsfw: program['nsfw'],
|
|
|
|
description: program['videoDescription'],
|
|
|
|
tags: program['tags'],
|
|
|
|
commentsEnabled: program['commentsEnabled'],
|
2018-10-08 14:45:22 +02:00
|
|
|
downloadEnabled: program['downloadEnabled'],
|
2018-02-15 14:46:26 +01:00
|
|
|
fixture: program['file'],
|
2018-09-03 11:12:53 +02:00
|
|
|
thumbnailfile: program['thumbnail'],
|
|
|
|
previewfile: program['preview'],
|
2018-06-12 20:04:58 +02:00
|
|
|
waitTranscoding: true,
|
2018-03-27 16:15:40 +02:00
|
|
|
privacy: program['privacy'],
|
2018-03-05 09:53:16 +01:00
|
|
|
support: undefined
|
2017-09-07 17:58:09 +02:00
|
|
|
}
|
2018-02-12 12:48:58 +01:00
|
|
|
|
2018-09-26 15:16:48 +02:00
|
|
|
await uploadVideo(program[ 'url' ], accessToken, videoAttributes)
|
2018-02-12 12:48:58 +01:00
|
|
|
|
|
|
|
console.log(`Video ${program['videoName']} uploaded.`)
|
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
function list (val) {
|
|
|
|
return val.split(',')
|
2017-09-07 17:58:09 +02:00
|
|
|
}
|