diff options
Diffstat (limited to 'server/tools/peertube-import-videos.ts')
-rw-r--r-- | server/tools/peertube-import-videos.ts | 89 |
1 files changed, 42 insertions, 47 deletions
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index 101a95b2a..a546a8dbe 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts | |||
@@ -8,17 +8,19 @@ import { truncate } from 'lodash' | |||
8 | import { join } from 'path' | 8 | 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 { advancedVideosSearch, getClient, getVideoCategories, login, uploadVideo } from '../../shared/extra-utils/index' | 11 | import { YoutubeDL } from '@server/helpers/youtube-dl' |
12 | import { getVideoCategories, uploadVideo } from '../../shared/extra-utils/index' | ||
12 | import { sha256 } from '../helpers/core-utils' | 13 | import { sha256 } from '../helpers/core-utils' |
13 | import { doRequestAndSaveToFile } from '../helpers/requests' | 14 | import { doRequestAndSaveToFile } from '../helpers/requests' |
14 | import { CONSTRAINTS_FIELDS } from '../initializers/constants' | 15 | import { CONSTRAINTS_FIELDS } from '../initializers/constants' |
15 | import { buildCommonVideoOptions, buildVideoAttributesFromCommander, getLogger, getServerCredentials } from './cli' | 16 | import { |
16 | import { YoutubeDL } from '@server/helpers/youtube-dl' | 17 | buildCommonVideoOptions, |
17 | 18 | buildServer, | |
18 | type UserInfo = { | 19 | buildVideoAttributesFromCommander, |
19 | username: string | 20 | getAccessTokenOrDie, |
20 | password: string | 21 | getLogger, |
21 | } | 22 | getServerCredentials |
23 | } from './cli' | ||
22 | 24 | ||
23 | const processOptions = { | 25 | const processOptions = { |
24 | maxBuffer: Infinity | 26 | maxBuffer: Infinity |
@@ -62,17 +64,13 @@ getServerCredentials(command) | |||
62 | url = normalizeTargetUrl(url) | 64 | url = normalizeTargetUrl(url) |
63 | options.targetUrl = normalizeTargetUrl(options.targetUrl) | 65 | options.targetUrl = normalizeTargetUrl(options.targetUrl) |
64 | 66 | ||
65 | const user = { username, password } | 67 | run(url, username, password) |
66 | |||
67 | run(url, user) | ||
68 | .catch(err => exitError(err)) | 68 | .catch(err => exitError(err)) |
69 | }) | 69 | }) |
70 | .catch(err => console.error(err)) | 70 | .catch(err => console.error(err)) |
71 | 71 | ||
72 | async function run (url: string, user: UserInfo) { | 72 | async function run (url: string, username: string, password: string) { |
73 | if (!user.password) { | 73 | if (!password) password = await promptPassword() |
74 | user.password = await promptPassword() | ||
75 | } | ||
76 | 74 | ||
77 | const youtubeDLBinary = await YoutubeDL.safeGetYoutubeDL() | 75 | const youtubeDLBinary = await YoutubeDL.safeGetYoutubeDL() |
78 | 76 | ||
@@ -111,7 +109,8 @@ async function run (url: string, user: UserInfo) { | |||
111 | await processVideo({ | 109 | await processVideo({ |
112 | cwd: options.tmpdir, | 110 | cwd: options.tmpdir, |
113 | url, | 111 | url, |
114 | user, | 112 | username, |
113 | password, | ||
115 | youtubeInfo: info | 114 | youtubeInfo: info |
116 | }) | 115 | }) |
117 | } catch (err) { | 116 | } catch (err) { |
@@ -119,17 +118,18 @@ async function run (url: string, user: UserInfo) { | |||
119 | } | 118 | } |
120 | } | 119 | } |
121 | 120 | ||
122 | log.info('Video/s for user %s imported: %s', user.username, options.targetUrl) | 121 | log.info('Video/s for user %s imported: %s', username, options.targetUrl) |
123 | process.exit(0) | 122 | process.exit(0) |
124 | } | 123 | } |
125 | 124 | ||
126 | async function processVideo (parameters: { | 125 | async function processVideo (parameters: { |
127 | cwd: string | 126 | cwd: string |
128 | url: string | 127 | url: string |
129 | user: { username: string, password: string } | 128 | username: string |
129 | password: string | ||
130 | youtubeInfo: any | 130 | youtubeInfo: any |
131 | }) { | 131 | }) { |
132 | const { youtubeInfo, cwd, url, user } = parameters | 132 | const { youtubeInfo, cwd, url, username, password } = parameters |
133 | const youtubeDL = new YoutubeDL('', []) | 133 | const youtubeDL = new YoutubeDL('', []) |
134 | 134 | ||
135 | log.debug('Fetching object.', youtubeInfo) | 135 | log.debug('Fetching object.', youtubeInfo) |
@@ -138,22 +138,29 @@ async function processVideo (parameters: { | |||
138 | log.debug('Fetched object.', videoInfo) | 138 | log.debug('Fetched object.', videoInfo) |
139 | 139 | ||
140 | const originallyPublishedAt = youtubeDL.buildOriginallyPublishedAt(videoInfo) | 140 | const originallyPublishedAt = youtubeDL.buildOriginallyPublishedAt(videoInfo) |
141 | |||
141 | if (options.since && originallyPublishedAt && originallyPublishedAt.getTime() < options.since.getTime()) { | 142 | if (options.since && originallyPublishedAt && originallyPublishedAt.getTime() < options.since.getTime()) { |
142 | log.info('Video "%s" has been published before "%s", don\'t upload it.\n', | 143 | log.info('Video "%s" has been published before "%s", don\'t upload it.\n', videoInfo.title, formatDate(options.since)) |
143 | videoInfo.title, formatDate(options.since)) | ||
144 | return | 144 | return |
145 | } | 145 | } |
146 | |||
146 | if (options.until && originallyPublishedAt && originallyPublishedAt.getTime() > options.until.getTime()) { | 147 | if (options.until && originallyPublishedAt && originallyPublishedAt.getTime() > options.until.getTime()) { |
147 | log.info('Video "%s" has been published after "%s", don\'t upload it.\n', | 148 | log.info('Video "%s" has been published after "%s", don\'t upload it.\n', videoInfo.title, formatDate(options.until)) |
148 | videoInfo.title, formatDate(options.until)) | ||
149 | return | 149 | return |
150 | } | 150 | } |
151 | 151 | ||
152 | const result = await advancedVideosSearch(url, { search: videoInfo.title, sort: '-match', searchTarget: 'local' }) | 152 | const server = buildServer(url) |
153 | const { data } = await server.searchCommand.advancedVideoSearch({ | ||
154 | search: { | ||
155 | search: videoInfo.title, | ||
156 | sort: '-match', | ||
157 | searchTarget: 'local' | ||
158 | } | ||
159 | }) | ||
153 | 160 | ||
154 | log.info('############################################################\n') | 161 | log.info('############################################################\n') |
155 | 162 | ||
156 | if (result.body.data.find(v => v.name === videoInfo.title)) { | 163 | if (data.find(v => v.name === videoInfo.title)) { |
157 | log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title) | 164 | log.info('Video "%s" already exists, don\'t reupload it.\n', videoInfo.title) |
158 | return | 165 | return |
159 | } | 166 | } |
@@ -172,7 +179,8 @@ async function processVideo (parameters: { | |||
172 | youtubeDL, | 179 | youtubeDL, |
173 | cwd, | 180 | cwd, |
174 | url, | 181 | url, |
175 | user, | 182 | username, |
183 | password, | ||
176 | videoInfo: normalizeObject(videoInfo), | 184 | videoInfo: normalizeObject(videoInfo), |
177 | videoPath: path | 185 | videoPath: path |
178 | }) | 186 | }) |
@@ -187,9 +195,10 @@ async function uploadVideoOnPeerTube (parameters: { | |||
187 | videoPath: string | 195 | videoPath: string |
188 | cwd: string | 196 | cwd: string |
189 | url: string | 197 | url: string |
190 | user: { username: string, password: string } | 198 | username: string |
199 | password: string | ||
191 | }) { | 200 | }) { |
192 | const { youtubeDL, videoInfo, videoPath, cwd, url, user } = parameters | 201 | const { youtubeDL, videoInfo, videoPath, cwd, url, username, password } = parameters |
193 | 202 | ||
194 | const category = await getCategory(videoInfo.categories, url) | 203 | const category = await getCategory(videoInfo.categories, url) |
195 | const licence = getLicence(videoInfo.license) | 204 | const licence = getLicence(videoInfo.license) |
@@ -223,7 +232,10 @@ async function uploadVideoOnPeerTube (parameters: { | |||
223 | tags | 232 | tags |
224 | } | 233 | } |
225 | 234 | ||
226 | const videoAttributes = await buildVideoAttributesFromCommander(url, program, defaultAttributes) | 235 | let accessToken = await getAccessTokenOrDie(url, username, password) |
236 | const server = buildServer(url, accessToken) | ||
237 | |||
238 | const videoAttributes = await buildVideoAttributesFromCommander(server, program, defaultAttributes) | ||
227 | 239 | ||
228 | Object.assign(videoAttributes, { | 240 | Object.assign(videoAttributes, { |
229 | originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, | 241 | originallyPublishedAt: originallyPublishedAt ? originallyPublishedAt.toISOString() : null, |
@@ -234,15 +246,13 @@ async function uploadVideoOnPeerTube (parameters: { | |||
234 | 246 | ||
235 | log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) | 247 | log.info('\nUploading on PeerTube video "%s".', videoAttributes.name) |
236 | 248 | ||
237 | let accessToken = await getAccessTokenOrDie(url, user) | ||
238 | |||
239 | try { | 249 | try { |
240 | await uploadVideo(url, accessToken, videoAttributes) | 250 | await uploadVideo(url, accessToken, videoAttributes) |
241 | } catch (err) { | 251 | } catch (err) { |
242 | if (err.message.indexOf('401') !== -1) { | 252 | if (err.message.indexOf('401') !== -1) { |
243 | log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') | 253 | log.info('Got 401 Unauthorized, token may have expired, renewing token and retry.') |
244 | 254 | ||
245 | accessToken = await getAccessTokenOrDie(url, user) | 255 | accessToken = await getAccessTokenOrDie(url, username, password) |
246 | 256 | ||
247 | await uploadVideo(url, accessToken, videoAttributes) | 257 | await uploadVideo(url, accessToken, videoAttributes) |
248 | } else { | 258 | } else { |
@@ -362,21 +372,6 @@ async function promptPassword () { | |||
362 | }) | 372 | }) |
363 | } | 373 | } |
364 | 374 | ||
365 | async function getAccessTokenOrDie (url: string, user: UserInfo) { | ||
366 | const resClient = await getClient(url) | ||
367 | const client = { | ||
368 | id: resClient.body.client_id, | ||
369 | secret: resClient.body.client_secret | ||
370 | } | ||
371 | |||
372 | try { | ||
373 | const res = await login(url, client, user) | ||
374 | return res.body.access_token | ||
375 | } catch (err) { | ||
376 | exitError('Cannot authenticate. Please check your username/password.') | ||
377 | } | ||
378 | } | ||
379 | |||
380 | function parseDate (dateAsStr: string): Date { | 375 | function parseDate (dateAsStr: string): Date { |
381 | if (!/\d{4}-\d{2}-\d{2}/.test(dateAsStr)) { | 376 | if (!/\d{4}-\d{2}-\d{2}/.test(dateAsStr)) { |
382 | exitError(`Invalid date passed: ${dateAsStr}. Expected format: YYYY-MM-DD. See help for usage.`) | 377 | exitError(`Invalid date passed: ${dateAsStr}. Expected format: YYYY-MM-DD. See help for usage.`) |