From fec30d4d92e2d6dbb03dbf54a2c2aa2773973852 Mon Sep 17 00:00:00 2001 From: ashley Date: Sat, 12 Oct 2024 10:16:58 +0000 Subject: [PATCH] add fetchWithRetry --- src/libpoketube/libpoketube-core.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/libpoketube/libpoketube-core.js b/src/libpoketube/libpoketube-core.js index 02f1cbc..bbabc37 100644 --- a/src/libpoketube/libpoketube-core.js +++ b/src/libpoketube/libpoketube-core.js @@ -87,10 +87,22 @@ class InnerTubePokeVidious { let desc = ""; + const fetchWithRetry = async (url, options = {}, retries = 3) => { + for (let attempt = 0; attempt < retries; attempt++) { + const res = await fetch(url, options); + if (res.status === 500 && attempt < retries - 1) { + continue; // retry on 500 + } + return res; + } + // If all retries fail, return last response + return null; + }; + try { const [invComments, videoInfo, videoData] = await Promise.all([ - fetch(`${this.config.invapi}/comments/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()), - fetch(`${this.config.invapi}/videos/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()), + fetchWithRetry(`${this.config.invapi}/comments/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()), + fetchWithRetry(`${this.config.invapi}/videos/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()), curly .get(`${this.config.tubeApi}video?v=${v}`, { httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),