multiple tried before quitting to get metadata

This commit is contained in:
localhost 2023-08-18 21:38:59 +02:00
parent 246bd8e4d2
commit 32d7451ef4
1 changed files with 35 additions and 6 deletions

View File

@ -1,4 +1,5 @@
const fetch = require('node-fetch')
const maxRetries = 5
async function getInstance() {
const instances = await (await fetch('https://api.invidious.io/instances.json?pretty=1')).json()
@ -12,15 +13,43 @@ async function getPipedInstance() {
}
async function getVideoMetadata(id) {
const instance = await getInstance()
const json = await (await fetch(`${instance}/api/v1/videos/${id}?fields=videoId,title,descriptionHtml,videoThumbnails,published,authorId&pretty=1`)).json()
return json
for (let retries = 0; retries < maxRetries; retries++) {
try {
const instance = await getInstance()
const response = await fetch(`${instance}/api/v1/videos/${id}?fields=videoId,title,descriptionHtml,videoThumbnails,published,authorId&pretty=1`)
if (response.ok) {
const json = await response.json()
return json
} else {
continue
}
} catch (error) {
continue
}
}
return false
}
async function getChannel(id) {
const instance = await getInstance()
const json = await (await fetch(`${instance}/api/v1/channels/${id}?pretty=1`)).json()
return json
for (let retries = 0; retries < maxRetries; retries++) {
try {
const instance = await getInstance()
const response = await fetch(`${instance}/api/v1/channels/${id}?pretty=1`)
if (response.ok) {
const json = await response.json()
return json
} else {
continue
}
} catch (error) {
continue
}
}
return false
}
async function getChannelVideos(id) {