1 import * as program from 'commander'
2 import { access, constants } from 'fs'
3 import { isAbsolute } from 'path'
4 import { promisify } from 'util'
5 import { getClient, login } from '../tests/utils'
6 import { uploadVideo } from '../tests/utils/index'
8 const accessPromise = promisify(access)
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('-N, --nsfw', 'Video is Not Safe For Work')
16 .option('-c, --category <category number>', 'Category number')
17 .option('-m, --comments-enabled', 'Enable comments')
18 .option('-l, --licence <licence number>', 'Licence number')
19 .option('-L, --language <language number>', 'Language number')
20 .option('-d, --video-description <description>', 'Video description')
21 .option('-t, --tags <tags>', 'Video tags', list)
22 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
23 .option('-f, --file <file>', 'Video absolute file path')
26 if (!program['tags']) program['tags'] = []
27 if (!program['nsfw']) program['nsfw'] = false
28 if (!program['commentsEnabled']) program['commentsEnabled'] = false
32 !program['username'] ||
33 !program['password'] ||
34 !program['videoName'] ||
37 console.error('Url, username, password, name and input file are required.')
41 if (isAbsolute(program['file']) === false) {
42 console.error('File path should be absolute.')
46 run().catch(err => console.error(err))
48 async function run () {
49 const res = await getClient(program[ 'url' ])
51 id: res.body.client_id,
52 secret: res.body.client_secret
56 username: program[ 'username' ],
57 password: program[ 'password' ]
60 const res2 = await login(program[ 'url' ], client, user)
61 const accessToken = res2.body.access_token
63 await accessPromise(program[ 'file' ], constants.F_OK)
65 console.log('Uploading %s video...', program[ 'videoName' ])
67 const videoAttributes = {
68 name: program['videoName'],
69 category: program['category'],
70 licence: program['licence'],
71 language: program['language'],
72 nsfw: program['nsfw'],
73 description: program['videoDescription'],
74 tags: program['tags'],
75 commentsEnabled: program['commentsEnabled'],
76 fixture: program['file'],
77 thumbnailfile: program['thumbnailPath']
80 await uploadVideo(program['url'], accessToken, videoAttributes)
82 console.log(`Video ${program['videoName']} uploaded.`)
86 // ----------------------------------------------------------------------------