From 71578f317e881f35ec905e9136f77740bbd7e7aa Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Tue, 20 Feb 2018 18:03:56 +0100 Subject: import-youtube: add try/catch to manage token expiration when importing lot of youtube videos (#306) --- server/tools/import-youtube.ts | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'server/tools') 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 () { youtubeDL.getInfo(program['youtubeUrl'], options, processOptions, async (err, info) => { if (err) throw err - let infoArray: any[] - // Normalize utf8 fields - if (Array.isArray(info) === true) { - infoArray = info.map(i => normalizeObject(i)) - } else { - infoArray = [ normalizeObject(info) ] - } + info = info.map(i => normalizeObject(i)) - const videos = infoArray.map(i => { + const videos = info.map(i => { return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title } }) console.log('Will download and upload %d videos.\n', videos.length) for (const video of videos) { - await processVideo(video, program['language']) + await processVideo(video, program['language'], client, user) } - console.log('I\'m finished!') + console.log('I have finished!') process.exit(0) }) } -function processVideo (video: { name: string, url: string }, languageCode: number) { +function processVideo (video: { name: string, url: string }, languageCode: number, client: { id: string, secret: string }, user: { username: string, password: string }) { return new Promise(async res => { const result = await searchVideo(program['url'], video.name) @@ -100,7 +94,7 @@ function processVideo (video: { name: string, url: string }, languageCode: numbe youtubeDL.getInfo(video.url, undefined, processOptions, async (err, videoInfo) => { if (err) return console.error(err) - await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, languageCode) + await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, client, user, languageCode) return res() }) @@ -108,7 +102,7 @@ function processVideo (video: { name: string, url: string }, languageCode: numbe }) } -async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, language?: number) { +async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, client: { id: string, secret: string }, user: { username: string, password: string }, language?: number) { const category = await getCategory(videoInfo.categories) const licence = getLicence(videoInfo.license) let tags = [] @@ -145,7 +139,17 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, languag } console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) - await uploadVideo(program['url'], accessToken, videoAttributes) + try { + await uploadVideo(program['url'], accessToken, videoAttributes) + } + catch (err) { + if ((err.message).search("401")) { + console.log("Get 401 Unauthorized, token may have expired, renewing token and retry.") + const res2 = await login(program['url'], client, user) + accessToken = res2.body.access_token + await uploadVideo(program['url'], accessToken, videoAttributes) + } + } await unlinkPromise(videoPath) if (thumbnailfile) { -- cgit v1.2.3