multiple tried before quitting to get metadata

This commit is contained in:
localhost 2023-08-18 21:31:40 +02:00
parent 19247ce30d
commit 3a5a086cee
1 changed files with 35 additions and 6 deletions

View File

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