From: Patrick Schönberger Date: Fri, 21 Dec 2018 16:35:05 +0000 (+0100) Subject: changes X-Git-Url: https://gitweb.ps.run/lolstats/commitdiff_plain/8897f7defca50437c54da9e57b0ef52a6b6089d6 changes --- diff --git a/data/EUW/n1MbjDU6aQTDyRsOksU41Vn8sFhqZqaldWPQmOJ16NCiYw.json b/data/EUW/n1MbjDU6aQTDyRsOksU41Vn8sFhqZqaldWPQmOJ16NCiYw.json index 9e5dd1e..202aff5 100644 --- a/data/EUW/n1MbjDU6aQTDyRsOksU41Vn8sFhqZqaldWPQmOJ16NCiYw.json +++ b/data/EUW/n1MbjDU6aQTDyRsOksU41Vn8sFhqZqaldWPQmOJ16NCiYw.json @@ -38170,6 +38170,36 @@ "timestamp": 1545254672781, "role": "SOLO", "lane": "MID" + }, + { + "platformId": "EUW1", + "gameId": 3870286201, + "champion": 101, + "queue": 420, + "season": 11, + "timestamp": 1545337897797, + "role": "SOLO", + "lane": "MID" + }, + { + "platformId": "EUW1", + "gameId": 3870397501, + "champion": 45, + "queue": 400, + "season": 11, + "timestamp": 1545344475813, + "role": "SOLO", + "lane": "MID" + }, + { + "platformId": "EUW1", + "gameId": 3870435234, + "champion": 99, + "queue": 400, + "season": 11, + "timestamp": 1545346303306, + "role": "SOLO", + "lane": "MID" } ], "account": {} diff --git a/index.js b/index.js index c06f1fe..b8ddb22 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ const fs = require("fs") const request = require("request-promise-native") -const sleep = require("util").promisify(setTimeout) const low = require("lowdb") const FileAsync = require("lowdb/adapters/FileAsync") @@ -11,8 +10,8 @@ const port = 3000 const patch = "8.24.1"; const key = "RGAPI-dce1850a-0954-4b1e-8072-77a26739f076"; -let appRateLimit1; -let appRateLimit120; +let appRateLimit1 = 10; +let appRateLimit120 = 100; let appRateLimitCount1; let appRateLimitCount120; @@ -50,35 +49,47 @@ const regions = { "PBE": "pbe1.api.riotgames.com", }; -async function riotRequest(region, url, params, retries) { - if (retries < 1) throw "Error too many tries"; +let requests = []; + +async function executeRequest() { + if (requests.length > 0) { + let req = requests.shift(); + if (req.retries < 1) req.rej(); + else { + try { + console.log("[Info] request: " + req.url); + let result = await request({uri: req.url, 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]; + + if (typeof result.body == "object") req.res(result.body); + else req.res(JSON.parse(result.body)); + } catch (err) { + console.log("[Error] riotRequest: " + err.message); + req.retries--; + requests.push(req); + } + } + } + let delay1 = 1000 / (appRateLimit1 - 1); + let delay120 = 120000 / (appRateLimit120 - 1); + setTimeout(executeRequest, Math.max(delay1, delay120)); +} +executeRequest(); +async function riotRequest(region, url, params, retries) { let req = "https://" + regions[region] + url + "?api_key=" + key; for (p in params) { req += "&" + p + "=" + params[p]; } - - try { - console.log("[Info] request: " + req); - 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)); - - if (typeof result.body == "object") return result.body; - return JSON.parse(result.body); - } catch (err) { - console.log("[Error] riotRequest: " + err.message); - return await riotRequest(region, url, params, retries - 1); - } + return new Promise((resolve, reject) => { + requests.push({ url: req, retries: 5, res: resolve, rej: reject }); + }); } // ---------------- @@ -90,7 +101,7 @@ let nameLookup = {}; async function loadUserDb(region, accountId) { if (!users[region]) users[region] = {}; if (users[region][accountId]) return; - let adapter = new FileAsync(region + "/" + accountId + ".json"); + let adapter = new FileAsync("data/" + region + "/" + accountId + ".json"); let db = await low(adapter); await db.defaults({ games: [], gameSummaries: [], account: {} }) .write();