aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tools/import-youtube.ts
diff options
context:
space:
mode:
authorLecygneNoir <victor.jlc@laposte.net>2018-02-20 18:03:56 +0100
committerChocobozzz <me@florianbigard.com>2018-02-20 18:03:56 +0100
commit71578f317e881f35ec905e9136f77740bbd7e7aa (patch)
tree222421a9abe701ae7e5a5b9f99fa90607d37ec19 /server/tools/import-youtube.ts
parent07fa4c97ca50b83b0bee9230da97d02401b4e05f (diff)
downloadPeerTube-71578f317e881f35ec905e9136f77740bbd7e7aa.tar.gz
PeerTube-71578f317e881f35ec905e9136f77740bbd7e7aa.tar.zst
PeerTube-71578f317e881f35ec905e9136f77740bbd7e7aa.zip
import-youtube: add try/catch to manage token expiration when importing lot of youtube videos (#306)
Diffstat (limited to 'server/tools/import-youtube.ts')
-rw-r--r--server/tools/import-youtube.ts32
1 files changed, 18 insertions, 14 deletions
diff --git a/server/tools/import-youtube.ts b/server/tools/import-youtube.ts
index e51a66e5f..20b4b0179 100644
--- a/server/tools/import-youtube.ts
+++ b/server/tools/import-youtube.ts
@@ -52,31 +52,25 @@ async function run () {
52 youtubeDL.getInfo(program['youtubeUrl'], options, processOptions, async (err, info) => { 52 youtubeDL.getInfo(program['youtubeUrl'], options, processOptions, async (err, info) => {
53 if (err) throw err 53 if (err) throw err
54 54
55 let infoArray: any[]
56
57 // Normalize utf8 fields 55 // Normalize utf8 fields
58 if (Array.isArray(info) === true) { 56 info = info.map(i => normalizeObject(i))
59 infoArray = info.map(i => normalizeObject(i))
60 } else {
61 infoArray = [ normalizeObject(info) ]
62 }
63 57
64 const videos = infoArray.map(i => { 58 const videos = info.map(i => {
65 return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title } 59 return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
66 }) 60 })
67 61
68 console.log('Will download and upload %d videos.\n', videos.length) 62 console.log('Will download and upload %d videos.\n', videos.length)
69 63
70 for (const video of videos) { 64 for (const video of videos) {
71 await processVideo(video, program['language']) 65 await processVideo(video, program['language'], client, user)
72 } 66 }
73 67
74 console.log('I\'m finished!') 68 console.log('I have finished!')
75 process.exit(0) 69 process.exit(0)
76 }) 70 })
77} 71}
78 72
79function processVideo (video: { name: string, url: string }, languageCode: number) { 73function processVideo (video: { name: string, url: string }, languageCode: number, client: { id: string, secret: string }, user: { username: string, password: string }) {
80 return new Promise(async res => { 74 return new Promise(async res => {
81 const result = await searchVideo(program['url'], video.name) 75 const result = await searchVideo(program['url'], video.name)
82 76
@@ -100,7 +94,7 @@ function processVideo (video: { name: string, url: string }, languageCode: numbe
100 youtubeDL.getInfo(video.url, undefined, processOptions, async (err, videoInfo) => { 94 youtubeDL.getInfo(video.url, undefined, processOptions, async (err, videoInfo) => {
101 if (err) return console.error(err) 95 if (err) return console.error(err)
102 96
103 await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, languageCode) 97 await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, client, user, languageCode)
104 98
105 return res() 99 return res()
106 }) 100 })
@@ -108,7 +102,7 @@ function processVideo (video: { name: string, url: string }, languageCode: numbe
108 }) 102 })
109} 103}
110 104
111async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, language?: number) { 105async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, client: { id: string, secret: string }, user: { username: string, password: string }, language?: number) {
112 const category = await getCategory(videoInfo.categories) 106 const category = await getCategory(videoInfo.categories)
113 const licence = getLicence(videoInfo.license) 107 const licence = getLicence(videoInfo.license)
114 let tags = [] 108 let tags = []
@@ -145,7 +139,17 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, languag
145 } 139 }
146 140
147 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) 141 console.log('\nUploading on PeerTube video "%s".', videoAttributes.name)
148 await uploadVideo(program['url'], accessToken, videoAttributes) 142 try {
143 await uploadVideo(program['url'], accessToken, videoAttributes)
144 }
145 catch (err) {
146 if ((err.message).search("401")) {
147 console.log("Get 401 Unauthorized, token may have expired, renewing token and retry.")
148 const res2 = await login(program['url'], client, user)
149 accessToken = res2.body.access_token
150 await uploadVideo(program['url'], accessToken, videoAttributes)
151 }
152 }
149 153
150 await unlinkPromise(videoPath) 154 await unlinkPromise(videoPath)
151 if (thumbnailfile) { 155 if (thumbnailfile) {