This repository has been archived on 2023-11-06. You can view files and clone it, but cannot push or open issues or pull requests.
beatsaber-overlay/pages/api/beatleader/stars.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-10-20 14:51:52 +00:00
import fetch from "node-fetch";
import WebsiteTypes from "../../../src/consts/LeaderboardType";
2022-10-20 17:00:27 +00:00
import RedisUtils from "../../../src/utils/redisUtils";
2022-10-20 14:51:52 +00:00
2022-10-20 17:00:27 +00:00
const KEY = "BL_MAP_STAR_";
/**
*
* @param {Request} req
* @param {Response} res
* @returns
*/
2022-10-20 14:51:52 +00:00
export default async function handler(req, res) {
const mapHash = req.query.hash.replace("custom_level_", "").toLowerCase();
const difficulty = req.query.difficulty;
const characteristic = req.query.characteristic;
2022-10-20 17:00:27 +00:00
const exists = await RedisUtils.exists(`${KEY}${mapHash}`);
if (exists) {
const data = await RedisUtils.getValue(`${KEY}${mapHash}`);
res.setHeader("Cache-Status", "hit");
return res.status(200).json({
2022-10-20 14:51:52 +00:00
status: "OK",
2022-10-20 17:00:27 +00:00
stars: Number.parseFloat(data),
2022-10-20 14:51:52 +00:00
});
}
const data = await fetch(
WebsiteTypes.BeatLeader.ApiUrl.MapData.replace("%h", mapHash)
.replace("%d", difficulty.replace("+", "Plus"))
.replace("%m", characteristic),
{
headers: {
"X-Requested-With": "BeatSaber Overlay",
},
}
);
if (data.status === 404) {
2022-10-20 17:00:27 +00:00
return res.status(404).json({
2022-10-20 14:51:52 +00:00
status: 404,
2022-10-20 17:00:27 +00:00
message: "Unknown Map Hash",
2022-10-20 14:51:52 +00:00
});
}
const json = await data.json();
2022-10-20 17:00:27 +00:00
RedisUtils.setValue(`${KEY}${mapHash}`, json.difficulty.stars);
res.setHeader("Cache-Status", "miss");
return res.status(200).json({
2022-10-20 14:51:52 +00:00
status: "OK",
stars: json.difficulty.stars,
});
}