-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKitsuAPI.js
81 lines (61 loc) · 2.14 KB
/
KitsuAPI.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const AsyncHttpRequest = require("./AsyncHttpRequest.js"), RequestType = require("./RequestType.json");
function queryConverter(query = "") {
query = query.split("+").join(" ");
switch (query) {
case "Newest":
case "Top Rated": // Kitsu doesn't keep track of rating so just fold it into "Newest".
return "[sort=date:DESC]";
case "Most Played":
return "[sort=playcount]";
default:
return query.split(" ").join("%20");
}
}
function kitsuDirectListingConvert(json = {}) {
let s = `${json.SetID}.osz|${json.Artist}|${json.Title}|${json.Creator}|${json.RankedStatus}|10.00000|${json.LastUpdate}|${json.SetID}|${json.ChildrenBeatmaps[0].BeatmapID}|0|0|0||`;
for (let diff of json.ChildrenBeatmaps) {
s += `${diff.DiffName}@${diff.Mode},`;
}
s = s.substring(0, s.length - 1);
return s;
}
function kitsuDirectSingleConvert(json) {
return `${json.SetID}.osz|${json.Artist}|${json.Title}|${json.Creator}|${1}|10.00000|${0}|${json.SetID}|${json.SetID}|0|0|0|`;
}
function statusConverter(status = 0) {
switch (status) {
case 0:
case 7:
return 1;
case 2:
return 0;
case 3:
return 3;
case 4:
return -50;
case 5:
return -2;
case 8:
return 4;
default:
return 1;
}
}
async function search(amount = 40, page = 0, status = 1, mode = 0, query = "") {
const response = await AsyncHttpRequest(`https://kitsu.moe/api/search?amount=50&offset=${amount * page}${(status == 4 ? "" : `&status=${statusConverter(status)}`)}${(mode == -1 ? "" : `&mode=${mode}`)}&query=${queryConverter(query)}`, RequestType.JSON);
return response;
}
async function searchSingle(beatmapId = 0) {
const beatmap = await AsyncHttpRequest(`https://kitsu.moe/api/b/${beatmapId}`, RequestType.JSON); // Get beatmap the client requested
if (beatmap != null && beatmap instanceof Object) {
const response = await AsyncHttpRequest(`https://kitsu.moe/api/s/${beatmap.ParentSetID}`, RequestType.JSON); // Get the parent set from the beatmap
return response;
}
else return null;
}
module.exports = {
search: search,
searchSingle: searchSingle,
kitsuDirectListingConvert: kitsuDirectListingConvert,
kitsuDirectSingleConvert: kitsuDirectSingleConvert
}