adding retry system

This commit is contained in:
unknown 2023-03-10 17:34:39 +01:00
parent 863ca93f6e
commit 7ba142580a
1 changed files with 28 additions and 4 deletions

View File

@ -1,10 +1,34 @@
const fetch = require('node-fetch') const fetch = require('node-fetch')
async function getInstance() { async function getInstance() {
// const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json() for (let i = 0; i < 5; i++) {
// const list = instances.filter(i => !i.cdn) const instance = await actuallyGettingInstance()
// return (list[Math.floor(Math.random() * list.length)]).api_url if (instance) return instance
return 'https://pipedapi.kavin.rocks' }
async function actuallyGettingInstance() {
const instance = await getRandomInstance()
const test = await testInstance(instance)
if (test) return instance
else {
return false
}
}
async function getRandomInstance() {
const instances = await (await fetch('https://piped-instances.kavin.rocks/')).json()
const list = instances.filter(i => !i.cdn)
return (list[Math.floor(Math.random() * list.length)]).api_url
}
async function testInstance(instance) {
try {
await (await fetch(`${instance}/streams/WDogskpmM7M`)).json()
return true
} catch (e) {
return false
}
}
} }
async function getVideoMetadata(instance, id) { async function getVideoMetadata(instance, id) {