diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2018-09-13 14:27:44 +0200 |
---|---|---|
committer | Rigel Kent <sendmemail@rigelk.eu> | 2018-09-14 11:08:55 +0200 |
commit | 8704acf49efc770d73bf07c10468ed8c74d28a83 (patch) | |
tree | ffd46289fcf9a13ac4412b167e9f71dfb35753c5 /server/tools/peertube-upload.ts | |
parent | 1d9d9cfdcf3983e3fd89026bc4b5633a8abf5752 (diff) | |
download | PeerTube-8704acf49efc770d73bf07c10468ed8c74d28a83.tar.gz PeerTube-8704acf49efc770d73bf07c10468ed8c74d28a83.tar.zst PeerTube-8704acf49efc770d73bf07c10468ed8c74d28a83.zip |
one cli to unite them all
Ash nazg thrakatulûk agh burzum-ishi krimpatul
- refactor import-videos to use the youtubeDL helper
- add very basic tests for the cli
Diffstat (limited to 'server/tools/peertube-upload.ts')
-rw-r--r-- | server/tools/peertube-upload.ts | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts new file mode 100644 index 000000000..1f871e660 --- /dev/null +++ b/server/tools/peertube-upload.ts | |||
@@ -0,0 +1,127 @@ | |||
1 | import * as program from 'commander' | ||
2 | import { access, constants } from 'fs-extra' | ||
3 | import { isAbsolute } from 'path' | ||
4 | import { getClient, login } from '../tests/utils' | ||
5 | import { uploadVideo } from '../tests/utils/index' | ||
6 | import { VideoPrivacy } from '../../shared/models/videos' | ||
7 | import { netrc, getSettings } from './cli' | ||
8 | |||
9 | program | ||
10 | .name('upload') | ||
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_code>', 'Language ISO 639 code (fr or en...)') | ||
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') | ||
26 | .parse(process.argv) | ||
27 | |||
28 | if (!program['tags']) program['tags'] = [] | ||
29 | if (!program['nsfw']) program['nsfw'] = false | ||
30 | if (!program['privacy']) program['privacy'] = VideoPrivacy.PUBLIC | ||
31 | if (!program['commentsEnabled']) program['commentsEnabled'] = false | ||
32 | |||
33 | getSettings() | ||
34 | .then(settings => { | ||
35 | if ( | ||
36 | (!program['url'] || | ||
37 | !program['username'] || | ||
38 | !program['password']) && | ||
39 | (settings.remotes.length === 0) | ||
40 | ) { | ||
41 | if (!program['url']) console.error('--url field is required.') | ||
42 | if (!program['username']) console.error('--username field is required.') | ||
43 | if (!program['password']) console.error('--password field is required.') | ||
44 | if (!program['videoName']) console.error('--video-name field is required.') | ||
45 | if (!program['file']) console.error('--file field is required.') | ||
46 | process.exit(-1) | ||
47 | } | ||
48 | |||
49 | if ( | ||
50 | (!program['url'] || | ||
51 | !program['username'] || | ||
52 | !program['password']) && | ||
53 | (settings.remotes.length > 0) | ||
54 | ) { | ||
55 | if (!program['url']) { | ||
56 | program['url'] = (settings.default !== -1) ? | ||
57 | settings.remotes[settings.default] : | ||
58 | settings.remotes[0] | ||
59 | } | ||
60 | if (!program['username']) program['username'] = netrc.machines[program['url']].login | ||
61 | if (!program['password']) program['password'] = netrc.machines[program['url']].password | ||
62 | } | ||
63 | |||
64 | if ( | ||
65 | !program['videoName'] || | ||
66 | !program['file'] | ||
67 | ) { | ||
68 | if (!program['videoName']) console.error('--video-name field is required.') | ||
69 | if (!program['file']) console.error('--file field is required.') | ||
70 | process.exit(-1) | ||
71 | } | ||
72 | |||
73 | if (isAbsolute(program['file']) === false) { | ||
74 | console.error('File path should be absolute.') | ||
75 | process.exit(-1) | ||
76 | } | ||
77 | |||
78 | run().catch(err => console.error(err)) | ||
79 | }) | ||
80 | |||
81 | async function run () { | ||
82 | const res = await getClient(program[ 'url' ]) | ||
83 | const client = { | ||
84 | id: res.body.client_id, | ||
85 | secret: res.body.client_secret | ||
86 | } | ||
87 | |||
88 | const user = { | ||
89 | username: program[ 'username' ], | ||
90 | password: program[ 'password' ] | ||
91 | } | ||
92 | |||
93 | const res2 = await login(program[ 'url' ], client, user) | ||
94 | const accessToken = res2.body.access_token | ||
95 | |||
96 | await access(program[ 'file' ], constants.F_OK) | ||
97 | |||
98 | console.log('Uploading %s video...', program[ 'videoName' ]) | ||
99 | |||
100 | const videoAttributes = { | ||
101 | name: program['videoName'], | ||
102 | category: program['category'], | ||
103 | licence: program['licence'], | ||
104 | language: program['language'], | ||
105 | nsfw: program['nsfw'], | ||
106 | description: program['videoDescription'], | ||
107 | tags: program['tags'], | ||
108 | commentsEnabled: program['commentsEnabled'], | ||
109 | fixture: program['file'], | ||
110 | thumbnailfile: program['thumbnail'], | ||
111 | previewfile: program['preview'], | ||
112 | waitTranscoding: true, | ||
113 | privacy: program['privacy'], | ||
114 | support: undefined | ||
115 | } | ||
116 | |||
117 | await uploadVideo(program['url'], accessToken, videoAttributes) | ||
118 | |||
119 | console.log(`Video ${program['videoName']} uploaded.`) | ||
120 | process.exit(0) | ||
121 | } | ||
122 | |||
123 | // ---------------------------------------------------------------------------- | ||
124 | |||
125 | function list (val) { | ||
126 | return val.split(',') | ||
127 | } | ||