mirror of
https://codeberg.org/ashley/poke.git
synced 2024-11-24 22:48:17 +01:00
proxy 1.1.0
This commit is contained in:
parent
a29939ef64
commit
b1ed5d7dae
1 changed files with 51 additions and 26 deletions
49
p/server.js
49
p/server.js
|
@ -22,6 +22,7 @@ const URL_WHITELIST = [
|
||||||
"is3-ssl.mzstatic.com",
|
"is3-ssl.mzstatic.com",
|
||||||
"twemoji.maxcdn.com",
|
"twemoji.maxcdn.com",
|
||||||
"unpkg.com",
|
"unpkg.com",
|
||||||
|
"lite.duckduckgo.com",
|
||||||
"youtube.com",
|
"youtube.com",
|
||||||
"returnyoutubedislikeapi.com",
|
"returnyoutubedislikeapi.com",
|
||||||
"cdn.zptr.cc",
|
"cdn.zptr.cc",
|
||||||
|
@ -93,7 +94,7 @@ const listener = (req, res) => {
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
var json = {
|
var json = {
|
||||||
status: "200",
|
status: "200",
|
||||||
version: "1.0.0",
|
version: "1.1.0",
|
||||||
URL_WHITELIST,
|
URL_WHITELIST,
|
||||||
cache: "max-age-1848",
|
cache: "max-age-1848",
|
||||||
};
|
};
|
||||||
|
@ -101,13 +102,15 @@ app.get("/", (req, res) => {
|
||||||
res.json(json);
|
res.json(json);
|
||||||
});
|
});
|
||||||
|
|
||||||
const apiUrl = "https://returnyoutubedislikeapi.com/votes?videoId=";
|
const apiUrls = [
|
||||||
|
"https://returnyoutubedislikeapi.com/votes?videoId=",
|
||||||
|
"https://ipv6-t.poketube.fun/api?v=",
|
||||||
|
];
|
||||||
|
|
||||||
// Define a cache object
|
// Define a cache object
|
||||||
const cache = {};
|
const cache = {};
|
||||||
|
|
||||||
app.get("/api", async (req, res) => {
|
app.get("/api", async (req, res) => {
|
||||||
if (req.query.hash && req.query.hash === "d0550b6e28c8f93533a569c314d5b4e2") {
|
|
||||||
try {
|
try {
|
||||||
const cacheKey = req.query.v;
|
const cacheKey = req.query.v;
|
||||||
|
|
||||||
|
@ -116,13 +119,16 @@ app.get("/api", async (req, res) => {
|
||||||
// If the cached result is less than 1 hour old, return it
|
// If the cached result is less than 1 hour old, return it
|
||||||
const cachedData = cache[cacheKey].data;
|
const cachedData = cache[cacheKey].data;
|
||||||
const cachedDate = new Date(cache[cacheKey].timestamp);
|
const cachedDate = new Date(cache[cacheKey].timestamp);
|
||||||
return res.json({ data: cachedData, cachedDate });
|
return res.json(cachedData);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the result is not cached or is older than 1 hour, fetch it from the API
|
// Initialize an array to store errors when trying different URLs
|
||||||
const engagement = await fetch(apiUrl + req.query.v).then((res) =>
|
const errors = [];
|
||||||
res.json()
|
|
||||||
);
|
for (const apiUrl of apiUrls) {
|
||||||
|
try {
|
||||||
|
// Fetch data from the current URL
|
||||||
|
const engagement = await fetch(apiUrl + req.query.v).then((res) => res.json());
|
||||||
|
|
||||||
// Cache the result for future requests
|
// Cache the result for future requests
|
||||||
cache[cacheKey] = {
|
cache[cacheKey] = {
|
||||||
|
@ -130,11 +136,30 @@ app.get("/api", async (req, res) => {
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
};
|
};
|
||||||
|
|
||||||
res.json({ data: engagement, cachedDate: new Date() });
|
res.json(engagement);
|
||||||
} catch {}
|
return; // Exit the loop if successful
|
||||||
} else {
|
} catch (err) {
|
||||||
return res.send("no hash query found");
|
// Log the error for this URL and continue to the next URL
|
||||||
|
console.log(`Error fetching data from ${apiUrl}: ${err.message}`);
|
||||||
|
errors.push(err.message);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If all URLs fail, return an error response
|
||||||
|
res.status(500).json({ error: "All API endpoints failed", errors });
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/bangs", async (req, res) => {
|
||||||
|
|
||||||
|
let f = await fetch("https://lite.duckduckgo.com/lite/?q=" + req.query.q, {
|
||||||
|
method: req.method,
|
||||||
|
});
|
||||||
|
|
||||||
|
res.redirect(f);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.all("/*", listener);
|
app.all("/*", listener);
|
||||||
|
|
Loading…
Reference in a new issue