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('-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('-m, --comments-enabled', 'Enable comments')
19 .option('-l, --licence <licence number>', 'Licence number')
20 .option('-L, --language <language number>', 'Language number')
21 .option('-d, --video-description <description>', 'Video description')
22 .option('-t, --tags <tags>', 'Video tags', list)
23 .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path')
24 .option('-v, --preview <previewPath>', 'Preview path')
25 .option('-f, --file <file>', 'Video absolute file path')
28 if (!program['tags']) program['tags'] = []
29 if (!program['nsfw']) program['nsfw'] = false
30 if (!program['commentsEnabled']) program['commentsEnabled'] = false
34 !program['username'] ||
35 !program['password'] ||
36 !program['videoName'] ||
39 if (!program['url']) console.error('--url field is required.')
40 if (!program['username']) console.error('--username field is required.')
41 if (!program['password']) console.error('--password field is required.')
42 if (!program['videoName']) console.error('--video-name field is required.')
43 if (!program['file']) console.error('--file field is required.')
47 if (isAbsolute(program['file']) === false) {
48 console.error('File path should be absolute.')
52 run().catch(err => console.error(err))
54 async function run () {
55 const res = await getClient(program[ 'url' ])
57 id: res.body.client_id,
58 secret: res.body.client_secret
62 username: program[ 'username' ],
63 password: program[ 'password' ]
66 const res2 = await login(program[ 'url' ], client, user)
67 const accessToken = res2.body.access_token
69 await accessPromise(program[ 'file' ], constants.F_OK)
71 console.log('Uploading %s video...', program[ 'videoName' ])
73 const videoAttributes = {
74 name: program['videoName'],
75 category: program['category'],
76 licence: program['licence'],
77 language: program['language'],
78 nsfw: program['nsfw'],
79 description: program['videoDescription'],
80 tags: program['tags'],
81 commentsEnabled: program['commentsEnabled'],
82 fixture: program['file'],
83 thumbnailfile: program['thumbnailPath'],
84 previewfile: program['previewPath'],
85 privacy: program['privacy'],
89 await uploadVideo(program['url'], accessToken, videoAttributes)
91 console.log(`Video ${program['videoName']} uploaded.`)
95 // ----------------------------------------------------------------------------