1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/**
* Fetch the raw Hypixel API
*/
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.hypixel_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
}
|