1 const express = require("express")
2 const fs = require("fs")
3 const request = require("request-promise-native")
6 const sleep = require('util').promisify(setTimeout)
8 const patch = "8.24.1";
9 const key = "RGAPI-c6cae96a-c4b0-4842-9017-ddd736f3f628";
12 let appRateLimitCount1;
13 let appRateLimitCount120;
16 ["/lol", "index.html"],
17 ["/lol/script.js", "script.js"],
18 ["/lol/style.css", "style.css"],
22 let path = rules[i][0];
23 let file = rules[i][1];
24 app.get(path, (req, res) => {
25 res.sendFile(__dirname + "/html/" + file);
30 "BR": "br1.api.riotgames.com",
31 "EUNE": "eun1.api.riotgames.com",
32 "EUW": "euw1.api.riotgames.com",
33 "JP": "jp1.api.riotgames.com",
34 "KR": "kr.api.riotgames.com",
35 "LAN": "la1.api.riotgames.com",
36 "LAS": "la2.api.riotgames.com",
37 "NA": "na1.api.riotgames.com",
38 "OCE": "oc1.api.riotgames.com",
39 "TR": "tr1.api.riotgames.com",
40 "RU": "ru.api.riotgames.com",
41 "PBE": "pbe1.api.riotgames.com",
44 async function riotRequest(region, url, params, retries) {
45 if (retries < 1) throw "Error too many tries";
46 let req = "https://" + regions[region] + url + "?api_key=" + key;
48 req += "&" + p + "=" + params[p];
51 let result = await request({uri: req, resolveWithFullResponse: true, json: true});
52 let appRateLimit = result.headers["x-app-rate-limit"];
53 let appRateLimitCount = result.headers["x-app-rate-limit-count"];
54 appRateLimit1 = appRateLimit.split(",")[0].split(":")[0];
55 appRateLimit120 = appRateLimit.split(",")[1].split(":")[0];
56 appRateLimitCount1 = appRateLimitCount.split(",")[0].split(":")[0];
57 appRateLimitCount120 = appRateLimitCount.split(",")[1].split(":")[0];
58 let delay1 = 1000 / (appRateLimit1 - 1);
59 let delay120 = 120000 / (appRateLimit120 - 1);
60 await sleep(Math.max(delay1, delay120));
61 console.log(Math.max(delay1, delay120));
64 console.log(err.message);
65 return await riotRequest(region, url, params, retries - 1);
69 async function getAllMatches(region, accountId) {
75 let m = await riotRequest(region, "/lol/match/v4/matchlists/by-account/" + accountId, { beginIndex: bI, endIndex: eI }, 5);
77 totalGames = m.totalGames;
78 matches = matches.concat(m.matches);
79 console.log("Added games " + bI + " to " + eI + ", " + matches.length + " of " + totalGames);
82 } while (bI <= totalGames);
91 function getChampions(cb) {
92 request("http://ddragon.leagueoflegends.com/cdn/" + patch + "/data/en_US/champion.json", (err, res, body) => {
93 champions = JSON.parse(body).data;
94 for (c in champions) {
95 champLookup[champions[c].key] = c;
100 app.get("/lol/champions", (req, res) => {
101 if (champions == null)
103 res.send(JSON.stringify(Object.keys(champions)));
106 res.send(JSON.stringify(Object.keys(champions)));
108 app.get("/lol/champlookup", (req, res) => {
109 if (champions == null)
111 res.send(JSON.stringify(champLookup));
114 res.send(JSON.stringify(champLookup));
117 if (fs.existsSync("users.js")) {
118 fs.readFile("users.js", (err, data) => {
119 users = JSON.parse(data);
122 app.get("/lol/matches", async (req, res) => {
123 let region = req.query.region;
124 let summoner = req.query.summoner;
125 let regionUrl = regions[region];
127 let data = await riotRequest(region, "/lol/summoner/v4/summoners/by-name/" + summoner, {}, 5);
128 let accountId = data.accountId;
129 if (users[accountId]) {
130 res.send(JSON.stringify(users[accountId]));
133 let matches = await getAllMatches(region, accountId);
134 users[accountId] = matches;
135 fs.writeFile("users.js", JSON.stringify(users), (err) => {
136 if (err) console.log("Error writing file: " + err);
138 res.send(JSON.stringify(matches));
146 app.listen(port, () => {
147 console.log("Listening on port %d", port)