diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-15 10:02:54 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-20 15:27:18 +0200 |
commit | d23dd9fbfc4d26026352c10f81d2795ceaf2908a (patch) | |
tree | da82286d423c5e834a1ee2dcd5970076b8263cf1 /server/tools | |
parent | 7926c5f9b3ffcabb1ffb0dcfa5e48b8e0b88fbc0 (diff) | |
download | PeerTube-d23dd9fbfc4d26026352c10f81d2795ceaf2908a.tar.gz PeerTube-d23dd9fbfc4d26026352c10f81d2795ceaf2908a.tar.zst PeerTube-d23dd9fbfc4d26026352c10f81d2795ceaf2908a.zip |
Introduce videos command
Diffstat (limited to 'server/tools')
-rw-r--r-- | server/tools/cli.ts | 2 | ||||
-rw-r--r-- | server/tools/peertube-import-videos.ts | 29 | ||||
-rw-r--r-- | server/tools/peertube-upload.ts | 11 |
3 files changed, 22 insertions, 20 deletions
diff --git a/server/tools/cli.ts b/server/tools/cli.ts index 17c2e8c74..163ed62d1 100644 --- a/server/tools/cli.ts +++ b/server/tools/cli.ts | |||
@@ -16,7 +16,7 @@ const version = require('../../../package.json').version | |||
16 | 16 | ||
17 | async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { | 17 | async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { |
18 | const token = await server.loginCommand.getAccessToken(username, password) | 18 | const token = await server.loginCommand.getAccessToken(username, password) |
19 | const me = await server.usersCommand.getMyUserInformation({ token }) | 19 | const me = await server.usersCommand.getMyInfo({ token }) |
20 | 20 | ||
21 | if (me.role !== UserRole.ADMINISTRATOR) { | 21 | if (me.role !== UserRole.ADMINISTRATOR) { |
22 | console.error('You must be an administrator.') | 22 | console.error('You must be an administrator.') |
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index 0a4d6fa6e..fc76735b9 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts | |||
@@ -9,7 +9,6 @@ import { join } from 'path' | |||
9 | import * as prompt from 'prompt' | 9 | import * as prompt from 'prompt' |
10 | import { promisify } from 'util' | 10 | import { promisify } from 'util' |
11 | import { YoutubeDL } from '@server/helpers/youtube-dl' | 11 | import { YoutubeDL } from '@server/helpers/youtube-dl' |
12 | import { getVideoCategories, uploadVideo } from '../../shared/extra-utils/index' | ||
13 | import { sha256 } from '../helpers/core-utils' | 12 | import { sha256 } from '../helpers/core-utils' |
14 | import { doRequestAndSaveToFile } from '../helpers/requests' | 13 | import { doRequestAndSaveToFile } from '../helpers/requests' |
15 | import { CONSTRAINTS_FIELDS } from '../initializers/constants' | 14 | import { CONSTRAINTS_FIELDS } from '../initializers/constants' |
@@ -21,6 +20,7 @@ import { | |||
21 | getLogger, | 20 | getLogger, |
22 | getServerCredentials | 21 | getServerCredentials |
23 | } from './cli' | 22 | } from './cli' |
23 | import { ServerInfo } from '@shared/extra-utils' | ||
24 | 24 | ||
25 | const processOptions = { | 25 | const processOptions = { |
26 | maxBuffer: Infinity | 26 | maxBuffer: Infinity |
@@ -200,7 +200,10 @@ async function uploadVideoOnPeerTube (parameters: { | |||
200 | }) { | 200 | }) { |
201 | const { youtubeDL, videoInfo, videoPath, cwd, url, username, password } = parameters | 201 | const { youtubeDL, videoInfo, videoPath, cwd, url, username, password } = parameters |
202 | 202 | ||
203 | const category = await getCategory(videoInfo.categories, url) | 203 | const server = buildServer(url) |
204 | await assignToken(server, username, password) | ||
205 | |||
206 | const category = await getCategory(server, videoInfo.categories) | ||
204 | const licence = getLicence(videoInfo.license) | 207 | const licence = getLicence(videoInfo.license) |
205 | let tags = [] | 208 | let tags = [] |
206 | if (Array.isArray(videoInfo.tags)) { | 209 | if (Array.isArray(videoInfo.tags)) { |
@@ -232,29 +235,28 @@ async function uploadVideoOnPeerTube (parameters: { | |||
232 | tags | 235 | tags |
233 | } | 236 | } |
234 | 237 | ||
235 | const server = buildServer(url) | 238 | const baseAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) |
236 | await assignToken(server, username, password) | ||
237 | 239 | ||
238 | const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) | 240 | const attributes = { |
241 | ...baseAttributes, | ||
239 | 242 | ||
240 | Object.assign(videoAttributes, { | ||
241 | originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, | 243 | originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, |
242 | thumbnailfile, | 244 | thumbnailfile, |
243 | previewfile: thumbnailfile, | 245 | previewfile: thumbnailfile, |
244 | fixture: videoPath | 246 | fixture: videoPath |
245 | }) | 247 | } |
246 | 248 | ||
247 | log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) | 249 | log.info('\nUploading on PeerTube video "%s".', attributes.name) |
248 | 250 | ||
249 | try { | 251 | try { |
250 | await uploadVideo(url, server.accessToken, videoAttributes) | 252 | await server.videosCommand.upload({ attributes }) |
251 | } catch (err) { | 253 | } catch (err) { |
252 | if (err.message.indexOf('401') !== -1) { | 254 | if (err.message.indexOf('401') !== -1) { |
253 | log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') | 255 | log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') |
254 | 256 | ||
255 | server.accessToken = await server.loginCommand.getAccessToken(username, password) | 257 | server.accessToken = await server.loginCommand.getAccessToken(username, password) |
256 | 258 | ||
257 | await uploadVideo(url, server.accessToken, videoAttributes) | 259 | await server.videosCommand.upload({ attributes }) |
258 | } else { | 260 | } else { |
259 | exitError(err.message) | 261 | exitError(err.message) |
260 | } | 262 | } |
@@ -263,20 +265,19 @@ async function uploadVideoOnPeerTube (parameters: { | |||
263 | await remove(videoPath) | 265 | await remove(videoPath) |
264 | if (thumbnailfile) await remove(thumbnailfile) | 266 | if (thumbnailfile) await remove(thumbnailfile) |
265 | 267 | ||
266 | log.warn('Uploaded video "%s"!\n', videoAttributes.name) | 268 | log.warn('Uploaded video "%s"!\n', attributes.name) |
267 | } | 269 | } |
268 | 270 | ||
269 | /* ---------------------------------------------------------- */ | 271 | /* ---------------------------------------------------------- */ |
270 | 272 | ||
271 | async function getCategory (categories: string[], url: string) { | 273 | async function getCategory (server: ServerInfo, categories: string[]) { |
272 | if (!categories) return undefined | 274 | if (!categories) return undefined |
273 | 275 | ||
274 | const categoryString = categories[0] | 276 | const categoryString = categories[0] |
275 | 277 | ||
276 | if (categoryString === 'News & Politics') return 11 | 278 | if (categoryString === 'News & Politics') return 11 |
277 | 279 | ||
278 | const res = await getVideoCategories(url) | 280 | const categoriesServer = await server.videosCommand.getCategories() |
279 | const categoriesServer = res.body | ||
280 | 281 | ||
281 | for (const key of Object.keys(categoriesServer)) { | 282 | for (const key of Object.keys(categoriesServer)) { |
282 | const categoryServer = categoriesServer[key] | 283 | const categoryServer = categoriesServer[key] |
diff --git a/server/tools/peertube-upload.ts b/server/tools/peertube-upload.ts index c94b05857..597137e4c 100644 --- a/server/tools/peertube-upload.ts +++ b/server/tools/peertube-upload.ts | |||
@@ -4,7 +4,6 @@ registerTSPaths() | |||
4 | import { program } from 'commander' | 4 | import { program } from 'commander' |
5 | import { access, constants } from 'fs-extra' | 5 | import { access, constants } from 'fs-extra' |
6 | import { isAbsolute } from 'path' | 6 | import { isAbsolute } from 'path' |
7 | import { uploadVideo } from '../../shared/extra-utils/' | ||
8 | import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' | 7 | import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' |
9 | 8 | ||
10 | let command = program | 9 | let command = program |
@@ -52,16 +51,18 @@ async function run (url: string, username: string, password: string) { | |||
52 | 51 | ||
53 | console.log('Uploading %s video...', options.videoName) | 52 | console.log('Uploading %s video...', options.videoName) |
54 | 53 | ||
55 | const videoAttributes = await buildVideoAttributesFromCommander(server, program) | 54 | const baseAttributes = await buildVideoAttributesFromCommander(server, program) |
55 | |||
56 | const attributes = { | ||
57 | ...baseAttributes, | ||
56 | 58 | ||
57 | Object.assign(videoAttributes, { | ||
58 | fixture: options.file, | 59 | fixture: options.file, |
59 | thumbnailfile: options.thumbnail, | 60 | thumbnailfile: options.thumbnail, |
60 | previewfile: options.preview | 61 | previewfile: options.preview |
61 | }) | 62 | } |
62 | 63 | ||
63 | try { | 64 | try { |
64 | await uploadVideo(url, server.accessToken, videoAttributes) | 65 | await server.videosCommand.upload({ attributes }) |
65 | console.log(`Video ${options.videoName} uploaded.`) | 66 | console.log(`Video ${options.videoName} uploaded.`) |
66 | process.exit(0) | 67 | process.exit(0) |
67 | } catch (err) { | 68 | } catch (err) { |