From d5445483f1dbbeb4048da5e3ff4a606ce7355bc1 Mon Sep 17 00:00:00 2001 From: mat Date: Thu, 24 Jun 2021 16:04:41 -0500 Subject: log api keys --- build/hypixelApi.js | 4 ++-- src/hypixelApi.ts | 5 +++-- src/util.ts | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/build/hypixelApi.js b/build/hypixelApi.js index 5009506..d6ad066 100644 --- a/build/hypixelApi.js +++ b/build/hypixelApi.js @@ -20,6 +20,7 @@ const httpsAgent = new https_1.Agent({ }); /** This array should only ever contain one item because using multiple hypixel api keys isn't allowed :) */ const apiKeys = (_c = (_b = (_a = process.env) === null || _a === void 0 ? void 0 : _a.hypixel_keys) === null || _b === void 0 ? void 0 : _b.split(' ')) !== null && _c !== void 0 ? _c : []; +console.log(apiKeys); const apiKeyUsage = {}; const baseHypixelAPI = 'https://api.hypixel.net'; /** Choose the best current API key */ @@ -27,7 +28,7 @@ function chooseApiKey() { // find the api key with the lowest amount of uses let bestKeyUsage = null; let bestKey = null; - for (let key of util_1.shuffle(apiKeys)) { + for (let key of util_1.shuffle(apiKeys.slice())) { const keyUsage = apiKeyUsage[key]; // if the key has never been used before, use it if (!keyUsage) @@ -70,7 +71,6 @@ async function sendApiRequest({ path, key, args }) { try { fetchResponse = await node_fetch_1.default(fetchUrl, { agent: () => httpsAgent }); fetchJsonParsed = await fetchResponse.json(); - console.log('gotten api response', fetchJsonParsed, key); } catch { // if there's an error, wait a second and try again diff --git a/src/hypixelApi.ts b/src/hypixelApi.ts index 0987ab5..9ce167f 100644 --- a/src/hypixelApi.ts +++ b/src/hypixelApi.ts @@ -19,6 +19,8 @@ const httpsAgent = new Agent({ /** This array should only ever contain one item because using multiple hypixel api keys isn't allowed :) */ const apiKeys = process.env?.hypixel_keys?.split(' ') ?? [] +console.log(apiKeys) + interface KeyUsage { remaining: number limit: number @@ -36,7 +38,7 @@ export function chooseApiKey(): string { // find the api key with the lowest amount of uses let bestKeyUsage: KeyUsage = null let bestKey: string = null - for (let key of shuffle(apiKeys)) { + for (let key of shuffle(apiKeys.slice())) { const keyUsage = apiKeyUsage[key] // if the key has never been used before, use it @@ -158,7 +160,6 @@ export async function sendApiRequest({ path, key, args }): Promise httpsAgent } ) fetchJsonParsed = await fetchResponse.json() - console.log('gotten api response', fetchJsonParsed, key) } catch { // if there's an error, wait a second and try again await new Promise((resolve) => setTimeout(resolve, 1000)) diff --git a/src/util.ts b/src/util.ts index 43bb2d8..67cf4b5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -11,7 +11,7 @@ export function jsonToQuery(data): string { return Object.entries(data || {}).map(e => e.join('=')).join('&') } -export function shuffle(a): string { +export function shuffle(a: T[]): T[] { for (let i = a.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)) ;[a[i], a[j]] = [a[j], a[i]] -- cgit