-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-curseforge-releases.ts
98 lines (85 loc) · 2.32 KB
/
index-curseforge-releases.ts
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
import { writeFileSync } from "node:fs";
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export type CurseforgeReleaseCache = {
releases: CurseforgeRelease[];
};
export enum CurseforgeReleaseType {
RELEASE = 1,
BETA = 2,
ALPHA = 3,
}
export type CurseforgeRelease = {
id: number;
filename: string;
displayName: string;
type: CurseforgeReleaseType;
gameVersions: string[];
published: string;
};
async function fetchReleases(): Promise<CurseforgeRelease[]> {
const urlPattern =
"https://www.curseforge.com/api/v1/mods/223794/files?pageIndex=%PAGE%&pageSize=%PAGESIZE%&sort=dateCreated&sortDescending=true&removeAlphas=false";
const pageSize = 50;
const data: any[] = [];
let nextPageIndex: number | undefined = 0;
while (nextPageIndex !== undefined) {
const url = urlPattern
.replace("%PAGESIZE%", "" + pageSize)
.replace("%PAGE%", "" + nextPageIndex);
console.info("Requesting %s", url);
const response = await fetch(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0",
},
});
if (!response.ok) {
throw new Error(
"Failed to fetch. " +
response.status +
" (" +
(await response.text()) +
")"
);
}
const page = await response.json();
const { index, totalCount } = page.pagination;
if (Array.isArray(page.data)) {
data.push(...page.data);
}
if ((index + 1) * pageSize <= totalCount) {
nextPageIndex = index + 1;
} else {
nextPageIndex = undefined;
}
await sleep(1000);
}
// Dedup
for (let i = 0; i < data.length; i++) {
const id = data[i].id;
for (let j = i + 1; j < data.length; j++) {
if (data[j].id === id) {
console.info("Removing duplicate file %d", id);
data.splice(j, 1);
}
}
}
return data.map((record: any) => ({
id: record.id,
filename: record.fileName,
displayName: record.displayName,
type: record.releaseType,
gameVersions: record.gameVersions,
published: record.dateCreated,
}));
}
const releases = await fetchReleases();
writeFileSync(
"caches/curseforge_releases.json",
JSON.stringify(releases, null, 2),
{
encoding: "utf-8",
}
);