]> gitweb.ps.run Git - lolstats/blob - index.js
users.hs koxnon
[lolstats] / index.js
1 const express = require("express")
2 const fs = require("fs")
3 const request = require("request-promise-native")
4 const app = express()
5 const port = 3000
6 const sleep = require('util').promisify(setTimeout)
7
8 const patch = "8.24.1";
9 const key = "RGAPI-c6cae96a-c4b0-4842-9017-ddd736f3f628";
10 let appRateLimit1;
11 let appRateLimit120;
12 let appRateLimitCount1;
13 let appRateLimitCount120;
14
15 let rules = [
16   ["/lol", "index.html"],
17   ["/lol/script.js", "script.js"],
18   ["/lol/style.css", "style.css"],
19 ];
20
21 for (i in rules) {
22   let path = rules[i][0];
23   let file = rules[i][1];
24   app.get(path, (req, res) => {
25     res.sendFile(__dirname + "/html/" + file);
26   });
27 }
28
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;
32   for (p in params) {
33     req += "&" + p + "=" + params[p];
34   }
35   try {
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));
47     return result.body;
48   } catch (err) {
49     console.log(err.message);
50     return await riotRequest(region, url, params, retries - 1);
51   }
52 }
53
54 async function getAllMatches(region, accountId) {
55   let matches = [];
56   let totalGames;
57   let bI = 0, eI = 99;
58
59   do {
60     let m = await riotRequest(region, "/lol/match/v4/matchlists/by-account/" + accountId, { beginIndex: bI, endIndex: eI }, 5);
61     console.log(m);
62     totalGames = m.totalGames;
63     matches = matches.concat(m.matches);
64     console.log("Added games " + bI + " to " + eI + ", " + matches.length + " of " + totalGames);
65     bI = eI + 1;
66     eI += 100;
67   } while (bI <= totalGames);
68
69   return matches;
70 }
71
72 // Static Data
73 // -----------
74
75 const regions = {
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",
88 };
89 app.get("/lol/regions", (req, res) => {
90   res.send(JSON.stringify(regions));
91 });
92 let champions = null;
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;
96     cb();
97   });
98 }
99 app.get("/lol/champions", (req, res) => {
100   if (champions == null)
101     getChampions(() => {
102       res.send(JSON.stringify(Object.keys(champions)));
103     });
104   else
105     res.send(JSON.stringify(Object.keys(champions)));
106 });
107 let users = {};
108 if (fs.existsSync("users.js")) {
109   fs.readFile("users.js", (err, data) => {
110     users = JSON.parse(data);
111   });
112 }
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];
117   try {
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]));
122       return;
123     }
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);
128     });
129     res.send(matches);
130   } catch (err) {
131     console.log(err);
132   }
133 });
134
135 // -----------
136
137 app.listen(port, () => {
138   console.log("Listening on port %d", port)
139 });