mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-10 04:08:39 +01:00
add cache to returnyoutubedislike owo
This commit is contained in:
parent
bfafd7a01e
commit
e613d8a742
1 changed files with 31 additions and 1 deletions
32
p/server.js
32
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"));
|
||||
|
|
Loading…
Reference in a new issue