diff options
-rw-r--r-- | server/tools/peertube-import-videos.ts | 70 |
1 files changed, 41 insertions, 29 deletions
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts index 2c9eabe98..d04ddfe06 100644 --- a/server/tools/peertube-import-videos.ts +++ b/server/tools/peertube-import-videos.ts | |||
@@ -72,42 +72,42 @@ async function run (url: string, user: UserInfo) { | |||
72 | 72 | ||
73 | const youtubeDL = await safeGetYoutubeDL() | 73 | const youtubeDL = await safeGetYoutubeDL() |
74 | 74 | ||
75 | const options = [ '-j', '--flat-playlist', '--playlist-reverse', ...command.args ] | 75 | let info = await getYoutubeDLInfo(youtubeDL, program['targetUrl'], command.args) |
76 | 76 | ||
77 | youtubeDL.getInfo(program['targetUrl'], options, processOptions, async (err, info) => { | 77 | if (info?.title === 'Uploads') { |
78 | if (err) { | 78 | console.log('Fixing URL to %s.', info.url) |
79 | exitError(err.stderr + ' ' + err.message) | ||
80 | } | ||
81 | 79 | ||
82 | let infoArray: any[] | 80 | info = await getYoutubeDLInfo(youtubeDL, info.url, command.args) |
81 | } | ||
83 | 82 | ||
84 | // Normalize utf8 fields | 83 | let infoArray: any[] |
85 | infoArray = [].concat(info) | ||
86 | if (program['first']) { | ||
87 | infoArray = infoArray.slice(0, program['first']) | ||
88 | } else if (program['last']) { | ||
89 | infoArray = infoArray.slice(-program['last']) | ||
90 | } | ||
91 | infoArray = infoArray.map(i => normalizeObject(i)) | ||
92 | 84 | ||
93 | log.info('Will download and upload %d videos.\n', infoArray.length) | 85 | // Normalize utf8 fields |
86 | infoArray = [].concat(info) | ||
87 | if (program['first']) { | ||
88 | infoArray = infoArray.slice(0, program['first']) | ||
89 | } else if (program['last']) { | ||
90 | infoArray = infoArray.slice(-program['last']) | ||
91 | } | ||
92 | infoArray = infoArray.map(i => normalizeObject(i)) | ||
94 | 93 | ||
95 | for (const info of infoArray) { | 94 | log.info('Will download and upload %d videos.\n', infoArray.length) |
96 | try { | 95 | |
97 | await processVideo({ | 96 | for (const info of infoArray) { |
98 | cwd: program['tmpdir'], | 97 | try { |
99 | url, | 98 | await processVideo({ |
100 | user, | 99 | cwd: program['tmpdir'], |
101 | youtubeInfo: info | 100 | url, |
102 | }) | 101 | user, |
103 | } catch (err) { | 102 | youtubeInfo: info |
104 | console.error('Cannot process video.', { info, url }) | 103 | }) |
105 | } | 104 | } catch (err) { |
105 | console.error('Cannot process video.', { info, url }) | ||
106 | } | 106 | } |
107 | } | ||
107 | 108 | ||
108 | log.info('Video/s for user %s imported: %s', user.username, program['targetUrl']) | 109 | log.info('Video/s for user %s imported: %s', user.username, program['targetUrl']) |
109 | process.exit(0) | 110 | process.exit(0) |
110 | }) | ||
111 | } | 111 | } |
112 | 112 | ||
113 | function processVideo (parameters: { | 113 | function processVideo (parameters: { |
@@ -397,3 +397,15 @@ function exitError (message: string, ...meta: any[]) { | |||
397 | console.error(message, ...meta) | 397 | console.error(message, ...meta) |
398 | process.exit(-1) | 398 | process.exit(-1) |
399 | } | 399 | } |
400 | |||
401 | function getYoutubeDLInfo (youtubeDL: any, url: string, args: string[]) { | ||
402 | return new Promise<any>((res, rej) => { | ||
403 | const options = [ '-j', '--flat-playlist', '--playlist-reverse', ...args ] | ||
404 | |||
405 | youtubeDL.getInfo(url, options, processOptions, async (err, info) => { | ||
406 | if (err) return rej(err) | ||
407 | |||
408 | return res(info) | ||
409 | }) | ||
410 | }) | ||
411 | } | ||