import-youtube: add try/catch to manage token expiration when importing lot of youtube videos (#306)

This commit is contained in:
LecygneNoir 2018-02-20 18:03:56 +01:00 committed by Chocobozzz
parent 07fa4c97ca
commit 71578f317e
1 changed files with 18 additions and 14 deletions

View File

@ -52,31 +52,25 @@ async function run () {
youtubeDL.getInfo(program['youtubeUrl'], options, processOptions, async (err, info) => { youtubeDL.getInfo(program['youtubeUrl'], options, processOptions, async (err, info) => {
if (err) throw err if (err) throw err
let infoArray: any[]
// Normalize utf8 fields // Normalize utf8 fields
if (Array.isArray(info) === true) { info = info.map(i => normalizeObject(i))
infoArray = info.map(i => normalizeObject(i))
} else {
infoArray = [ normalizeObject(info) ]
}
const videos = infoArray.map(i => { const videos = info.map(i => {
return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title } return { url: 'https://www.youtube.com/watch?v=' + i.id, name: i.title }
}) })
console.log('Will download and upload %d videos.\n', videos.length) console.log('Will download and upload %d videos.\n', videos.length)
for (const video of videos) { 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) 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 => { return new Promise(async res => {
const result = await searchVideo(program['url'], video.name) 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) => { youtubeDL.getInfo(video.url, undefined, processOptions, async (err, videoInfo) => {
if (err) return console.error(err) if (err) return console.error(err)
await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, languageCode) await uploadVideoOnPeerTube(normalizeObject(videoInfo), path, client, user, languageCode)
return res() 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 category = await getCategory(videoInfo.categories)
const licence = getLicence(videoInfo.license) const licence = getLicence(videoInfo.license)
let tags = [] let tags = []
@ -145,7 +139,17 @@ async function uploadVideoOnPeerTube (videoInfo: any, videoPath: string, languag
} }
console.log('\nUploading on PeerTube video "%s".', videoAttributes.name) 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) await unlinkPromise(videoPath)
if (thumbnailfile) { if (thumbnailfile) {