]>
Commit | Line | Data |
---|---|---|
8df87ce7 C |
1 | import * as program from 'commander' |
2 | import { access, constants } from 'fs' | |
3 | import { isAbsolute } from 'path' | |
4 | import { promisify } from 'util' | |
a87d467a C |
5 | import { getClient, login } from '../tests/utils' |
6 | import { uploadVideo } from '../tests/utils/index' | |
8df87ce7 C |
7 | |
8 | const accessPromise = promisify(access) | |
9 | ||
8df87ce7 C |
10 | program |
11 | .option('-u, --url <url>', 'Server url') | |
a87d467a C |
12 | .option('-U, --username <username>', 'Username') |
13 | .option('-p, --password <token>', 'Password') | |
14 | .option('-n, --video-name <name>', 'Video name') | |
8df87ce7 C |
15 | .option('-N, --nsfw', 'Video is Not Safe For Work') |
16 | .option('-c, --category <category number>', 'Category number') | |
a87d467a | 17 | .option('-m, --comments-enabled', 'Enable comments') |
8df87ce7 C |
18 | .option('-l, --licence <licence number>', 'Licence number') |
19 | .option('-L, --language <language number>', 'Language number') | |
a87d467a | 20 | .option('-d, --video-description <description>', 'Video description') |
8df87ce7 | 21 | .option('-t, --tags <tags>', 'Video tags', list) |
2422c46b | 22 | .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path') |
02988fdc | 23 | .option('-v, --preview <previewPath>', 'Preview path') |
8df87ce7 C |
24 | .option('-f, --file <file>', 'Video absolute file path') |
25 | .parse(process.argv) | |
26 | ||
27 | if (!program['tags']) program['tags'] = [] | |
28 | if (!program['nsfw']) program['nsfw'] = false | |
a87d467a | 29 | if (!program['commentsEnabled']) program['commentsEnabled'] = false |
8df87ce7 C |
30 | |
31 | if ( | |
32 | !program['url'] || | |
a87d467a C |
33 | !program['username'] || |
34 | !program['password'] || | |
35 | !program['videoName'] || | |
8df87ce7 C |
36 | !program['file'] |
37 | ) { | |
a87d467a C |
38 | console.error('Url, username, password, name and input file are required.') |
39 | process.exit(-1) | |
8df87ce7 C |
40 | } |
41 | ||
42 | if (isAbsolute(program['file']) === false) { | |
a87d467a C |
43 | console.error('File path should be absolute.') |
44 | process.exit(-1) | |
8df87ce7 C |
45 | } |
46 | ||
a87d467a | 47 | run().catch(err => console.error(err)) |
8df87ce7 | 48 | |
a87d467a C |
49 | async function run () { |
50 | const res = await getClient(program[ 'url' ]) | |
51 | const client = { | |
52 | id: res.body.client_id, | |
53 | secret: res.body.client_secret | |
54 | } | |
8df87ce7 | 55 | |
a87d467a C |
56 | const user = { |
57 | username: program[ 'username' ], | |
58 | password: program[ 'password' ] | |
59 | } | |
60 | ||
61 | const res2 = await login(program[ 'url' ], client, user) | |
62 | const accessToken = res2.body.access_token | |
8df87ce7 | 63 | |
a87d467a C |
64 | await accessPromise(program[ 'file' ], constants.F_OK) |
65 | ||
66 | console.log('Uploading %s video...', program[ 'videoName' ]) | |
8df87ce7 C |
67 | |
68 | const videoAttributes = { | |
a87d467a C |
69 | name: program['videoName'], |
70 | category: program['category'], | |
71 | licence: program['licence'], | |
72 | language: program['language'], | |
73 | nsfw: program['nsfw'], | |
74 | description: program['videoDescription'], | |
75 | tags: program['tags'], | |
76 | commentsEnabled: program['commentsEnabled'], | |
2422c46b | 77 | fixture: program['file'], |
02988fdc C |
78 | thumbnailfile: program['thumbnailPath'], |
79 | previewfile: program['previewPath'] | |
8df87ce7 | 80 | } |
a87d467a C |
81 | |
82 | await uploadVideo(program['url'], accessToken, videoAttributes) | |
83 | ||
84 | console.log(`Video ${program['videoName']} uploaded.`) | |
85 | process.exit(0) | |
86 | } | |
87 | ||
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | function list (val) { | |
91 | return val.split(',') | |
8df87ce7 | 92 | } |