forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathggn.ts
More file actions
229 lines (206 loc) · 6.67 KB
/
Copy pathggn.ts
File metadata and controls
229 lines (206 loc) · 6.67 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// src/lib/adapters/ggn.ts
import { computeBufferBytes, floatBytesToBigInt } from "@/lib/data-transforms"
import { adapterFetch } from "./adapter-fetch"
import type {
DebugApiCall,
FetchOptions,
GGnPlatformMeta,
TrackerAdapter,
TrackerStats,
} from "./types"
interface GGnQuickUserResponse {
status: string
error?: string
response?: {
username: string
id: number
}
}
interface GGnUserResponse {
status: string
error?: string
response?: {
id: number
username: string
stats: {
uploaded: number
downloaded: number
ratio: string | number
requiredRatio: number
gold: number
joinedDate: string | null
lastAccess?: string | null
onIRC: boolean
shareScore: number | null
}
personal: {
class: string
hnrs: number | null
warned: boolean
donor: boolean
enabled: boolean
parked: boolean
invites: number | null
}
community: {
seeding: number | null
leeching: number | null
hourlyGold: number | null
snatched: number | null
uniqueSnatched: number | null
}
buffs?: Record<string, number>
achievements?: {
userLevel: string
nextLevel: string
totalPoints: number
pointsToNextLvl: number
}
}
}
export class GGnAdapter implements TrackerAdapter {
async fetchStats(
baseUrl: string,
apiToken: string,
apiPath: string,
options?: FetchOptions
): Promise<TrackerStats> {
const hostname = new URL(baseUrl).hostname
let userId: number
let username: string
if (options?.remoteUserId) {
// Skip quick_user call because we already resolved this user's ID on a prior poll
userId = options.remoteUserId
username = "" // Will be overwritten from the full user response
} else {
// Step 1: quick_user to get the user ID (first poll only)
const quickUrl = new URL(apiPath, baseUrl)
quickUrl.searchParams.set("request", "quick_user")
quickUrl.searchParams.set("key", apiToken)
const quickData = await adapterFetch<GGnQuickUserResponse>(
quickUrl.toString(),
hostname,
options
)
if (quickData.status !== "success") {
throw new Error(quickData.error ?? `GGn API returned status: ${quickData.status}`)
}
if (!quickData.response?.id || !quickData.response.username) {
throw new Error(`Unexpected response from ${hostname}: missing user ID or username`)
}
userId = quickData.response.id
username = quickData.response.username
}
// Step 2: full user profile for detailed stats
const userUrl = new URL(apiPath, baseUrl)
userUrl.searchParams.set("request", "user")
userUrl.searchParams.set("id", String(userId))
userUrl.searchParams.set("key", apiToken)
const userData = await adapterFetch<GGnUserResponse>(userUrl.toString(), hostname, options)
if (userData.status !== "success") {
throw new Error(userData.error ?? `GGn API returned status: ${userData.status}`)
}
const resp = userData.response
if (!resp?.stats) {
throw new Error(`Unexpected response from ${hostname}: missing stats`)
}
const uploaded = floatBytesToBigInt(resp.stats.uploaded)
const downloaded = floatBytesToBigInt(resp.stats.downloaded)
const ratio =
typeof resp.stats.ratio === "number" ? resp.stats.ratio : parseFloat(resp.stats.ratio) || 0
const platformMeta: GGnPlatformMeta = {
donor: resp.personal?.donor ?? false,
parked: resp.personal?.parked ?? false,
enabled: resp.personal?.enabled ?? true,
invites: resp.personal?.invites ?? 0,
onIRC: resp.stats.onIRC ?? false,
hourlyGold: resp.community?.hourlyGold ?? 0,
snatched: resp.community?.snatched ?? undefined,
uniqueSnatched: resp.community?.uniqueSnatched ?? undefined,
}
if (resp.buffs) platformMeta.buffs = resp.buffs
if (resp.achievements) platformMeta.achievements = resp.achievements
return {
username: resp.username || username,
group: resp.personal?.class ?? "Unknown",
remoteUserId: userId,
uploadedBytes: uploaded,
downloadedBytes: downloaded,
ratio,
bufferBytes: computeBufferBytes(uploaded, downloaded),
seedingCount: resp.community?.seeding ?? 0,
leechingCount: resp.community?.leeching ?? 0,
seedbonus: resp.stats.gold ?? 0,
hitAndRuns: resp.personal?.hnrs ?? null,
requiredRatio: typeof resp.stats.requiredRatio === "number" ? resp.stats.requiredRatio : null,
warned: resp.personal?.warned ?? false,
freeleechTokens: null,
joinedDate: resp.stats.joinedDate ?? undefined,
lastAccessDate: resp.stats.lastAccess ?? undefined,
shareScore: resp.stats.shareScore ?? undefined,
platformMeta,
}
}
async fetchRaw(
baseUrl: string,
apiToken: string,
apiPath: string,
options?: FetchOptions
): Promise<DebugApiCall[]> {
const hostname = new URL(baseUrl).hostname
const calls: DebugApiCall[] = []
let userId: number | undefined = options?.remoteUserId
// Call 1: Quick User (skip if we already have a cached user ID)
if (!userId) {
const quickUrl = new URL(apiPath, baseUrl)
quickUrl.searchParams.set("request", "quick_user")
quickUrl.searchParams.set("key", apiToken)
try {
const quickData = await adapterFetch<GGnQuickUserResponse>(
quickUrl.toString(),
hostname,
options
)
calls.push({
label: "Quick User",
endpoint: `${apiPath}?request=quick_user`,
data: quickData,
error: null,
})
userId = quickData.response?.id
} catch (err) {
calls.push({
label: "Quick User",
endpoint: `${apiPath}?request=quick_user`,
data: null,
error: err instanceof Error ? err.message : "Request failed",
})
return calls
}
}
// Call 2: Full User Profile
if (userId) {
const userUrl = new URL(apiPath, baseUrl)
userUrl.searchParams.set("request", "user")
userUrl.searchParams.set("id", String(userId))
userUrl.searchParams.set("key", apiToken)
const endpoint = `${apiPath}?request=user&id=${userId}`
try {
const userData = await adapterFetch<Record<string, unknown>>(
userUrl.toString(),
hostname,
options
)
calls.push({ label: "User Profile", endpoint, data: userData, error: null })
} catch (err) {
calls.push({
label: "User Profile",
endpoint,
data: null,
error: err instanceof Error ? err.message : "Request failed",
})
}
}
return calls
}
}