poke/src/libpoketube/libpoketube-core.js

207 lines
4.4 KiB
JavaScript
Raw Normal View History

2022-10-22 16:58:54 +02:00
/*
2022-10-09 12:37:45 +02:00
2022-10-09 14:58:53 +02:00
PokeTube is a Free/Libre youtube front-end !
2022-10-09 12:37:45 +02:00
2023-01-11 17:24:54 +01:00
Copyright (C) 2021-2023 POKETUBE
2022-10-09 12:37:45 +02:00
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
2022-10-12 16:40:31 +02:00
please dont remove this comment while sharing this code
2022-10-09 14:58:53 +02:00
*/
2022-10-09 12:37:45 +02:00
const fetch = require("node-fetch");
const { toJson } = require("xml2json");
2022-10-27 11:30:49 +02:00
const fetcher = require("../libpoketube/libpoketube-fetcher.js");
2022-10-09 12:37:45 +02:00
const getColors = require("get-image-colors");
const wiki = require("wikipedia");
2023-02-25 18:08:54 +01:00
const sqp =
"-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
2022-10-09 12:37:45 +02:00
const config = {
2022-12-13 19:22:15 +01:00
tubeApi: "https://tube-srv.ashley143.gay/api/",
2023-02-10 18:37:50 +01:00
invapi: "https://inv.zzls.xyz/api/v1",
2022-10-09 12:37:45 +02:00
dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=",
2022-10-09 14:58:53 +02:00
t_url: "https://t.poketube.fun/", // def matomo url
2022-10-09 12:37:45 +02:00
};
// Util functions
2022-10-09 14:30:08 +02:00
function getJson(str) {
2022-10-09 12:37:45 +02:00
try {
2022-11-16 18:32:02 +01:00
return JSON.parse(str);
2022-10-09 14:30:08 +02:00
} catch {
return null;
2022-10-09 12:37:45 +02:00
}
}
2022-12-18 15:11:24 +01:00
function checkUnexistingObject(obj) {
2022-12-25 17:42:08 +01:00
if (obj) {
if ("authorId" in obj) {
return true;
}
2022-12-16 20:11:53 +01:00
}
}
2022-10-09 12:37:45 +02:00
/*
* Api functions
*/
async function channel(id, cnt) {
2022-10-09 14:30:08 +02:00
if (id == null) return "Gib ID";
2022-10-09 12:37:45 +02:00
2022-10-09 14:30:08 +02:00
const videos = await fetch(
`${config.tubeApi}channel?id=${id}&tab=videos&continuation=${cnt || ""}`
)
.then((res) => res.text())
2022-11-16 17:58:03 +01:00
.then((xml) => getJson(toJson(xml)));
2022-10-09 12:37:45 +02:00
2022-10-09 14:30:08 +02:00
const about = await fetch(`${config.tubeApi}channel?id=${id}&tab=about`)
.then((res) => res.text())
2022-11-16 17:58:03 +01:00
.then((xml) => getJson(toJson(xml)));
2022-10-09 12:37:45 +02:00
2022-10-09 14:30:08 +02:00
return { videos, about };
2022-10-09 12:37:45 +02:00
}
async function video(v) {
2022-11-16 17:58:03 +01:00
if (v == null) return "Gib ID";
2022-12-18 15:11:24 +01:00
let nightlyRes;
2022-12-20 21:19:02 +01:00
var desc = "";
2023-02-25 18:08:54 +01:00
var iurl = "invidious.privacydev.net";
2023-01-26 10:24:22 +01:00
2023-01-24 10:01:44 +01:00
try {
var inv_comments = await fetch(`${config.invapi}/comments/${v}`).then(
(res) => res.text()
);
2022-10-09 12:37:45 +02:00
2023-01-24 10:01:44 +01:00
var comments = await getJson(inv_comments);
} catch {
var comments = "";
}
2023-02-25 18:08:54 +01:00
const urls = [
"invidious.sethforprivacy.com",
"invidious.weblibre.org",
2023-02-25 20:39:16 +01:00
"inv.vern.cc",
"invidious.privacydev.net",
2023-03-02 16:07:32 +01:00
"inv.zzls.xyz",
"vid.puffyan.us"
2023-02-25 18:08:54 +01:00
];
2023-01-26 10:24:22 +01:00
2023-02-25 18:08:54 +01:00
let vid;
2023-01-24 10:10:06 +01:00
2023-03-02 16:05:53 +01:00
try {
for (const url of urls) {
const videoInfo = await fetch(`https://${url}/api/v1/videos/${v}`).then(res => res.text());
vid = await getJson(videoInfo);
2023-02-25 18:08:54 +01:00
2023-03-02 16:05:53 +01:00
if (vid?.descriptionHtml !== "<p></p>") {
break;
}
2023-01-24 10:10:06 +01:00
}
2023-02-25 18:08:54 +01:00
2023-03-02 16:05:53 +01:00
if (!vid) {
// Handle error case
throw new Error('Unable to fetch video information.');
}
} catch (error) {
console.error(error);
2023-02-25 18:08:54 +01:00
// Handle error case
}
2022-12-18 15:11:24 +01:00
if (checkUnexistingObject(vid)) {
2022-12-21 16:38:26 +01:00
var a;
2022-12-24 11:48:55 +01:00
2022-12-21 16:38:26 +01:00
try {
2022-12-24 11:48:55 +01:00
var a = await fetch(
`${config.tubeApi}channel?id=${vid.authorId}&tab=about`
)
.then((res) => res.text())
.then((xml) => getJson(toJson(xml)));
2022-12-21 16:38:26 +01:00
} catch {
2022-12-24 11:48:55 +01:00
var a = "";
2022-12-21 16:38:26 +01:00
}
2022-12-24 11:48:55 +01:00
2022-12-18 15:11:24 +01:00
const summary = await wiki
.summary(vid.author + " ")
.then((summary_) =>
summary_.title !== "Not found." ? summary_ : "none"
);
2022-12-20 21:19:02 +01:00
desc = a.Channel?.Contents?.ItemSection?.About?.Description;
2022-12-18 15:11:24 +01:00
const data = await fetcher(v);
const nightlyJsonData = getJson(nightlyRes);
2022-12-20 15:42:07 +01:00
2022-12-18 15:11:24 +01:00
return {
2023-01-26 10:24:22 +01:00
json: data?.video?.Player,
2022-12-18 15:11:24 +01:00
video: await fetch(`${config.tubeApi}video?v=${v}`)
.then((res) => res.text())
2023-02-22 17:03:02 +01:00
.then((xml) => getJson(toJson(xml)))
.catch(" "),
2022-12-18 15:11:24 +01:00
vid,
comments,
engagement: data.engagement,
wiki: summary,
desc: desc,
color: await getColors(
2023-01-26 10:24:22 +01:00
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${sqp}`
2022-12-18 15:11:24 +01:00
).then((colors) => colors[0].hex()),
2022-11-24 21:13:32 +01:00
color2: await getColors(
2023-01-26 10:24:22 +01:00
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${sqp}`
2022-12-18 15:11:24 +01:00
).then((colors) => colors[1].hex()),
};
2022-12-16 20:11:53 +01:00
}
2022-10-09 12:37:45 +02:00
}
async function search(query, cnt) {
2022-10-09 14:30:08 +02:00
if (query == null) return "Gib Query";
2022-10-09 12:37:45 +02:00
2022-10-09 14:30:08 +02:00
const data = await fetch(
`${config.tubeApi}search?query=${query}&continuation=${cnt || ""}`
)
.then((res) => res.text())
2022-11-16 17:58:03 +01:00
.then((xml) => getJson(toJson(xml)));
2022-10-09 12:37:45 +02:00
2022-10-09 14:30:08 +02:00
return data;
2022-10-09 12:37:45 +02:00
}
2022-11-07 17:49:21 +01:00
async function isvalidvideo(v) {
2022-12-18 15:11:24 +01:00
if (v != "assets") {
var status;
async function ryd() {
try {
const engagement = await fetch(`${config.dislikes}${v}`).then((res) =>
res.json()
);
return engagement;
} catch {}
}
2022-12-18 15:11:24 +01:00
if (ryd.status) {
status = await ryd.status();
} else {
status = "200";
}
2022-12-20 21:19:02 +01:00
2022-12-18 15:11:24 +01:00
if (status == 400) {
return false;
} else {
return true;
}
2022-11-07 17:49:21 +01:00
}
}
2022-10-09 12:37:45 +02:00
module.exports = {
search,
video,
2022-11-07 17:49:21 +01:00
isvalidvideo,
2022-10-09 14:58:53 +02:00
channel,
2022-10-12 17:03:43 +02:00
};