]>
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') | |
320f8d7e | 15 | .option('-P, --privacy <privacy number>', 'Privacy') |
8df87ce7 C |
16 | .option('-N, --nsfw', 'Video is Not Safe For Work') |
17 | .option('-c, --category <category number>', 'Category number') | |
a87d467a | 18 | .option('-m, --comments-enabled', 'Enable comments') |
8df87ce7 C |
19 | .option('-l, --licence <licence number>', 'Licence number') |
20 | .option('-L, --language <language number>', 'Language number') | |
a87d467a | 21 | .option('-d, --video-description <description>', 'Video description') |
8df87ce7 | 22 | .option('-t, --tags <tags>', 'Video tags', list) |
2422c46b | 23 | .option('-b, --thumbnail <thumbnailPath>', 'Thumbnail path') |
02988fdc | 24 | .option('-v, --preview <previewPath>', 'Preview path') |
8df87ce7 C |
25 | .option('-f, --file <file>', 'Video absolute file path') |
26 | .parse(process.argv) | |
27 | ||
28 | if (!program['tags']) program['tags'] = [] | |
29 | if (!program['nsfw']) program['nsfw'] = false | |
a87d467a | 30 | if (!program['commentsEnabled']) program['commentsEnabled'] = false |
8df87ce7 C |
31 | |
32 | if ( | |
33 | !program['url'] || | |
a87d467a C |
34 | !program['username'] || |
35 | !program['password'] || | |
36 | !program['videoName'] || | |
8df87ce7 C |
37 | !program['file'] |
38 | ) { | |
48e35415 RK |
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.') | |
a87d467a | 44 | process.exit(-1) |
8df87ce7 C |
45 | } |
46 | ||
47 | if (isAbsolute(program['file']) === false) { | |
a87d467a C |
48 | console.error('File path should be absolute.') |
49 | process.exit(-1) | |
8df87ce7 C |
50 | } |
51 | ||
a87d467a | 52 | run().catch(err => console.error(err)) |
8df87ce7 | 53 | |
a87d467a C |
54 | async function run () { |
55 | const res = await getClient(program[ 'url' ]) | |
56 | const client = { | |
57 | id: res.body.client_id, | |
58 | secret: res.body.client_secret | |
59 | } | |
8df87ce7 | 60 | |
a87d467a C |
61 | const user = { |
62 | username: program[ 'username' ], | |
63 | password: program[ 'password' ] | |
64 | } | |
65 | ||
66 | const res2 = await login(program[ 'url' ], client, user) | |
67 | const accessToken = res2.body.access_token | |
8df87ce7 | 68 | |
a87d467a C |
69 | await accessPromise(program[ 'file' ], constants.F_OK) |
70 | ||
71 | console.log('Uploading %s video...', program[ 'videoName' ]) | |
8df87ce7 C |
72 | |
73 | const videoAttributes = { | |
a87d467a C |
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'], | |
2422c46b | 82 | fixture: program['file'], |
02988fdc | 83 | thumbnailfile: program['thumbnailPath'], |
ad34cfc8 | 84 | previewfile: program['previewPath'], |
320f8d7e | 85 | privacy: program['privacy'], |
ad34cfc8 | 86 | support: undefined |
8df87ce7 | 87 | } |
a87d467a C |
88 | |
89 | await uploadVideo(program['url'], accessToken, videoAttributes) | |
90 | ||
91 | console.log(`Video ${program['videoName']} uploaded.`) | |
92 | process.exit(0) | |
93 | } | |
94 | ||
95 | // ---------------------------------------------------------------------------- | |
96 | ||
97 | function list (val) { | |
98 | return val.split(',') | |
8df87ce7 | 99 | } |