forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit3d.ts
More file actions
90 lines (80 loc) · 2.43 KB
/
Copy pathunit3d.ts
File metadata and controls
90 lines (80 loc) · 2.43 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
// src/lib/adapters/unit3d.ts
import { parseBytes } from "@/lib/parser"
import { adapterFetch } from "./adapter-fetch"
import type { DebugApiCall, FetchOptions, TrackerAdapter, TrackerStats } from "./types"
interface Unit3dApiResponse {
username: string
group: string
uploaded: string
downloaded: string
ratio: string
buffer: string
seeding: number
leeching: number
seedbonus: string
hit_and_runs: number
}
export class Unit3dAdapter implements TrackerAdapter {
async fetchStats(
baseUrl: string,
apiToken: string,
apiPath: string,
options?: FetchOptions
): Promise<TrackerStats> {
const url = new URL(apiPath, baseUrl)
const hostname = new URL(baseUrl).hostname
const headers: Record<string, string> =
options?.unit3dAuthStyle === "bearer" ? { Authorization: `Bearer ${apiToken}` } : {}
if (options?.unit3dAuthStyle !== "bearer") {
url.searchParams.set("api_token", apiToken)
}
const data = await adapterFetch<Unit3dApiResponse>(url.toString(), hostname, options, headers)
return {
username: data.username,
group: data.group,
uploadedBytes: parseBytes(data.uploaded),
downloadedBytes: parseBytes(data.downloaded),
ratio: parseFloat(data.ratio) || 0,
bufferBytes: parseBytes(data.buffer),
seedingCount: data.seeding,
leechingCount: data.leeching,
seedbonus: parseFloat(data.seedbonus) || 0,
hitAndRuns: data.hit_and_runs,
requiredRatio: null,
warned: null,
freeleechTokens: null,
}
}
async fetchRaw(
baseUrl: string,
apiToken: string,
apiPath: string,
options?: FetchOptions
): Promise<DebugApiCall[]> {
const url = new URL(apiPath, baseUrl)
const hostname = new URL(baseUrl).hostname
const headers: Record<string, string> =
options?.unit3dAuthStyle === "bearer" ? { Authorization: `Bearer ${apiToken}` } : {}
if (options?.unit3dAuthStyle !== "bearer") {
url.searchParams.set("api_token", apiToken)
}
try {
const data = await adapterFetch<Record<string, unknown>>(
url.toString(),
hostname,
options,
headers
)
return [{ label: "User Stats", endpoint: apiPath, data, error: null }]
} catch (err) {
return [
{
label: "User Stats",
endpoint: apiPath,
data: null,
error: err instanceof Error ? err.message : "Request failed",
},
]
}
}
}