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,35 +34,32 @@ 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
let inv_instance_list = await modules .fetch(invUrl)
.fetch(inv_url)
.then((res) => res.text()) .then((res) => res.text())
.then((json) => JSON.parse(json)); .then((json) => JSON.parse(json));
// gets random instances from the list // gets random instances from the list
const instance = await inv_instance_list[ const instance = invInstanceList[Math.floor(Math.random() * invInstanceList.length)];
Math.floor(Math.random() * inv_instance_list.length)
];
const stringed = toObject(instance); let url;
if (instance[1].type != "https") {
if (stringed[1].type != "https") {
url = "https://invidious.weblibre.org"; url = "https://invidious.weblibre.org";
} else { } else {
url = stringed[1].uri url = instance[1].uri
.replace("invidious.tiekoetter.com", "invidious.weblibre.org") .replace("invidious.tiekoetter.com", "invidious.weblibre.org")
.replace("yewtu.be", "invidious.sethforprivacy.com") .replace("yewtu.be", "invidious.sethforprivacy.com")
.replace("invidious.slipfox.xyz", "invidious.weblibre.org") .replace("invidious.slipfox.xyz", "invidious.weblibre.org")
@ -71,7 +68,8 @@ module.exports = async function (video_id) {
} }
return url; return url;
} catch { } catch (error) {
return " " console.error("Failed to get Invidious instance:", error);
return " ";
} }
}; };