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);
29 async function riotRequest(region, url, params, retries) {
30 if (retries < 1) throw "Error too many tries";
31 let req = "https://" + regions[region] + url + "?api_key=" + key;
33 req += "&" + p + "=" + params[p];
36 let result = await request({uri: req, resolveWithFullResponse: true, json: true});
37 let appRateLimit = result.headers["x-app-rate-limit"];
38 let appRateLimitCount = result.headers["x-app-rate-limit-count"];
39 appRateLimit1 = appRateLimit.split(",")[0].split(":")[0];
40 appRateLimit120 = appRateLimit.split(",")[1].split(":")[0];
41 appRateLimitCount1 = appRateLimitCount.split(",")[0].split(":")[0];
42 appRateLimitCount120 = appRateLimitCount.split(",")[1].split(":")[0];
43 let delay1 = 1000 / (appRateLimit1 - 1);
44 let delay120 = 120000 / (appRateLimit120 - 1);
45 await sleep(Math.max(delay1, delay120));
46 console.log(Math.max(delay1, delay120));
49 console.log(err.message);
50 return await riotRequest(region, url, params, retries - 1);
54 async function getAllMatches(region, accountId) {
60 let m = await riotRequest(region, "/lol/match/v4/matchlists/by-account/" + accountId, { beginIndex: bI, endIndex: eI }, 5);
62 totalGames = m.totalGames;
63 matches = matches.concat(m.matches);
64 console.log("Added games " + bI + " to " + eI + ", " + matches.length + " of " + totalGames);
67 } while (bI <= totalGames);
76 "BR": "br1.api.riotgames.com",
77 "EUNE": "eun1.api.riotgames.com",
78 "EUW": "euw1.api.riotgames.com",
79 "JP": "jp1.api.riotgames.com",
80 "KR": "kr.api.riotgames.com",
81 "LAN": "la1.api.riotgames.com",
82 "LAS": "la2.api.riotgames.com",
83 "NA": "na1.api.riotgames.com",
84 "OCE": "oc1.api.riotgames.com",
85 "TR": "tr1.api.riotgames.com",
86 "RU": "ru.api.riotgames.com",
87 "PBE": "pbe1.api.riotgames.com",
89 app.get("/lol/regions", (req, res) => {
90 res.send(JSON.stringify(regions));
93 function getChampions(cb) {
94 request("http://ddragon.leagueoflegends.com/cdn/" + patch + "/data/en_US/champion.json", (err, res, body) => {
95 champions = JSON.parse(body).data;
99 app.get("/lol/champions", (req, res) => {
100 if (champions == null)
102 res.send(JSON.stringify(Object.keys(champions)));
105 res.send(JSON.stringify(Object.keys(champions)));
108 if (fs.existsSync("users.js")) {
109 fs.readFile("users.js", (err, data) => {
110 users = JSON.parse(data);
113 app.get("/lol/matches", async (req, res) => {
114 let region = req.query.region;
115 let summoner = req.query.summoner;
116 let regionUrl = regions[region];
118 let data = await riotRequest(region, "/lol/summoner/v4/summoners/by-name/" + summoner, {}, 5);
119 let accountId = data.accountId;
120 if (users[accountId]) {
121 res.send(JSON.stringify(users[accountId]));
124 let matches = await getAllMatches(region, accountId);
125 users[accountId] = matches;
126 fs.writeFile("users.js", JSON.stringify(users), (err) => {
127 console.log("Error writing file: " + err);
137 app.listen(port, () => {
138 console.log("Listening on port %d", port)