diff options
Diffstat (limited to 'server/tools/peertube-import-videos.ts')
-rw-r--r-- | server/tools/peertube-import-videos.ts | 29 |
1 files changed, 15 insertions, 14 deletions
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] |