mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-17 11:34:39 -05:00
Refactor code :3
This commit is contained in:
parent
2c10e36d71
commit
ce114a9c02
1 changed files with 31 additions and 33 deletions
|
@ -34,35 +34,32 @@ const {
|
|||
} = require("./ptutils/libpt-coreutils.js");
|
||||
|
||||
module.exports = async function (video_id) {
|
||||
var url;
|
||||
|
||||
// function to convert an array to an object, ignoring undefined values
|
||||
function toObject(arr) {
|
||||
var rv = {};
|
||||
for (var i = 0; i < arr.length; ++i)
|
||||
if (arr[i] !== undefined) rv[i] = arr[i];
|
||||
return rv;
|
||||
return arr.reduce((acc, cur, i) => {
|
||||
if (cur !== undefined) {
|
||||
acc[i] = cur;
|
||||
}
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
try {
|
||||
// gets invidious instances
|
||||
let inv_url = "https://api.invidious.io/instances.json?sort_by=type,health";
|
||||
|
||||
let inv_instance_list = await modules
|
||||
.fetch(inv_url)
|
||||
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));
|
||||
|
||||
// gets random instances from the list
|
||||
const instance = await inv_instance_list[
|
||||
Math.floor(Math.random() * inv_instance_list.length)
|
||||
];
|
||||
const instance = invInstanceList[Math.floor(Math.random() * invInstanceList.length)];
|
||||
|
||||
const stringed = toObject(instance);
|
||||
|
||||
if (stringed[1].type != "https") {
|
||||
let url;
|
||||
if (instance[1].type != "https") {
|
||||
url = "https://invidious.weblibre.org";
|
||||
} else {
|
||||
url = stringed[1].uri
|
||||
url = instance[1].uri
|
||||
.replace("invidious.tiekoetter.com", "invidious.weblibre.org")
|
||||
.replace("yewtu.be", "invidious.sethforprivacy.com")
|
||||
.replace("invidious.slipfox.xyz", "invidious.weblibre.org")
|
||||
|
@ -71,7 +68,8 @@ module.exports = async function (video_id) {
|
|||
}
|
||||
|
||||
return url;
|
||||
} catch {
|
||||
return " "
|
||||
} catch (error) {
|
||||
console.error("Failed to get Invidious instance:", error);
|
||||
return " ";
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue