From 6d2a1b10a97f231f23f7f65b5aff8e8503bcbcf9 Mon Sep 17 00:00:00 2001 From: Ashley Date: Thu, 9 Mar 2023 19:41:01 +0000 Subject: [PATCH] add cache to returnyoutubedislike owo --- p/server.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/p/server.js b/p/server.js index b9a68bdd..758e062a 100644 --- a/p/server.js +++ b/p/server.js @@ -47,7 +47,7 @@ app.use(function (req, res, next) { app.use(function (req, res, next) { res.header("Access-Control-Allow-Origin", "*"); - res.setHeader("Cache-Control", "public, max-age=870"); // cache header + res.setHeader("Cache-Control", "public, max-age=1848"); // cache header res.setHeader("poketube-cacher", "PROXY_FILES"); next(); @@ -95,6 +95,36 @@ app.get("/", (req, res) => res.redirect(`https://poketube.fun/watch?v=l3eww1dnd0k`) ); +const apiUrl = "https://returnyoutubedislikeapi.com/votes?videoId="; + +// Define a cache object +const cache = {}; + +app.get('/api', async (req, res) => { + const cacheKey = req.query.v; + + // Check if the result is already cached + if (cache[cacheKey] && Date.now() - cache[cacheKey].timestamp < 3600000) { + // If the cached result is less than 1 hour old, return it + const cachedData = cache[cacheKey].data; + const cachedDate = new Date(cache[cacheKey].timestamp); + return res.json({ data: cachedData, cachedDate }); + } + + // If the result is not cached or is older than 1 hour, fetch it from the API + const engagement = await fetch(apiUrl + req.query.v).then((res) => res.json()); + + // Cache the result for future requests + cache[cacheKey] = { + data: engagement, + timestamp: Date.now() + }; + + res.json({ data: engagement, cachedDate: new Date() }); +}); + + + app.all("/*", listener); app.listen(3000, () => console.log("Listening on 0.0.0.0:3000"));