1
0
Fork 0
mirror of https://codeberg.org/ashley/poke.git synced 2025-02-19 04:58:49 -05:00
poke/src/libpoketube/libpoketube-core.js

171 lines
3.8 KiB
JavaScript
Raw Normal View History

2023-03-12 06:41:06 +00: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 16:24:54 +00: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");
2023-03-12 06:41:06 +00:00
const { curly } = require("node-libcurl");
2022-10-09 12:37:45 +02:00
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-04-19 22:10:18 +00:00
2022-10-09 12:37:45 +02:00
// Util functions
2023-05-01 19:26:18 +00:00
/*
* Api 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 14:11:24 +00:00
function checkUnexistingObject(obj) {
2022-12-25 16:42:08 +00:00
if (obj) {
if ("authorId" in obj) {
return true;
}
2022-12-16 19:11:53 +00:00
}
}
2023-05-01 19:26:18 +00:00
const cache = {};
const sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
2022-10-09 12:37:45 +02:00
2023-05-01 19:26:18 +00:00
const config = {
tubeApi: "https://inner-api.poketube.fun/api/",
invapi: "https://invid-api.poketube.fun/api/v1",
dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=",
t_url: "https://t.poketube.fun/", // def matomo url
};
2022-10-09 12:37:45 +02:00
2023-05-01 19:26:18 +00:00
function initerr(args){
console.error("[LIBPT CORE ERROR]" + args)
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 14:11:24 +00:00
2023-03-03 17:41:19 +00:00
// Check if result is already cached
2023-03-12 06:41:06 +00:00
if (cache[v] && Date.now() - cache[v].timestamp < 3600000) {
2023-03-03 17:41:19 +00:00
console.log("Returning cached result");
return cache[v].result;
}
2023-05-01 19:26:18 +00:00
var desc = "";
2023-03-12 06:41:06 +00:00
2023-01-24 09:01:44 +00:00
try {
2023-03-05 13:28:34 +00:00
var inv_comments = await fetch(`${config.invapi}/comments/${v}`).then(
2023-01-24 09:01:44 +00:00
(res) => res.text()
);
2022-10-09 12:37:45 +02:00
2023-01-24 09:01:44 +00:00
var comments = await getJson(inv_comments);
2023-03-11 16:03:14 +00:00
} catch (error) {
2023-04-19 22:10:18 +00:00
initerr("Error getting comments", error);
2023-01-24 09:01:44 +00:00
var comments = "";
}
2023-03-12 06:41:06 +00:00
2023-03-03 17:41:19 +00:00
let vid;
2023-01-24 09:10:06 +00:00
2023-03-11 16:03:14 +00:00
try {
2023-03-12 06:41:06 +00:00
const videoInfo = await fetch(`${config.invapi}/videos/${v}`).then((res) =>
res.text()
);
2023-03-11 16:03:14 +00:00
vid = await getJson(videoInfo);
} catch (error) {
2023-04-19 22:10:18 +00:00
initerr("Error getting video info", error);
2023-03-11 16:03:14 +00:00
}
2023-03-12 06:41:06 +00:00
2023-03-03 17:41:19 +00:00
if (!vid) {
2023-03-12 06:41:06 +00:00
console.log(
`Sorry nya, we couldn't find any information about that video qwq`
);
2023-03-03 17:41:19 +00:00
}
2023-02-25 17:08:54 +00:00
2022-12-18 14:11:24 +00:00
if (checkUnexistingObject(vid)) {
2022-12-21 15:38:26 +00:00
var a;
2022-12-24 10:48:55 +00:00
2022-12-21 15:38:26 +00:00
try {
2022-12-24 10:48:55 +00:00
var a = await fetch(
`${config.tubeApi}channel?id=${vid.authorId}&tab=about`
)
.then((res) => res.text())
.then((xml) => getJson(toJson(xml)));
2023-03-11 16:03:14 +00:00
} catch (error) {
2023-04-19 22:10:18 +00:00
initerr("Error getting channel info", error);
2022-12-24 10:48:55 +00:00
var a = "";
2022-12-21 15:38:26 +00:00
}
2023-05-01 19:26:18 +00:00
2022-12-20 20:19:02 +00:00
desc = a.Channel?.Contents?.ItemSection?.About?.Description;
2023-03-12 06:41:06 +00:00
const fe = await fetcher(v);
2022-12-18 14:11:24 +00:00
2023-03-11 16:03:14 +00:00
try {
const summary = await wiki
.summary(vid.author + " ")
.then((summary_) =>
summary_.title !== "Not found." ? summary_ : "none"
);
2023-03-12 06:41:06 +00:00
const headers = {};
var { data } = await curly.get(`${config.tubeApi}video?v=${v}`, {
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
});
var json = toJson(data);
const video = getJson(json);
2023-03-11 16:03:14 +00:00
// Store result in cache
cache[v] = {
result: {
2023-03-12 06:41:06 +00:00
json: fe?.video?.Player,
2023-03-11 16:03:14 +00:00
video,
vid,
comments,
2023-03-12 06:41:06 +00:00
engagement: fe.engagement,
2023-03-11 16:03:14 +00:00
wiki: summary,
desc: desc,
color: await getColors(
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${sqp}`
).then((colors) => colors[0].hex()),
color2: await getColors(
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${sqp}`
).then((colors) => colors[1].hex()),
},
2023-03-12 06:41:06 +00:00
timestamp: Date.now(),
2023-03-11 16:03:14 +00:00
};
return cache[v].result;
} catch (error) {
2023-04-19 22:10:18 +00:00
initerr("Error getting video", error);
2023-03-11 16:03:14 +00:00
}
2022-12-16 19:11:53 +00:00
}
2022-10-09 12:37:45 +02:00
}
2022-11-07 17:49:21 +01:00
async function isvalidvideo(v) {
if (v != "assets" && v != "cdn-cgi" && v != "404") {
return true;
} else {
return false;
2022-12-18 14:11:24 +00:00
}
}
2022-11-07 17:49:21 +01:00
2022-10-09 12:37:45 +02:00
module.exports = {
video,
2023-05-01 19:26:18 +00:00
isvalidvideo
2022-10-12 17:03:43 +02:00
};