mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-22 22:57:54 +01:00
Refactor code :3
This commit is contained in:
parent
afd6420244
commit
014ba91637
1 changed files with 170 additions and 151 deletions
|
@ -1,32 +1,42 @@
|
||||||
/*
|
/**
|
||||||
|
* PokeTube is a Free/Libre youtube front-end !
|
||||||
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.
|
||||||
Copyright (C) 2021-2023 POKETUBE
|
* See a copy here: https://www.gnu.org/licenses/lgpl-3.0.txt
|
||||||
|
* Please don't remove this comment while sharing this code.
|
||||||
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 fetch = require("node-fetch");
|
const fetch = require("node-fetch");
|
||||||
const { toJson } = require("xml2json");
|
const { toJson } = require("xml2json");
|
||||||
const { curly } = require("node-libcurl");
|
const { curly } = require("node-libcurl");
|
||||||
|
|
||||||
const fetcher = require("../libpoketube/libpoketube-fetcher.js");
|
const fetcher = require("../libpoketube/libpoketube-fetcher.js");
|
||||||
const getColors = require("get-image-colors");
|
const getColors = require("get-image-colors");
|
||||||
|
|
||||||
const wiki = require("wikipedia");
|
const wiki = require("wikipedia");
|
||||||
|
|
||||||
// Util functions
|
/**
|
||||||
|
* Class representing PokeTube's core functionality.
|
||||||
/*
|
|
||||||
* Api functions
|
|
||||||
*/
|
*/
|
||||||
function getJson(str) {
|
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 = {};
|
||||||
|
this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
try {
|
||||||
return JSON.parse(str);
|
return JSON.parse(str);
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -34,7 +44,12 @@ function getJson(str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkUnexistingObject(obj) {
|
/**
|
||||||
|
* 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 (obj) {
|
||||||
if ("authorId" in obj) {
|
if ("authorId" in obj) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -42,78 +57,63 @@ function checkUnexistingObject(obj) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const cache = {};
|
/**
|
||||||
const sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
|
* Fetch video information.
|
||||||
|
* @param {string} v - Video ID.
|
||||||
const config = {
|
* @returns {Promise<object>} Promise resolving to the video information.
|
||||||
tubeApi: "https://inner-api.poketube.fun/api/",
|
*/
|
||||||
invapi: "https://invid-api.poketube.fun/api/v1",
|
async video(v) {
|
||||||
dislikes: "https://returnyoutubedislikeapi.com/votes?videoId=",
|
|
||||||
t_url: "https://t.poketube.fun/", // def matomo url
|
|
||||||
};
|
|
||||||
|
|
||||||
function initerr(args){
|
|
||||||
console.error("[LIBPT CORE ERROR]" + args)
|
|
||||||
}
|
|
||||||
|
|
||||||
async function video(v) {
|
|
||||||
if (v == null) return "Gib ID";
|
if (v == null) return "Gib ID";
|
||||||
|
|
||||||
// Check if result is already cached
|
// Check if result is already cached
|
||||||
if (cache[v] && Date.now() - cache[v].timestamp < 3600000) {
|
if (this.cache[v] && Date.now() - this.cache[v].timestamp < 3600000) {
|
||||||
console.log("Returning cached result");
|
console.log("Returning cached result");
|
||||||
return cache[v].result;
|
return this.cache[v].result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var desc = "";
|
let desc = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var inv_comments = await fetch(`${config.invapi}/comments/${v}`).then(
|
const inv_comments = await fetch(`${this.config.invapi}/comments/${v}`).then((res) =>
|
||||||
(res) => res.text()
|
res.text()
|
||||||
);
|
);
|
||||||
|
var comments = await this.getJson(inv_comments);
|
||||||
var comments = await getJson(inv_comments);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
initerr("Error getting comments", error);
|
this.initError("Error getting comments", error);
|
||||||
var comments = "";
|
var comments = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
let vid;
|
let vid;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const videoInfo = await fetch(`${config.invapi}/videos/${v}`).then((res) =>
|
const videoInfo = await fetch(`${this.config.invapi}/videos/${v}`).then((res) =>
|
||||||
res.text()
|
res.text()
|
||||||
);
|
);
|
||||||
vid = await getJson(videoInfo);
|
vid = await this.getJson(videoInfo);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
initerr("Error getting video info", error);
|
this.initError("Error getting video info", error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!vid) {
|
if (!vid) {
|
||||||
console.log(
|
console.log(`Sorry nya, we couldn't find any information about that video qwq`);
|
||||||
`Sorry nya, we couldn't find any information about that video qwq`
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checkUnexistingObject(vid)) {
|
if (this.checkUnexistingObject(vid)) {
|
||||||
var a;
|
let a;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var a = await fetch(
|
a = await fetch(`${this.config.tubeApi}channel?id=${vid.authorId}&tab=about`)
|
||||||
`${config.tubeApi}channel?id=${vid.authorId}&tab=about`
|
|
||||||
)
|
|
||||||
.then((res) => res.text())
|
.then((res) => res.text())
|
||||||
.then((xml) => getJson(toJson(xml)));
|
.then((xml) => this.getJson(toJson(xml)));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
initerr("Error getting channel info", error);
|
this.initError("Error getting channel info", error);
|
||||||
var a = "";
|
a = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
desc = a.Channel?.Contents?.ItemSection?.About?.Description;
|
desc = a.Channel?.Contents?.ItemSection?.About?.Description;
|
||||||
const fe = await fetcher(v);
|
const fe = await fetcher(v);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const summary = await wiki
|
const summary = await wiki
|
||||||
.summary(vid.author + " ")
|
.summary(vid.author + " ")
|
||||||
.then((summary_) =>
|
.then((summary_) =>
|
||||||
|
@ -122,14 +122,14 @@ async function video(v) {
|
||||||
|
|
||||||
const headers = {};
|
const headers = {};
|
||||||
|
|
||||||
var { data } = await curly.get(`${config.tubeApi}video?v=${v}`, {
|
const { data } = await curly.get(`${this.config.tubeApi}video?v=${v}`, {
|
||||||
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
|
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
|
||||||
});
|
});
|
||||||
var json = toJson(data);
|
const json = toJson(data);
|
||||||
const video = getJson(json);
|
const video = this.getJson(json);
|
||||||
|
|
||||||
// Store result in cache
|
// Store result in cache
|
||||||
cache[v] = {
|
this.cache[v] = {
|
||||||
result: {
|
result: {
|
||||||
json: fe?.video?.Player,
|
json: fe?.video?.Player,
|
||||||
video,
|
video,
|
||||||
|
@ -139,24 +139,28 @@ async function video(v) {
|
||||||
wiki: summary,
|
wiki: summary,
|
||||||
desc: desc,
|
desc: desc,
|
||||||
color: await getColors(
|
color: await getColors(
|
||||||
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${sqp}`
|
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
|
||||||
).then((colors) => colors[0].hex()),
|
).then((colors) => colors[0].hex()),
|
||||||
color2: await getColors(
|
color2: await getColors(
|
||||||
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${sqp}`
|
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
|
||||||
).then((colors) => colors[1].hex()),
|
).then((colors) => colors[1].hex()),
|
||||||
},
|
},
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return cache[v].result;
|
return this.cache[v].result;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
initerr("Error getting video", error);
|
this.initError("Error getting video", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
async function isvalidvideo(v) {
|
* 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) {
|
||||||
if (v != "assets" && v != "cdn-cgi" && v != "404") {
|
if (v != "assets" && v != "cdn-cgi" && v != "404") {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -164,7 +168,22 @@ async function isvalidvideo(v) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
/**
|
||||||
video,
|
* Initialize an error.
|
||||||
isvalidvideo
|
* @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
|
||||||
|
const pokeTubeCore = new PokeTubeCore({
|
||||||
|
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/",
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = pokeTubeCore;
|
||||||
|
|
Loading…
Reference in a new issue