2023-05-21 22:22:44 +02:00
|
|
|
/**
|
|
|
|
* PokeTube is a Free/Libre youtube front-end !
|
|
|
|
*
|
|
|
|
* 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 don't remove this comment while sharing this code.
|
|
|
|
*/
|
2022-10-09 14:58:53 +02:00
|
|
|
|
2022-10-09 12:37:45 +02:00
|
|
|
const { toJson } = require("xml2json");
|
2023-03-12 07:41:06 +01:00
|
|
|
const { curly } = require("node-libcurl");
|
2023-09-09 18:52:06 +02:00
|
|
|
const getdislikes = require("../libpoketube/libpoketube-dislikes.js");
|
2022-10-09 12:37:45 +02:00
|
|
|
const getColors = require("get-image-colors");
|
2023-04-20 00:10:18 +02:00
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
/**
|
|
|
|
* Class representing PokeTube's core functionality.
|
2023-05-01 21:26:18 +02:00
|
|
|
*/
|
2023-05-21 22:22:44 +02:00
|
|
|
class PokeTubeCore {
|
|
|
|
/**
|
|
|
|
* Create an instance of PokeTubeCore.
|
|
|
|
* @param {object} config - Configuration object for PokeTubeCore.
|
|
|
|
* @param {string} config.tubeApi - Tube API URL.
|
|
|
|
* @param {string} config.invapi - Invid API URL.
|
|
|
|
* @param {string} config.dislikes - Dislikes API URL.
|
|
|
|
* @param {string} config.t_url - Matomo URL.
|
|
|
|
*/
|
|
|
|
constructor(config) {
|
|
|
|
this.config = config;
|
|
|
|
this.cache = {};
|
2023-09-07 21:02:57 +02:00
|
|
|
this.language = "hl=en-US";
|
|
|
|
this.region = "region=US";
|
|
|
|
this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
|
2022-10-09 12:37:45 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
/**
|
|
|
|
* Fetch JSON from API response.
|
|
|
|
* @param {string} str - String response from the API.
|
|
|
|
* @returns {object|null} Parsed JSON object or null if parsing failed.
|
|
|
|
*/
|
|
|
|
getJson(str) {
|
|
|
|
try {
|
|
|
|
return JSON.parse(str);
|
|
|
|
} catch {
|
|
|
|
return null;
|
2022-12-25 17:42:08 +01:00
|
|
|
}
|
2022-12-16 20:11:53 +01:00
|
|
|
}
|
2023-03-12 07:41:06 +01:00
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
/**
|
|
|
|
* Check if the provided object has the required properties.
|
|
|
|
* @param {object} obj - Object to check.
|
|
|
|
* @returns {boolean} True if the object has the required properties, false otherwise.
|
|
|
|
*/
|
|
|
|
checkUnexistingObject(obj) {
|
|
|
|
if (obj) {
|
|
|
|
if ("authorId" in obj) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 17:03:14 +01:00
|
|
|
}
|
2023-03-12 07:41:06 +01:00
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
/**
|
|
|
|
* Fetch video information.
|
|
|
|
* @param {string} v - Video ID.
|
|
|
|
* @returns {Promise<object>} Promise resolving to the video information.
|
|
|
|
*/
|
2023-09-23 17:33:30 +02:00
|
|
|
async video(v, contentlang, contentregion) {
|
2023-08-21 23:08:57 +02:00
|
|
|
|
|
|
|
const { fetch } = await import("undici");
|
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
if (v == null) return "Gib ID";
|
|
|
|
|
|
|
|
// Check if result is already cached
|
|
|
|
if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) {
|
|
|
|
return this.cache[v].result;
|
|
|
|
}
|
2023-08-05 22:39:44 +02:00
|
|
|
const headers = {};
|
2023-02-25 18:08:54 +01:00
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
let desc = "";
|
2023-08-05 22:39:44 +02:00
|
|
|
|
2022-12-21 16:38:26 +01:00
|
|
|
try {
|
2023-08-05 22:39:44 +02:00
|
|
|
const [invComments, videoInfo, videoData] = await Promise.all([
|
2024-02-06 15:17:32 +01:00
|
|
|
fetch(`${this.config.invapi_alt}/comments/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()),
|
|
|
|
fetch(`${this.config.invapi_alt}/videos/${v}?hl=${contentlang}®ion=${contentregion}&h=${btoa(Date.now())}`).then((res) => res.text()),
|
2023-08-05 22:39:44 +02:00
|
|
|
curly
|
2023-11-12 15:40:06 +01:00
|
|
|
.get(`${this.config.tubeApi}video?v=${v}`, {
|
2023-08-05 22:39:44 +02:00
|
|
|
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
const json = toJson(res.data);
|
|
|
|
const video = this.getJson(json);
|
|
|
|
return { json, video };
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
|
|
|
|
const comments = await this.getJson(invComments);
|
|
|
|
const vid = await this.getJson(videoInfo);
|
|
|
|
const { json, video } = videoData;
|
2022-10-09 12:37:45 +02:00
|
|
|
|
2024-02-01 12:29:49 +01:00
|
|
|
const channel_uploads = await fetch(
|
2024-02-05 20:10:18 +01:00
|
|
|
`${this.config.invapi_alt}/channels/${vid.authorId}?hl=${contentlang}®ion=${contentregion}`
|
2024-02-01 12:29:49 +01:00
|
|
|
);
|
|
|
|
const p = this.getJson(await channel_uploads.text());
|
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
if (!vid) {
|
2023-08-05 22:39:44 +02:00
|
|
|
console.log(
|
|
|
|
`Sorry nya, we couldn't find any information about that video qwq`
|
|
|
|
);
|
2023-05-21 22:22:44 +02:00
|
|
|
}
|
2022-10-09 12:37:45 +02:00
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
if (this.checkUnexistingObject(vid)) {
|
2023-09-09 18:52:06 +02:00
|
|
|
const fe = await getdislikes(v);
|
2023-05-21 22:22:44 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
const headers = {};
|
|
|
|
|
|
|
|
// Store result in cache
|
|
|
|
this.cache[v] = {
|
|
|
|
result: {
|
2023-06-02 17:57:53 +02:00
|
|
|
json: json?.video,
|
2023-05-21 22:22:44 +02:00
|
|
|
video,
|
|
|
|
vid,
|
|
|
|
comments,
|
2024-02-01 12:29:49 +01:00
|
|
|
channel_uploads:p,
|
2023-05-21 22:22:44 +02:00
|
|
|
engagement: fe.engagement,
|
2023-08-05 22:39:44 +02:00
|
|
|
wiki: "",
|
2023-08-04 18:52:04 +02:00
|
|
|
desc: "",
|
2023-05-21 22:22:44 +02:00
|
|
|
color: await getColors(
|
|
|
|
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
|
|
|
|
).then((colors) => colors[0].hex()),
|
|
|
|
color2: await getColors(
|
|
|
|
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
|
|
|
|
).then((colors) => colors[1].hex()),
|
|
|
|
},
|
|
|
|
timestamp: Date.now(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.cache[v].result;
|
|
|
|
} catch (error) {
|
|
|
|
this.initError("Error getting video", error);
|
|
|
|
}
|
|
|
|
}
|
2023-08-05 22:39:44 +02:00
|
|
|
} catch {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 22:22:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a video ID is valid.
|
|
|
|
* @param {string} v - Video ID.
|
|
|
|
* @returns {boolean} True if the video ID is valid, false otherwise.
|
|
|
|
*/
|
|
|
|
isvalidvideo(v) {
|
2023-05-08 17:53:19 +02:00
|
|
|
if (v != "assets" && v != "cdn-cgi" && v != "404") {
|
2023-05-21 22:22:44 +02:00
|
|
|
return true;
|
2023-03-24 21:54:46 +01:00
|
|
|
} else {
|
2023-05-21 22:22:44 +02:00
|
|
|
return false;
|
2022-12-18 15:11:24 +01:00
|
|
|
}
|
2023-05-21 22:22:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize an error.
|
|
|
|
* @param {string} args - Error message.
|
|
|
|
* @param {Error} error - Error object.
|
|
|
|
*/
|
|
|
|
initError(args, error) {
|
|
|
|
console.error("[LIBPT CORE ERROR]" + args, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an instance of PokeTubeCore with the provided config
|
2023-06-02 17:57:53 +02:00
|
|
|
const pokeTubeApiCore = new PokeTubeCore({
|
2023-05-21 22:22:44 +02:00
|
|
|
tubeApi: "https://inner-api.poketube.fun/api/",
|
2024-02-02 10:26:00 +01:00
|
|
|
invapi: "https://invid-api.poketube.fun/api/v1",
|
2024-02-04 12:33:49 +01:00
|
|
|
invapi_alt: "https://iv.ggtyler.dev/api/v1",
|
2023-05-21 22:22:44 +02:00
|
|
|
dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=",
|
|
|
|
t_url: "https://t.poketube.fun/",
|
|
|
|
});
|
2022-11-07 17:49:21 +01:00
|
|
|
|
2023-06-02 17:57:53 +02:00
|
|
|
module.exports = pokeTubeApiCore;
|