Refactor code :3

This commit is contained in:
Ashley 2023-02-25 17:48:15 +00:00
parent 5b6355438f
commit d19407156e

View file

@ -10,7 +10,7 @@
please dont remove this comment while sharing this code please dont remove this comment while sharing this code
*/ */
const { const {
fetcher, fetcher,
@ -34,44 +34,42 @@ const {
} = require("./ptutils/libpt-coreutils.js"); } = require("./ptutils/libpt-coreutils.js");
module.exports = async function (video_id) { module.exports = async function (video_id) {
var url; // function to convert an array to an object, ignoring undefined values
function toObject(arr) { function toObject(arr) {
var rv = {}; return arr.reduce((acc, cur, i) => {
for (var i = 0; i < arr.length; ++i) if (cur !== undefined) {
if (arr[i] !== undefined) rv[i] = arr[i]; acc[i] = cur;
return rv; }
return acc;
}, {});
} }
try { try {
// gets invidious instances // gets invidious instances
let inv_url = "https://api.invidious.io/instances.json?sort_by=type,health"; const invUrl = "https://api.invidious.io/instances.json?sort_by=type,health";
const invInstanceList = await modules
.fetch(invUrl)
.then((res) => res.text())
.then((json) => JSON.parse(json));
let inv_instance_list = await modules // gets random instances from the list
.fetch(inv_url) const instance = invInstanceList[Math.floor(Math.random() * invInstanceList.length)];
.then((res) => res.text())
.then((json) => JSON.parse(json));
// gets random instances from the list let url;
const instance = await inv_instance_list[ if (instance[1].type != "https") {
Math.floor(Math.random() * inv_instance_list.length) url = "https://invidious.weblibre.org";
]; } else {
url = instance[1].uri
.replace("invidious.tiekoetter.com", "invidious.weblibre.org")
.replace("yewtu.be", "invidious.sethforprivacy.com")
.replace("invidious.slipfox.xyz", "invidious.weblibre.org")
.replace("vid.priv.au", "inv.vern.cc")
.replace("invidious.snopyta.org", "invidious.sethforprivacy.com");
}
const stringed = toObject(instance); return url;
} catch (error) {
if (stringed[1].type != "https") { console.error("Failed to get Invidious instance:", error);
url = "https://invidious.weblibre.org"; return " ";
} else {
url = stringed[1].uri
.replace("invidious.tiekoetter.com", "invidious.weblibre.org")
.replace("yewtu.be", "invidious.sethforprivacy.com")
.replace("invidious.slipfox.xyz", "invidious.weblibre.org")
.replace("vid.priv.au", "inv.vern.cc")
.replace("invidious.snopyta.org", "invidious.sethforprivacy.com");
}
return url;
} catch {
return " "
} }
}; };