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
2022-10-20 18:00:27 +01:00

53 lines
1.3 KiB
JavaScript

import fetch from "node-fetch";
import WebsiteTypes from "../../../src/consts/LeaderboardType";
import RedisUtils from "../../../src/utils/redisUtils";
const KEY = "BL_MAP_STAR_";
/**
*
* @param {Request} req
* @param {Response} res
* @returns
*/
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;
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({
status: "OK",
stars: Number.parseFloat(data),
});
}
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) {
return res.status(404).json({
status: 404,
message: "Unknown Map Hash",
});
}
const json = await data.json();
RedisUtils.setValue(`${KEY}${mapHash}`, json.difficulty.stars);
res.setHeader("Cache-Status", "miss");
return res.status(200).json({
status: "OK",
stars: json.difficulty.stars,
});
}