aboutsummaryrefslogtreecommitdiff
path: root/hypixelApi.ts
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-02-13 14:13:42 -0600
committermat <27899617+mat-1@users.noreply.github.com>2021-02-13 14:13:42 -0600
commit52e38809212133ef673d11bfa96ba3bb43c3644c (patch)
treef54408afd41cc64b64d5e82a3ad814b1bb55d4a7 /hypixelApi.ts
parenta23103ec24128f2e24b93ad101ade6dfdd4758c3 (diff)
downloadskyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.tar.gz
skyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.tar.bz2
skyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.zip
move stuff into src folder
Diffstat (limited to 'hypixelApi.ts')
-rw-r--r--hypixelApi.ts154
1 files changed, 0 insertions, 154 deletions
diff --git a/hypixelApi.ts b/hypixelApi.ts
deleted file mode 100644
index e91d4f5..0000000
--- a/hypixelApi.ts
+++ /dev/null
@@ -1,154 +0,0 @@
-import fetch from 'node-fetch'
-import { jsonToQuery, shuffle } from './util'
-import { Agent } from 'https'
-require('dotenv').config()
-
-// We need to create an agent to prevent memory leaks and to only do dns lookups once
-const httpsAgent = new Agent({
- keepAlive: true
-})
-
-/* Lower level code related to the Hypixel api */
-
-const apiKeys = process.env.keys.split(' ')
-
-interface KeyUsage {
- remaining: number
- limit: number
- reset: number
-}
-
-const apiKeyUsage: { [ key: string ]: KeyUsage } = {}
-
-
-const baseHypixelAPI = 'https://api.hypixel.net'
-
-/** Choose the best current API key */
-export function chooseApiKey(): string {
- // find the api key with the lowest amount of uses
- let bestKeyUsage: KeyUsage = null
- let bestKey: string = null
- for (var key of shuffle(apiKeys)) {
- const keyUsage = apiKeyUsage[key]
-
- // if the key has never been used before, use it
- if (!keyUsage) return key
-
- // if the key has reset since the last use, set the remaining count to the default
- if (Date.now() > keyUsage.reset)
- keyUsage.remaining = keyUsage.limit
-
- // if this key has more uses remaining than the current known best one, save it
- if (!bestKeyUsage || keyUsage.remaining > bestKeyUsage.remaining) {
- bestKeyUsage = keyUsage
- bestKey = key
- }
- }
- return bestKey
-}
-
-export interface HypixelResponse {
- [key: string]: any | {
- success: boolean
- throttled?: boolean
- }
-}
-
-
-export interface HypixelPlayerStatsSkyBlockProfiles {
- [ uuid: string ]: {
- profile_id: string
- cute_name: string
- }
-}
-
-interface HypixelPlayerStatsSkyBlock {
- profiles: HypixelPlayerStatsSkyBlockProfiles
-}
-
-export interface HypixelPlayerSocialMedia {
- YOUTUBE?: string
- prompt: boolean
- links: {
- DISCORD?: string
- HYPIXEL?: string
- }
-}
-
-export interface HypixelPlayer {
- _id: string
- achievementsOneTime: string[]
- displayname: string
-
- firstLogin: number,
- lastLogin: number,
- lastLogout: number
-
- knownAliases: string[],
- knownAliasesLower: string[]
-
- networkExp: number
- playername: string
- stats: {
- SkyBlock: HypixelPlayerStatsSkyBlock
- [ name: string ]: any
- },
- timePlaying: number,
- uuid: string,
- achievements: { [ name: string ]: number },
- petConsumables: { [ name: string ]: number },
- vanityMeta: {
- packages: string[]
- },
-
- language: string,
- userLanguage?: string
-
- packageRank?: string
- newPackageRank?: string
- rankPlusColor?: string
- monthlyPackageRank?: string
- rank?: string
- prefix?: string
-
- claimed_potato_talisman?: number
- skyblock_free_cookie?: number
-
- socialMedia?: HypixelPlayerSocialMedia
-}
-
-
-/** Send an HTTP request to the Hypixel API */
-export async function sendApiRequest({ path, key, args }): Promise<HypixelResponse> {
- console.log('sending api request to', path, args)
- // Send a raw http request to api.hypixel.net, and return the parsed json
-
- if (key)
- // If there's an api key, add it to the arguments
- args.key = key
-
- // Construct a url from the base api url, path, and arguments
- const fetchUrl = baseHypixelAPI + '/' + path + '?' + jsonToQuery(args)
-
- const fetchResponse = await fetch(
- fetchUrl,
- { agent: () => httpsAgent }
- )
-
- if (fetchResponse.headers['ratelimit-limit'])
- // remember how many uses it has
- apiKeyUsage[key] = {
- remaining: fetchResponse.headers['ratelimit-remaining'],
- limit: fetchResponse.headers['ratelimit-limit'],
- reset: Date.now() + parseInt(fetchResponse.headers['ratelimit-reset']) * 1000
- }
-
- const fetchJsonParsed = await fetchResponse.json()
- if (fetchJsonParsed.throttle) {
- apiKeyUsage[key].remaining = 0
- console.log('throttled :(')
- return { throttled: true }
- }
- return fetchJsonParsed
-}
-