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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
const express = require("express")
const fs = require("fs")
const request = require("request-promise-native")
const app = express()
const port = 3000
const sleep = require('util').promisify(setTimeout)
const patch = "8.24.1";
const key = "RGAPI-c6cae96a-c4b0-4842-9017-ddd736f3f628";
let appRateLimit1;
let appRateLimit120;
let appRateLimitCount1;
let appRateLimitCount120;
let rules = [
["/lol", "index.html"],
["/lol/script.js", "script.js"],
["/lol/style.css", "style.css"],
];
for (i in rules) {
let path = rules[i][0];
let file = rules[i][1];
app.get(path, (req, res) => {
res.sendFile(__dirname + "/html/" + file);
});
}
const regions = {
"BR": "br1.api.riotgames.com",
"EUNE": "eun1.api.riotgames.com",
"EUW": "euw1.api.riotgames.com",
"JP": "jp1.api.riotgames.com",
"KR": "kr.api.riotgames.com",
"LAN": "la1.api.riotgames.com",
"LAS": "la2.api.riotgames.com",
"NA": "na1.api.riotgames.com",
"OCE": "oc1.api.riotgames.com",
"TR": "tr1.api.riotgames.com",
"RU": "ru.api.riotgames.com",
"PBE": "pbe1.api.riotgames.com",
};
async function riotRequest(region, url, params, retries) {
if (retries < 1) throw "Error too many tries";
let req = "https://" + regions[region] + url + "?api_key=" + key;
for (p in params) {
req += "&" + p + "=" + params[p];
}
try {
let result = await request({uri: req, resolveWithFullResponse: true, json: true});
let appRateLimit = result.headers["x-app-rate-limit"];
let appRateLimitCount = result.headers["x-app-rate-limit-count"];
appRateLimit1 = appRateLimit.split(",")[0].split(":")[0];
appRateLimit120 = appRateLimit.split(",")[1].split(":")[0];
appRateLimitCount1 = appRateLimitCount.split(",")[0].split(":")[0];
appRateLimitCount120 = appRateLimitCount.split(",")[1].split(":")[0];
let delay1 = 1000 / (appRateLimit1 - 1);
let delay120 = 120000 / (appRateLimit120 - 1);
await sleep(Math.max(delay1, delay120));
console.log(Math.max(delay1, delay120));
return result.body;
} catch (err) {
console.log(err.message);
return await riotRequest(region, url, params, retries - 1);
}
}
async function getAllMatches(region, accountId) {
let matches = [];
let totalGames;
let bI = 0, eI = 99;
do {
let m = await riotRequest(region, "/lol/match/v4/matchlists/by-account/" + accountId, { beginIndex: bI, endIndex: eI }, 5);
console.log(m);
totalGames = m.totalGames;
matches = matches.concat(m.matches);
console.log("Added games " + bI + " to " + eI + ", " + matches.length + " of " + totalGames);
bI = eI + 1;
eI += 100;
} while (bI <= totalGames);
return matches;
}
// Static Data
// -----------
let champions = null;
let champLookup = {};
function getChampions(cb) {
request("http://ddragon.leagueoflegends.com/cdn/" + patch + "/data/en_US/champion.json", (err, res, body) => {
champions = JSON.parse(body).data;
for (c in champions) {
champLookup[champions[c].key] = c;
}
cb();
});
}
app.get("/lol/champions", (req, res) => {
if (champions == null)
getChampions(() => {
res.send(JSON.stringify(Object.keys(champions)));
});
else
res.send(JSON.stringify(Object.keys(champions)));
});
app.get("/lol/champlookup", (req, res) => {
if (champions == null)
getChampions(() => {
res.send(JSON.stringify(champLookup));
});
else
res.send(JSON.stringify(champLookup));
});
let users = {};
if (fs.existsSync("users.js")) {
fs.readFile("users.js", (err, data) => {
users = JSON.parse(data);
});
}
app.get("/lol/matches", async (req, res) => {
let region = req.query.region;
let summoner = req.query.summoner;
let regionUrl = regions[region];
try {
let data = await riotRequest(region, "/lol/summoner/v4/summoners/by-name/" + summoner, {}, 5);
let accountId = data.accountId;
if (users[accountId]) {
res.send(JSON.stringify(users[accountId]));
return;
}
let matches = await getAllMatches(region, accountId);
users[accountId] = matches;
fs.writeFile("users.js", JSON.stringify(users), (err) => {
if (err) console.log("Error writing file: " + err);
});
res.send(JSON.stringify(matches));
} catch (err) {
console.log(err);
}
});
// -----------
app.listen(port, () => {
console.log("Listening on port %d", port)
});
|