From ce16fffe9d93e8f17f7ad90b437f4bc848b75805 Mon Sep 17 00:00:00 2001 From: Ashley Date: Fri, 23 Sep 2022 18:54:05 +0200 Subject: [PATCH] fix some issues regarding the api :3 --- src/fetcher.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/fetcher.js b/src/fetcher.js index 9b95135e..0ea22e22 100644 --- a/src/fetcher.js +++ b/src/fetcher.js @@ -15,6 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. */ + const fetch = require("node-fetch"); //2.5.x const { toJson } = require("xml2json"); @@ -28,14 +29,27 @@ module.exports = async function (video_id) { ); const dislikes = dislike.dislikes || "none"; + const headers = {}; /* * Parses and fetches an xml */ + async function parsexml(id) { - const player = await fetch(`${new_api_url}?v=${id}`); - var h = await player.text(); - var j = toJson(h); - return JSON.parse(j); + for (let i = 0; i < 3; i++) { + try { + const player = await fetch(`${new_api_url}?v=${id}`, headers); + var h = await player.text(); + var j = toJson(h); + return JSON.parse(j); + } catch (err) { + if (err.status === 503) { + // retry after a bit + await new Promise((resolve) => setTimeout(resolve, 1000)); + } else { + return ""; + } + } + } } /*