add cache for channels oowowowow :3

This commit is contained in:
Ashley 2023-04-28 20:39:55 +00:00
parent 56d01efe6d
commit 340c2a4ce0

View file

@ -31,31 +31,31 @@ function getJson(str) {
module.exports = function (app, config, renderTemplate) { module.exports = function (app, config, renderTemplate) {
app.get("/download", async function (req, res) { app.get("/download", async function (req, res) {
try { try {
var v = req.query.v; var v = req.query.v;
// video // video
const video = await modules.fetch(config.tubeApi + `video?v=${v}`); const video = await modules.fetch(config.tubeApi + `video?v=${v}`);
const h = await video.text(); const h = await video.text();
const k = JSON.parse(modules.toJson(h)); const k = JSON.parse(modules.toJson(h));
if (!v) res.redirect("/"); if (!v) res.redirect("/");
var fetching = await fetcher(v); var fetching = await fetcher(v);
const json = fetching.video.Player; const json = fetching.video.Player;
const engagement = fetching.engagement; const engagement = fetching.engagement;
renderTemplate(res, req, "download.ejs", { renderTemplate(res, req, "download.ejs", {
engagement: engagement, engagement: engagement,
k: k, k: k,
video: json, video: json,
date: k.Video.uploadDate, date: k.Video.uploadDate,
color: await modules color: await modules
.getColors(`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`) .getColors(`https://i.ytimg.com/vi/${v}/maxresdefault.jpg`)
.then((colors) => colors[0].hex()), .then((colors) => colors[0].hex()),
}); });
} catch { } catch {
res.redirect("/") res.redirect("/");
} }
}); });
@ -114,6 +114,7 @@ module.exports = function (app, config, renderTemplate) {
try { try {
const ID = req.query.id; const ID = req.query.id;
const tab = req.query.tab; const tab = req.query.tab;
const cache = {};
try { try {
// about // about
@ -148,20 +149,34 @@ module.exports = function (app, config, renderTemplate) {
return null; return null;
} }
}; };
var tj = await getChannelData(
const tj = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/videos/${ID}/?sort_by=${sort_by}${continuation}` `https://invid-api.poketube.fun/api/v1/channels/videos/${ID}/?sort_by=${sort_by}${continuation}`
); );
const shorts = await getChannelData( var shorts = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/${ID}/shorts?sort_by=${sort_by}${continuations}` `https://invid-api.poketube.fun/api/v1/channels/${ID}/shorts?sort_by=${sort_by}${continuations}`
); );
const stream = await getChannelData( var stream = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/${ID}/streams?sort_by=${sort_by}${continuationl}` `https://invid-api.poketube.fun/api/v1/channels/${ID}/streams?sort_by=${sort_by}${continuationl}`
); );
const c = await getChannelData( var c = await getChannelData(
`https://invid-api.poketube.fun/api/v1/channels/community/${ID}/` `https://invid-api.poketube.fun/api/v1/channels/community/${ID}/`
); );
cache[ID] = {
result: {
tj,
shorts,
stream,
c,
boutJson,
},
timestamp: Date.now(),
};
if (cache[ID] && Date.now() - cache[ID].timestamp < 3600000) {
var { tj, shorts, stream, c, boutJson } = cache[ID].result;
}
const summary = await wiki.summary(boutJson.Channel.Metadata.Name); const summary = await wiki.summary(boutJson.Channel.Metadata.Name);
const wikiSummary = summary.title !== "Not found." ? summary : "none"; const wikiSummary = summary.title !== "Not found." ? summary : "none";