X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftools%2Fpeertube-import-videos.ts;h=758b561e1b959c4b3745fae55026c971fde5b942;hb=e1a570abff3ebf375433e58e7362d56bd32d4cd8;hp=a546a8dbe13f6f152a597519a948537740f58ed2;hpb=078f17e6d90376050f43ce639e88e11869b49ee7;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index a546a8dbe..758b561e1 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts @@ -6,21 +6,22 @@ import { accessSync, constants } from 'fs' import { remove } from 'fs-extra' import { truncate } from 'lodash' 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' import { + assignToken, buildCommonVideoOptions, buildServer, buildVideoAttributesFromCommander, - getAccessTokenOrDie, getLogger, getServerCredentials } from './cli' +import { PeerTubeServer } from '@shared/extra-utils' + +import prompt = require('prompt') const processOptions = { maxBuffer: Infinity @@ -150,7 +151,7 @@ async function processVideo (parameters: { } const server = buildServer(url) - const { data } = await server.searchCommand.advancedVideoSearch({ + const { data } = await server.search.advancedVideoSearch({ search: { search: videoInfo.title, sort: '-match', @@ -200,7 +201,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 +236,28 @@ async function uploadVideoOnPeerTube (parameters: { tags } - let accessToken = await getAccessTokenOrDie(url, username, password) - const server = buildServer(url, accessToken) + 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, accessToken, videoAttributes) + await server.videos.upload({ attributes }) } catch (err) { if (err.message.indexOf('401') !== -1) { log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') - accessToken = await getAccessTokenOrDie(url, username, password) + server.accessToken = await server.login.getAccessToken(username, password) - await uploadVideo(url, accessToken, videoAttributes) + await server.videos.upload({ attributes }) } else { exitError(err.message) } @@ -263,20 +266,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: PeerTubeServer, 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.videos.getCategories() for (const key of Object.keys(categoriesServer)) { const categoryServer = categoriesServer[key] @@ -406,7 +408,7 @@ function getYoutubeDLInfo (youtubeDL: any, url: string, args: string[]) { return new Promise((res, rej) => { const options = [ '-j', '--flat-playlist', '--playlist-reverse', ...args ] - youtubeDL.getInfo(url, options, processOptions, async (err, info) => { + youtubeDL.getInfo(url, options, processOptions, (err, info) => { if (err) return rej(err) return res(info)