From 7ba142580af37398d3b934559ccf8b5251c05eaa Mon Sep 17 00:00:00 2001 From: unknown <89595418+unknownsrc@users.noreply.github.com> Date: Fri, 10 Mar 2023 17:34:39 +0100 Subject: [PATCH] adding retry system --- utils/metadata.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/utils/metadata.js b/utils/metadata.js index c55d3f4..95a445b 100644 --- a/utils/metadata.js +++ b/utils/metadata.js @@ -1,10 +1,34 @@ const fetch = require('node-fetch') async function getInstance() { - // 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 - return 'https://pipedapi.kavin.rocks' + for (let i = 0; i < 5; i++) { + const instance = await actuallyGettingInstance() + if (instance) return instance + } + + 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) {