aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/peertube-import-videos.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/tools/peertube-import-videos.ts')
-rw-r--r--server/tools/peertube-import-videos.ts34
1 files changed, 26 insertions, 8 deletions
diff --git a/server/tools/peertube-import-videos.ts b/server/tools/peertube-import-videos.ts
index 15f517cab..151c5a989 100644
--- a/server/tools/peertube-import-videos.ts
+++ b/server/tools/peertube-import-videos.ts
@@ -6,10 +6,11 @@ import { join } from 'path'
6import { VideoPrivacy } from '../../shared/models/videos' 6import { VideoPrivacy } from '../../shared/models/videos'
7import { doRequestAndSaveToFile } from '../helpers/requests' 7import { doRequestAndSaveToFile } from '../helpers/requests'
8import { CONSTRAINTS_FIELDS } from '../initializers' 8import { CONSTRAINTS_FIELDS } from '../initializers'
9import { getClient, getVideoCategories, login, searchVideoWithSort, uploadVideo } from '../tests/utils' 9import { getClient, getVideoCategories, login, searchVideoWithSort, uploadVideo } from '../../shared/utils/index'
10import { truncate } from 'lodash' 10import { truncate } from 'lodash'
11import * as prompt from 'prompt' 11import * as prompt from 'prompt'
12import { remove } from 'fs-extra' 12import { remove } from 'fs-extra'
13import { sha256 } from '../helpers/core-utils'
13import { safeGetYoutubeDL } from '../helpers/youtube-dl' 14import { safeGetYoutubeDL } from '../helpers/youtube-dl'
14import { getSettings, netrc } from './cli' 15import { getSettings, netrc } from './cli'
15 16
@@ -57,6 +58,7 @@ getSettings()
57 settings.remotes[settings.default] : 58 settings.remotes[settings.default] :
58 settings.remotes[0] 59 settings.remotes[0]
59 } 60 }
61
60 if (!program['username']) program['username'] = netrc.machines[program['url']].login 62 if (!program['username']) program['username'] = netrc.machines[program['url']].login
61 if (!program['password']) program['password'] = netrc.machines[program['url']].password 63 if (!program['password']) program['password'] = netrc.machines[program['url']].password
62 } 64 }
@@ -68,12 +70,19 @@ getSettings()
68 process.exit(-1) 70 process.exit(-1)
69 } 71 }
70 72
73 removeEndSlashes(program['url'])
74 removeEndSlashes(program['targetUrl'])
75
71 const user = { 76 const user = {
72 username: program['username'], 77 username: program['username'],
73 password: program['password'] 78 password: program['password']
74 } 79 }
75 80
76 run(user, program['url']).catch(err => console.error(err)) 81 run(user, program['url'])
82 .catch(err => {
83 console.error(err)
84 process.exit(-1)
85 })
77}) 86})
78 87
79async function promptPassword () { 88async function promptPassword () {
@@ -107,8 +116,12 @@ async function run (user, url: string) {
107 secret: res.body.client_secret 116 secret: res.body.client_secret
108 } 117 }
109 118
110 const res2 = await login(url, client, user) 119 try {
111 accessToken = res2.body.access_token 120 const res = await login(program[ 'url' ], client, user)
121 accessToken = res.body.access_token
122 } catch (err) {
123 throw new Error('Cannot authenticate. Please check your username/password.')
124 }
112 125
113 const youtubeDL = await safeGetYoutubeDL() 126 const youtubeDL = await safeGetYoutubeDL()
114 127
@@ -133,8 +146,7 @@ async function run (user, url: string) {
133 await processVideo(info, program['language'], processOptions.cwd, url, user) 146 await processVideo(info, program['language'], processOptions.cwd, url, user)
134 } 147 }
135 148
136 // https://www.youtube.com/watch?v=2Upx39TBc1s 149 console.log('Video/s for user %s imported: %s', program['username'], program['targetUrl'])
137 console.log('I\'m finished!')
138 process.exit(0) 150 process.exit(0)
139 }) 151 })
140} 152}
@@ -155,7 +167,7 @@ function processVideo (info: any, languageCode: string, cwd: string, url: string
155 return res() 167 return res()
156 } 168 }
157 169
158 const path = join(cwd, new Date().getTime() + '.mp4') 170 const path = join(cwd, sha256(videoInfo.url) + '.mp4')
159 171
160 console.log('Downloading video "%s"...', videoInfo.title) 172 console.log('Downloading video "%s"...', videoInfo.title)
161 173
@@ -192,7 +204,7 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, cwd: st
192 204
193 let thumbnailfile 205 let thumbnailfile
194 if (videoInfo.thumbnail) { 206 if (videoInfo.thumbnail) {
195 thumbnailfile = join(cwd, 'thumbnail.jpg') 207 thumbnailfile = join(cwd, sha256(videoInfo.thumbnail) + '.jpg')
196 208
197 await doRequestAndSaveToFile({ 209 await doRequestAndSaveToFile({
198 method: 'GET', 210 method: 'GET',
@@ -322,3 +334,9 @@ function isNSFW (info: any) {
322 334
323 return false 335 return false
324} 336}
337
338function removeEndSlashes (url: string) {
339 while (url.endsWith('/')) {
340 url.slice(0, -1)
341 }
342}