mirror of
https://github.com/Shineii86/MiruroAPI.git
synced 2026-10-22 10:15:10 +00:00
Fix random (Date.now+hrtime seed) + character/staff 404 detection
This commit is contained in:
@@ -320,9 +320,10 @@ const createApiRoutes = (jsonResponse, jsonError, startTime) => {
|
|||||||
// ---- FEATURE: Random anime of the day ----
|
// ---- FEATURE: Random anime of the day ----
|
||||||
router.get("/random", async (req, res) => {
|
router.get("/random", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
// NOTE: Use crypto for better randomness in serverless + wider range for variety
|
// NOTE: Use Date.now() + process.hrtime for randomness in serverless
|
||||||
const crypto = require("crypto");
|
const hrtime = process.hrtime();
|
||||||
const randomPage = crypto.randomInt(1, 500);
|
const seed = Date.now() ^ (hrtime[0] * 1000 + hrtime[1]);
|
||||||
|
const randomPage = (Math.abs(seed) % 499) + 1;
|
||||||
const data = await anilist.getCollection("POPULARITY_DESC", null, randomPage, 1);
|
const data = await anilist.getCollection("POPULARITY_DESC", null, randomPage, 1);
|
||||||
const anime = data.results[0];
|
const anime = data.results[0];
|
||||||
|
|
||||||
@@ -506,7 +507,7 @@ const createApiRoutes = (jsonResponse, jsonError, startTime) => {
|
|||||||
|
|
||||||
jsonResponse(res, result);
|
jsonResponse(res, result);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const status = err.message.includes("not found") ? 404 : 500;
|
const status = err.message.includes("not found") || err.message.includes("404") ? 404 : 500;
|
||||||
jsonError(res, err.message, status);
|
jsonError(res, err.message, status);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -526,7 +527,7 @@ const createApiRoutes = (jsonResponse, jsonError, startTime) => {
|
|||||||
|
|
||||||
jsonResponse(res, result);
|
jsonResponse(res, result);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const status = err.message.includes("not found") ? 404 : 500;
|
const status = err.message.includes("not found") || err.message.includes("404") ? 404 : 500;
|
||||||
jsonError(res, err.message, status);
|
jsonError(res, err.message, status);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user