diff options
author | Chocobozzz <me@florianbigard.com> | 2018-02-12 12:48:58 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-02-12 12:48:58 +0100 |
commit | a87d467ad76505312fe63c597ee03a66126d0492 (patch) | |
tree | 9368de15c30c59f40fb45db7f7a60efeb4a5987b /server/tools | |
parent | 3df456380a3d6c7ff39b0004a5390011739ebc8e (diff) | |
download | PeerTube-a87d467ad76505312fe63c597ee03a66126d0492.tar.gz PeerTube-a87d467ad76505312fe63c597ee03a66126d0492.tar.zst PeerTube-a87d467ad76505312fe63c597ee03a66126d0492.zip |
Fix upload from CLI script
Diffstat (limited to 'server/tools')
-rw-r--r-- | server/tools/import-youtube.ts | 3 | ||||
-rw-r--r-- | server/tools/upload-directory.ts | 82 | ||||
-rw-r--r-- | server/tools/upload.ts | 101 |
3 files changed, 54 insertions, 132 deletions
diff --git a/server/tools/import-youtube.ts b/server/tools/import-youtube.ts index d57798d5b..96bce29b5 100644 --- a/server/tools/import-youtube.ts +++ b/server/tools/import-youtube.ts | |||
@@ -20,7 +20,8 @@ if ( | |||
20 | !program['password'] || | 20 | !program['password'] || |
21 | !program['youtubeUrl'] | 21 | !program['youtubeUrl'] |
22 | ) { | 22 | ) { |
23 | throw new Error('All arguments are required.') | 23 | console.error('All arguments are required.') |
24 | process.exit(-1) | ||
24 | } | 25 | } |
25 | 26 | ||
26 | run().catch(err => console.error(err)) | 27 | run().catch(err => console.error(err)) |
diff --git a/server/tools/upload-directory.ts b/server/tools/upload-directory.ts deleted file mode 100644 index c0094f852..000000000 --- a/server/tools/upload-directory.ts +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
1 | import * as program from 'commander' | ||
2 | import * as Promise from 'bluebird' | ||
3 | import { isAbsolute, join } from 'path' | ||
4 | |||
5 | import { readdirPromise } from '../helpers/core-utils' | ||
6 | import { execCLI } from '../tests/utils/index' | ||
7 | |||
8 | program | ||
9 | .option('-u, --url <url>', 'Server url') | ||
10 | .option('-U, --username <username>', 'Username') | ||
11 | .option('-p, --password <token>', 'Password') | ||
12 | .option('-i, --input <directory>', 'Videos directory absolute path') | ||
13 | .option('-d, --description <description>', 'Video descriptions') | ||
14 | .option('-c, --category <category>', 'Video categories') | ||
15 | .option('-l, --licence <licence>', 'Video licences') | ||
16 | .option('-t, --tags <tags>', 'Video tags', list) | ||
17 | .parse(process.argv) | ||
18 | |||
19 | if ( | ||
20 | !program['url'] || | ||
21 | !program['username'] || | ||
22 | !program['password'] || | ||
23 | !program['input'] || | ||
24 | !program['description'] || | ||
25 | !program['category'] || | ||
26 | !program['licence'] || | ||
27 | !program['tags'] | ||
28 | ) { | ||
29 | throw new Error('All arguments are required.') | ||
30 | } | ||
31 | |||
32 | if (isAbsolute(program['input']) === false) { | ||
33 | throw new Error('Input path should be absolute.') | ||
34 | } | ||
35 | |||
36 | let command = `npm run ts-node -- ${__dirname}/get-access-token.ts` | ||
37 | command += ` -u "${program['url']}"` | ||
38 | command += ` -n "${program['username']}"` | ||
39 | command += ` -p "${program['password']}"` | ||
40 | |||
41 | execCLI(command) | ||
42 | .then(stdout => { | ||
43 | const accessToken = stdout.replace('\n', '') | ||
44 | |||
45 | console.log(accessToken) | ||
46 | |||
47 | return readdirPromise(program['input']).then(files => ({ accessToken, files })) | ||
48 | }) | ||
49 | .then(({ accessToken, files }) => { | ||
50 | return Promise.each(files, file => { | ||
51 | const video = { | ||
52 | tags: program['tags'], | ||
53 | name: file, | ||
54 | description: program['description'], | ||
55 | category: program['category'], | ||
56 | licence: program['licence'] | ||
57 | } | ||
58 | |||
59 | let command = `npm run ts-node -- ${__dirname}/upload.ts` | ||
60 | command += ` -u "${program['url']}"` | ||
61 | command += ` -a "${accessToken}"` | ||
62 | command += ` -n "${video.name}"` | ||
63 | command += ` -d "${video.description}"` | ||
64 | command += ` -c "${video.category}"` | ||
65 | command += ` -l "${video.licence}"` | ||
66 | command += ` -t "${video.tags.join(',')}"` | ||
67 | command += ` -f "${join(program['input'], file)}"` | ||
68 | |||
69 | return execCLI(command).then(stdout => console.log(stdout)) | ||
70 | }) | ||
71 | }) | ||
72 | .then(() => process.exit(0)) | ||
73 | .catch(err => { | ||
74 | console.error(err) | ||
75 | process.exit(-1) | ||
76 | }) | ||
77 | |||
78 | // ---------------------------------------------------------------------------- | ||
79 | |||
80 | function list (val) { | ||
81 | return val.split(',') | ||
82 | } | ||
diff --git a/server/tools/upload.ts b/server/tools/upload.ts index db59bbdff..3bf9dd65e 100644 --- a/server/tools/upload.ts +++ b/server/tools/upload.ts | |||
@@ -2,84 +2,87 @@ import * as program from 'commander' | |||
2 | import { access, constants } from 'fs' | 2 | import { access, constants } from 'fs' |
3 | import { isAbsolute } from 'path' | 3 | import { isAbsolute } from 'path' |
4 | import { promisify } from 'util' | 4 | import { promisify } from 'util' |
5 | import { getClient, login } from '../tests/utils' | ||
6 | import { uploadVideo } from '../tests/utils/index' | ||
5 | 7 | ||
6 | const accessPromise = promisify(access) | 8 | const accessPromise = promisify(access) |
7 | 9 | ||
8 | import { uploadVideo } from '../tests/utils/index' | ||
9 | |||
10 | program | 10 | program |
11 | .option('-u, --url <url>', 'Server url') | 11 | .option('-u, --url <url>', 'Server url') |
12 | .option('-a, --access-token <token>', 'Access token') | 12 | .option('-U, --username <username>', 'Username') |
13 | .option('-n, --name <name>', 'Video name') | 13 | .option('-p, --password <token>', 'Password') |
14 | .option('-n, --video-name <name>', 'Video name') | ||
14 | .option('-N, --nsfw', 'Video is Not Safe For Work') | 15 | .option('-N, --nsfw', 'Video is Not Safe For Work') |
15 | .option('-c, --category <category number>', 'Category number') | 16 | .option('-c, --category <category number>', 'Category number') |
17 | .option('-m, --comments-enabled', 'Enable comments') | ||
16 | .option('-l, --licence <licence number>', 'Licence number') | 18 | .option('-l, --licence <licence number>', 'Licence number') |
17 | .option('-L, --language <language number>', 'Language number') | 19 | .option('-L, --language <language number>', 'Language number') |
18 | .option('-d, --description <description>', 'Video description') | 20 | .option('-d, --video-description <description>', 'Video description') |
19 | .option('-t, --tags <tags>', 'Video tags', list) | 21 | .option('-t, --tags <tags>', 'Video tags', list) |
20 | .option('-f, --file <file>', 'Video absolute file path') | 22 | .option('-f, --file <file>', 'Video absolute file path') |
21 | .parse(process.argv) | 23 | .parse(process.argv) |
22 | 24 | ||
23 | if (!program['tags']) program['tags'] = [] | 25 | if (!program['tags']) program['tags'] = [] |
24 | if (!program['nsfw']) program['nsfw'] = false | 26 | if (!program['nsfw']) program['nsfw'] = false |
27 | if (!program['commentsEnabled']) program['commentsEnabled'] = false | ||
25 | 28 | ||
26 | if ( | 29 | if ( |
27 | !program['url'] || | 30 | !program['url'] || |
28 | !program['accessToken'] || | 31 | !program['username'] || |
29 | !program['name'] || | 32 | !program['password'] || |
30 | !program['category'] || | 33 | !program['videoName'] || |
31 | !program['licence'] || | ||
32 | !program['description'] || | ||
33 | !program['file'] | 34 | !program['file'] |
34 | ) { | 35 | ) { |
35 | throw new Error('All arguments but tags, language and nsfw are required.') | 36 | console.error('Url, username, password, name and input file are required.') |
37 | process.exit(-1) | ||
36 | } | 38 | } |
37 | 39 | ||
38 | if (isAbsolute(program['file']) === false) { | 40 | if (isAbsolute(program['file']) === false) { |
39 | throw new Error('File path should be absolute.') | 41 | console.error('File path should be absolute.') |
42 | process.exit(-1) | ||
40 | } | 43 | } |
41 | 44 | ||
42 | accessPromise(program['file'], constants.F_OK) | 45 | run().catch(err => console.error(err)) |
43 | .then(() => { | ||
44 | return upload( | ||
45 | program['url'], | ||
46 | program['accessToken'], | ||
47 | program['name'], | ||
48 | program['category'], | ||
49 | program['licence'], | ||
50 | program['language'], | ||
51 | program['nsfw'], | ||
52 | program['description'], | ||
53 | program['tags'], | ||
54 | program['file'] | ||
55 | ) | ||
56 | }) | ||
57 | .then(() => process.exit(0)) | ||
58 | .catch(err => { | ||
59 | console.error(err) | ||
60 | process.exit(-1) | ||
61 | }) | ||
62 | 46 | ||
63 | // ---------------------------------------------------------------------------- | 47 | async function run () { |
48 | const res = await getClient(program[ 'url' ]) | ||
49 | const client = { | ||
50 | id: res.body.client_id, | ||
51 | secret: res.body.client_secret | ||
52 | } | ||
64 | 53 | ||
65 | function list (val) { | 54 | const user = { |
66 | return val.split(',') | 55 | username: program[ 'username' ], |
67 | } | 56 | password: program[ 'password' ] |
57 | } | ||
58 | |||
59 | const res2 = await login(program[ 'url' ], client, user) | ||
60 | const accessToken = res2.body.access_token | ||
68 | 61 | ||
69 | function upload (url, accessToken, name, category, licence, language, nsfw, description, tags, fixture) { | 62 | await accessPromise(program[ 'file' ], constants.F_OK) |
70 | console.log('Uploading %s video...', program['name']) | 63 | |
64 | console.log('Uploading %s video...', program[ 'videoName' ]) | ||
71 | 65 | ||
72 | const videoAttributes = { | 66 | const videoAttributes = { |
73 | name, | 67 | name: program['videoName'], |
74 | category, | 68 | category: program['category'], |
75 | licence, | 69 | licence: program['licence'], |
76 | language, | 70 | language: program['language'], |
77 | nsfw, | 71 | nsfw: program['nsfw'], |
78 | description, | 72 | description: program['videoDescription'], |
79 | tags, | 73 | tags: program['tags'], |
80 | fixture | 74 | commentsEnabled: program['commentsEnabled'], |
75 | fixture: program['file'] | ||
81 | } | 76 | } |
82 | return uploadVideo(url, accessToken, videoAttributes).then(() => { | 77 | |
83 | console.log(`Video ${name} uploaded.`) | 78 | await uploadVideo(program['url'], accessToken, videoAttributes) |
84 | }) | 79 | |
80 | console.log(`Video ${program['videoName']} uploaded.`) | ||
81 | process.exit(0) | ||
82 | } | ||
83 | |||
84 | // ---------------------------------------------------------------------------- | ||
85 | |||
86 | function list (val) { | ||
87 | return val.split(',') | ||
85 | } | 88 | } |