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('-v, --preview <previewPath>', 'Preview path')
24 .option('-f, --file <file>', 'Video absolute file path')
27 if (!program['tags']) program['tags'] = []
28 if (!program['nsfw']) program['nsfw'] = false
29 if (!program['commentsEnabled']) program['commentsEnabled'] = false
33 !program['username'] ||
34 !program['password'] ||
35 !program['videoName'] ||
38 if (!program['url']) console.error('--url field is required.')
39 if (!program['username']) console.error('--username field is required.')
40 if (!program['password']) console.error('--password field is required.')
41 if (!program['videoName']) console.error('--video-name field is required.')
42 if (!program['file']) console.error('--file field is required.')
46 if (isAbsolute(program['file']) === false) {
47 console.error('File path should be absolute.')
51 run().catch(err => console.error(err))
53 async function run () {
54 const res = await getClient(program[ 'url' ])
56 id: res.body.client_id,
57 secret: res.body.client_secret
61 username: program[ 'username' ],
62 password: program[ 'password' ]
65 const res2 = await login(program[ 'url' ], client, user)
66 const accessToken = res2.body.access_token
68 await accessPromise(program[ 'file' ], constants.F_OK)
70 console.log('Uploading %s video...', program[ 'videoName' ])
72 const videoAttributes = {
73 name: program['videoName'],
74 category: program['category'],
75 licence: program['licence'],
76 language: program['language'],
77 nsfw: program['nsfw'],
78 description: program['videoDescription'],
79 tags: program['tags'],
80 commentsEnabled: program['commentsEnabled'],
81 fixture: program['file'],
82 thumbnailfile: program['thumbnailPath'],
83 previewfile: program['previewPath'],
87 await uploadVideo(program['url'], accessToken, videoAttributes)
89 console.log(`Video ${program['videoName']} uploaded.`)
93 // ----------------------------------------------------------------------------