]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/tools/peertube-import-videos.ts
Fix user subscription follows count
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-import-videos.ts
index 0a4d6fa6e6fcf572e5eeb792098ec6fdc098a9fd..758b561e1b959c4b3745fae55026c971fde5b942 100644 (file)
@@ -6,10 +6,8 @@ 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'
@@ -21,6 +19,9 @@ import {
   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
   }
 
-  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.videos.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)
+      server.accessToken = await server.login.getAccessToken(username, password)
 
-      await uploadVideo(url, server.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<any>((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)