From 2c52963c8494ff63e8fba7b7567b82f435a8036a Mon Sep 17 00:00:00 2001 From: Ashley Date: Sat, 21 Oct 2023 05:12:57 +0000 Subject: [PATCH] does this work idk qwq --- src/libpoketube/libpoketube-dislikes.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/libpoketube/libpoketube-dislikes.js b/src/libpoketube/libpoketube-dislikes.js index 197dcd89..b8377c73 100644 --- a/src/libpoketube/libpoketube-dislikes.js +++ b/src/libpoketube/libpoketube-dislikes.js @@ -41,13 +41,21 @@ class PokeTubeDislikesAPIManager { * @private */ async _getEngagementData() { - const apiUrl = `https://p.poketube.fun/api?v=${this.videoId}&hash=d0550b6e28c8f93533a569c314d5b4e2`; + const apiUrl = `https://p.poketube.fun/api?v=${this.videoId}&hash=d0550b6e28c8f93533a569c314d5b4e2`; const fallbackUrl = `https://returnyoutubedislikeapi.com/votes?videoId=${this.videoId}`; const { fetch } = await import("undici"); try { - var engagementP = await fetch(apiUrl).then((res) => res.json()); + // Set a timeout of 2 seconds. + const timeoutMilliseconds = 2000; // 2 seconds + var engagementP = await fetch(apiUrl, { timeout: timeoutMilliseconds }) + .then((res) => { + if (res.statusCode === 504) { + throw new Error("Request timed out."); + } + return res.json(); + }); if (typeof engagementP.dislikes === 'number') { return engagementP; @@ -55,10 +63,12 @@ try { throw new Error("API response doesn't contain valid dislikes count. Using fallback URL."); } } catch (error) { - var engagement = await fetch(fallbackUrl).then((res) => res.json()); + console.error(error); + var engagement = await fetch(fallbackUrl).then((res) => res.json()); return engagement; } + }