From d23dd9fbfc4d26026352c10f81d2795ceaf2908a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Jul 2021 10:02:54 +0200 Subject: Introduce videos command --- server/tools/cli.ts | 2 +- server/tools/peertube-import-videos.ts | 29 +++++++++++++++-------------- server/tools/peertube-upload.ts | 11 ++++++----- 3 files changed, 22 insertions(+), 20 deletions(-) (limited to 'server/tools') 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 async function getAdminTokenOrDie (server: ServerInfo, username: string, password: string) { const token = await server.loginCommand.getAccessToken(username, password) - const me = await server.usersCommand.getMyUserInformation({ token }) + const me = await server.usersCommand.getMyInfo({ token }) if (me.role !== UserRole.ADMINISTRATOR) { 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' import * as prompt from 'prompt' import { promisify } from 'util' import { YoutubeDL } from '@server/helpers/youtube-dl' -import { getVideoCategories, uploadVideo } from '../../shared/extra-utils/index' import { sha256 } from '../helpers/core-utils' import { doRequestAndSaveToFile } from '../helpers/requests' import { CONSTRAINTS_FIELDS } from '../initializers/constants' @@ -21,6 +20,7 @@ import { getLogger, getServerCredentials } from './cli' +import { ServerInfo } from '@shared/extra-utils' const processOptions = { maxBuffer: Infinity @@ -200,7 +200,10 @@ async function uploadVideoOnPeerTube (parameters: { }) { const { youtubeDL, videoInfo, videoPath, cwd, url, username, password } = parameters - const category = await getCategory(videoInfo.categories, url) + const server = buildServer(url) + await assignToken(server, username, password) + + const category = await getCategory(server, videoInfo.categories) const licence = getLicence(videoInfo.license) let tags = [] if (Array.isArray(videoInfo.tags)) { @@ -232,29 +235,28 @@ async function uploadVideoOnPeerTube (parameters: { tags } - const server = buildServer(url) - await assignToken(server, username, password) + const baseAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) - const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) + const attributes = { + ...baseAttributes, - Object.assign(videoAttributes, { originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, thumbnailfile, previewfile: thumbnailfile, fixture: videoPath - }) + } - log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) + log.info('\nUploading on PeerTube video "%s".', attributes.name) try { - await uploadVideo(url, server.accessToken, videoAttributes) + await server.videosCommand.upload({ attributes }) } catch (err) { if (err.message.indexOf('401') !== -1) { log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') server.accessToken = await server.loginCommand.getAccessToken(username, password) - await uploadVideo(url, server.accessToken, videoAttributes) + await server.videosCommand.upload({ attributes }) } else { exitError(err.message) } @@ -263,20 +265,19 @@ async function uploadVideoOnPeerTube (parameters: { await remove(videoPath) if (thumbnailfile) await remove(thumbnailfile) - log.warn('Uploaded video "%s"!\n', videoAttributes.name) + log.warn('Uploaded video "%s"!\n', attributes.name) } /* ---------------------------------------------------------- */ -async function getCategory (categories: string[], url: string) { +async function getCategory (server: ServerInfo, categories: string[]) { if (!categories) return undefined const categoryString = categories[0] if (categoryString === 'News & Politics') return 11 - const res = await getVideoCategories(url) - const categoriesServer = res.body + const categoriesServer = await server.videosCommand.getCategories() for (const key of Object.keys(categoriesServer)) { 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() import { program } from 'commander' import { access, constants } from 'fs-extra' import { isAbsolute } from 'path' -import { uploadVideo } from '../../shared/extra-utils/' import { assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, getServerCredentials } from './cli' let command = program @@ -52,16 +51,18 @@ async function run (url: string, username: string, password: string) { console.log('Uploading %s video...', options.videoName) - const videoAttributes = await buildVideoAttributesFromCommander(server, program) + const baseAttributes = await buildVideoAttributesFromCommander(server, program) + + const attributes = { + ...baseAttributes, - Object.assign(videoAttributes, { fixture: options.file, thumbnailfile: options.thumbnail, previewfile: options.preview - }) + } try { - await uploadVideo(url, server.accessToken, videoAttributes) + await server.videosCommand.upload({ attributes }) console.log(`Video ${options.videoName} uploaded.`) process.exit(0) } catch (err) { -- cgit v1.2.3