Fix upload from CLI script

pull/290/head
Chocobozzz 2018-02-12 12:48:58 +01:00
parent 3df456380a
commit a87d467ad7
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 58 additions and 134 deletions

View File

@ -251,10 +251,12 @@ async function uploadVideo (url: string, accessToken: string, videoAttributesArg
.field('name', attributes.name) .field('name', attributes.name)
.field('nsfw', JSON.stringify(attributes.nsfw)) .field('nsfw', JSON.stringify(attributes.nsfw))
.field('commentsEnabled', JSON.stringify(attributes.commentsEnabled)) .field('commentsEnabled', JSON.stringify(attributes.commentsEnabled))
.field('description', attributes.description)
.field('privacy', attributes.privacy.toString()) .field('privacy', attributes.privacy.toString())
.field('channelId', attributes.channelId) .field('channelId', attributes.channelId)
if (attributes.description !== undefined) {
req.field('description', attributes.description)
}
if (attributes.language !== undefined) { if (attributes.language !== undefined) {
req.field('language', attributes.language.toString()) req.field('language', attributes.language.toString())
} }

View File

@ -20,7 +20,8 @@ if (
!program['password'] || !program['password'] ||
!program['youtubeUrl'] !program['youtubeUrl']
) { ) {
throw new Error('All arguments are required.') console.error('All arguments are required.')
process.exit(-1)
} }
run().catch(err => console.error(err)) run().catch(err => console.error(err))

View File

@ -1,82 +0,0 @@
import * as program from 'commander'
import * as Promise from 'bluebird'
import { isAbsolute, join } from 'path'
import { readdirPromise } from '../helpers/core-utils'
import { execCLI } from '../tests/utils/index'
program
.option('-u, --url <url>', 'Server url')
.option('-U, --username <username>', 'Username')
.option('-p, --password <token>', 'Password')
.option('-i, --input <directory>', 'Videos directory absolute path')
.option('-d, --description <description>', 'Video descriptions')
.option('-c, --category <category>', 'Video categories')
.option('-l, --licence <licence>', 'Video licences')
.option('-t, --tags <tags>', 'Video tags', list)
.parse(process.argv)
if (
!program['url'] ||
!program['username'] ||
!program['password'] ||
!program['input'] ||
!program['description'] ||
!program['category'] ||
!program['licence'] ||
!program['tags']
) {
throw new Error('All arguments are required.')
}
if (isAbsolute(program['input']) === false) {
throw new Error('Input path should be absolute.')
}
let command = `npm run ts-node -- ${__dirname}/get-access-token.ts`
command += ` -u "${program['url']}"`
command += ` -n "${program['username']}"`
command += ` -p "${program['password']}"`
execCLI(command)
.then(stdout => {
const accessToken = stdout.replace('\n', '')
console.log(accessToken)
return readdirPromise(program['input']).then(files => ({ accessToken, files }))
})
.then(({ accessToken, files }) => {
return Promise.each(files, file => {
const video = {
tags: program['tags'],
name: file,
description: program['description'],
category: program['category'],
licence: program['licence']
}
let command = `npm run ts-node -- ${__dirname}/upload.ts`
command += ` -u "${program['url']}"`
command += ` -a "${accessToken}"`
command += ` -n "${video.name}"`
command += ` -d "${video.description}"`
command += ` -c "${video.category}"`
command += ` -l "${video.licence}"`
command += ` -t "${video.tags.join(',')}"`
command += ` -f "${join(program['input'], file)}"`
return execCLI(command).then(stdout => console.log(stdout))
})
})
.then(() => process.exit(0))
.catch(err => {
console.error(err)
process.exit(-1)
})
// ----------------------------------------------------------------------------
function list (val) {
return val.split(',')
}

View File

@ -2,84 +2,87 @@ import * as program from 'commander'
import { access, constants } from 'fs' import { access, constants } from 'fs'
import { isAbsolute } from 'path' import { isAbsolute } from 'path'
import { promisify } from 'util' import { promisify } from 'util'
import { getClient, login } from '../tests/utils'
import { uploadVideo } from '../tests/utils/index'
const accessPromise = promisify(access) const accessPromise = promisify(access)
import { uploadVideo } from '../tests/utils/index'
program program
.option('-u, --url <url>', 'Server url') .option('-u, --url <url>', 'Server url')
.option('-a, --access-token <token>', 'Access token') .option('-U, --username <username>', 'Username')
.option('-n, --name <name>', 'Video name') .option('-p, --password <token>', 'Password')
.option('-n, --video-name <name>', 'Video name')
.option('-N, --nsfw', 'Video is Not Safe For Work') .option('-N, --nsfw', 'Video is Not Safe For Work')
.option('-c, --category <category number>', 'Category number') .option('-c, --category <category number>', 'Category number')
.option('-m, --comments-enabled', 'Enable comments')
.option('-l, --licence <licence number>', 'Licence number') .option('-l, --licence <licence number>', 'Licence number')
.option('-L, --language <language number>', 'Language number') .option('-L, --language <language number>', 'Language number')
.option('-d, --description <description>', 'Video description') .option('-d, --video-description <description>', 'Video description')
.option('-t, --tags <tags>', 'Video tags', list) .option('-t, --tags <tags>', 'Video tags', list)
.option('-f, --file <file>', 'Video absolute file path') .option('-f, --file <file>', 'Video absolute file path')
.parse(process.argv) .parse(process.argv)
if (!program['tags']) program['tags'] = [] if (!program['tags']) program['tags'] = []
if (!program['nsfw']) program['nsfw'] = false if (!program['nsfw']) program['nsfw'] = false
if (!program['commentsEnabled']) program['commentsEnabled'] = false
if ( if (
!program['url'] || !program['url'] ||
!program['accessToken'] || !program['username'] ||
!program['name'] || !program['password'] ||
!program['category'] || !program['videoName'] ||
!program['licence'] ||
!program['description'] ||
!program['file'] !program['file']
) { ) {
throw new Error('All arguments but tags, language and nsfw are required.') console.error('Url, username, password, name and input file are required.')
process.exit(-1)
} }
if (isAbsolute(program['file']) === false) { if (isAbsolute(program['file']) === false) {
throw new Error('File path should be absolute.') console.error('File path should be absolute.')
process.exit(-1)
} }
accessPromise(program['file'], constants.F_OK) run().catch(err => console.error(err))
.then(() => {
return upload( async function run () {
program['url'], const res = await getClient(program[ 'url' ])
program['accessToken'], const client = {
program['name'], id: res.body.client_id,
program['category'], secret: res.body.client_secret
program['licence'], }
program['language'],
program['nsfw'], const user = {
program['description'], username: program[ 'username' ],
program['tags'], password: program[ 'password' ]
program['file'] }
)
}) const res2 = await login(program[ 'url' ], client, user)
.then(() => process.exit(0)) const accessToken = res2.body.access_token
.catch(err => {
console.error(err) await accessPromise(program[ 'file' ], constants.F_OK)
process.exit(-1)
}) console.log('Uploading %s video...', program[ 'videoName' ])
const videoAttributes = {
name: program['videoName'],
category: program['category'],
licence: program['licence'],
language: program['language'],
nsfw: program['nsfw'],
description: program['videoDescription'],
tags: program['tags'],
commentsEnabled: program['commentsEnabled'],
fixture: program['file']
}
await uploadVideo(program['url'], accessToken, videoAttributes)
console.log(`Video ${program['videoName']} uploaded.`)
process.exit(0)
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
function list (val) { function list (val) {
return val.split(',') return val.split(',')
} }
function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) {
console.log('Uploading %s video...', program['name'])
const videoAttributes = {
name,
category,
licence,
language,
nsfw,
description,
tags,
fixture
}
return uploadVideo(url, accessToken, videoAttributes).then(() => {
console.log(`Video ${name} uploaded.`)
})
}