optimize code owo

This commit is contained in:
Ashley 2023-08-05 20:39:44 +00:00
parent 194764ba03
commit bd64d7f659

View file

@ -28,7 +28,8 @@ class PokeTubeCore {
constructor(config) { constructor(config) {
this.config = config; this.config = config;
this.cache = {}; this.cache = {};
this.sqp = "-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw"; this.sqp =
"-oaymwEbCKgBEF5IVfKriqkDDggBFQAAiEIYAXABwAEG&rs=AOn4CLBy_x4UUHLNDZtJtH0PXeQGoRFTgw";
} }
/** /**
@ -70,52 +71,41 @@ class PokeTubeCore {
console.log("Returning cached result"); console.log("Returning cached result");
return this.cache[v].result; return this.cache[v].result;
} }
const headers = {};
let desc = ""; let desc = "";
try { try {
const inv_comments = await fetch(`${this.config.invapi}/comments/${v}`).then((res) => const [invComments, videoInfo, videoData] = await Promise.all([
res.text() fetch(`${this.config.invapi}/comments/${v}`).then((res) => res.text()),
); fetch(`${this.config.invapi}/videos/${v}`).then((res) => res.text()),
var comments = await this.getJson(inv_comments); curly
} catch (error) { .get(`${this.config.tubeApi}video?v=${v}`, {
this.initError("Error getting comments", error); httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
var comments = ""; })
} .then((res) => {
const json = toJson(res.data);
const video = this.getJson(json);
return { json, video };
}),
]);
let vid; const comments = await this.getJson(invComments);
const vid = await this.getJson(videoInfo);
try { const { json, video } = videoData;
const videoInfo = await fetch(`${this.config.invapi}/videos/${v}`).then((res) =>
res.text()
);
vid = await this.getJson(videoInfo);
} catch (error) {
this.initError("Error getting video info", error);
}
if (!vid) { if (!vid) {
console.log(`Sorry nya, we couldn't find any information about that video qwq`); console.log(
`Sorry nya, we couldn't find any information about that video qwq`
);
} }
if (this.checkUnexistingObject(vid)) { if (this.checkUnexistingObject(vid)) {
const fe = await fetcher(v); const fe = await fetcher(v);
try { try {
const summary = await wiki
.summary(vid.author + " ")
.then((summary_) =>
summary_.title !== "Not found." ? summary_ : "none"
);
const headers = {}; const headers = {};
const { data } = await curly.get(`${this.config.tubeApi}video?v=${v}`, {
httpHeader: Object.entries(headers).map(([k, v]) => `${k}: ${v}`),
});
const json = toJson(data);
const video = this.getJson(json);
// Store result in cache // Store result in cache
this.cache[v] = { this.cache[v] = {
result: { result: {
@ -124,7 +114,7 @@ class PokeTubeCore {
vid, vid,
comments, comments,
engagement: fe.engagement, engagement: fe.engagement,
wiki: summary, wiki: "",
desc: "", desc: "",
color: await getColors( color: await getColors(
`https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}` `https://i.ytimg.com/vi/${v}/hqdefault.jpg?sqp=${this.sqp}`
@ -141,7 +131,11 @@ class PokeTubeCore {
this.initError("Error getting video", error); this.initError("Error getting video", error);
} }
} }
} } catch {
}
}
/** /**
* Check if a video ID is valid. * Check if a video ID is valid.