poke/src/libpoketube/libpoketube-video.js

78 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-01-10 16:59:43 +01:00
/*
PokeTube is a Free/Libre youtube front-end !
Copyright (C) 2021-2023 POKETUBE
This file is Licensed under LGPL-3.0-or-later. Poketube itself is GPL, Only this file is LGPL.
see a copy here:https://www.gnu.org/licenses/lgpl-3.0.txt
please dont remove this comment while sharing this code
*/
const {
fetcher,
core,
wiki,
musicInfo,
modules,
version,
initlog,
init,
} = require("./libpoketube-initsys.js");
const {
IsJsonString,
convert,
getFirstLine,
capitalizeFirstLetter,
turntomins,
getRandomInt,
getRandomArbitrary,
} = require("./ptutils/libpt-coreutils.js");
module.exports = async function (video_id) {
var url;
2023-01-21 19:03:01 +01:00
2023-01-10 16:59:43 +01:00
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i)
if (arr[i] !== undefined) rv[i] = arr[i];
return rv;
}
2023-02-05 13:30:17 +01:00
try {
2023-01-10 16:59:43 +01:00
// 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)
.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 stringed = toObject(instance);
if (stringed[1].type != "https") {
2023-01-11 20:06:10 +01:00
url = "https://invidious.weblibre.org";
2023-01-10 16:59:43 +01:00
} else {
2023-01-10 20:03:11 +01:00
url = stringed[1].uri
.replace("invidious.tiekoetter.com", "invidious.weblibre.org")
2023-01-11 20:06:10 +01:00
.replace("yewtu.be", "invidious.sethforprivacy.com")
.replace("invidious.slipfox.xyz", "invidious.weblibre.org")
2023-01-21 19:03:01 +01:00
.replace("vid.priv.au", "inv.vern.cc")
.replace("invidious.snopyta.org", "invidious.sethforprivacy.com");
2023-01-10 16:59:43 +01:00
}
return url;
2023-02-05 13:30:17 +01:00
} catch {
return " "
}
2023-01-10 16:59:43 +01:00
};