-
Notifications
You must be signed in to change notification settings - Fork 19
/
index-curseforge-releases.ts
178 lines (158 loc) · 4.85 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
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
import { writeFileSync } from "node:fs";
import { coerce, lte } from "semver";
import { CurseforgeRelease, ModLoader, ReleaseAssetType, ReleaseType } from "./lib/releases/types.js";
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function filterTruthy<T>(values: (T|undefined|null)[]): T[] {
return values.filter(Boolean) as T[];
}
export enum CurseforgeReleaseType {
RELEASE = 1,
BETA = 2,
ALPHA = 3,
}
// Describes shape of data observed that CF website API returns for each release
type ReleaseApiRecord = {
totalDownloads: number;
dateCreated: string;
releaseType: CurseforgeReleaseType;
displayName: string;
fileLength: number;
fileName: string;
id: number;
// These are labels for supported versions
gameVersions: string[];
};
/**
* Anything that looks like a Minecraft-Version is returned.
*/
function getGameVersions(record: ReleaseApiRecord) {
return record.gameVersions.filter((v) => v.match(/\d+\.\d+(\.\d+|)/));
}
function getModLoaders(record: ReleaseApiRecord): ModLoader[] {
let loaders = record.gameVersions.map((v) => {
switch (v) {
case "Forge":
return ModLoader.FORGE;
case "NeoForge":
return ModLoader.NEOFORGE;
case "Fabric":
return ModLoader.FABRIC;
default:
// Anything below 1.16.2 had to be Forge
if (v.match(/\d+\.\d+(\.\d+|)/)) {
if (lte(coerce(v) ?? "0.0.0", "1.16.1")) {
return ModLoader.FORGE;
}
}
}
});
const actualLoaders = filterTruthy(loaders); // Filter out undefined values
if (actualLoaders.length === 0) {
console.warn(
"Failed to determine the mod loader for Curseforge release %s",
record.displayName
);
}
return actualLoaders;
}
function convertReleaseType(releaseType: CurseforgeReleaseType): ReleaseType {
switch (releaseType) {
case CurseforgeReleaseType.BETA:
return ReleaseType.BETA;
case CurseforgeReleaseType.ALPHA:
return ReleaseType.ALPHA;
default:
return ReleaseType.STABLE;
}
}
function guessModVersionFromFilename(filename: string): string | undefined {
const m = filename.match(
/^appliedenergistics2-(?:forge-|fabric-|)(.+)\.jar$/
);
return m ? m[1] : undefined;
}
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.flatMap((record: ReleaseApiRecord) => {
const modVersion = guessModVersionFromFilename(record.fileName);
if (!modVersion) {
console.warn("Cannot deduce Mod version from Curseforge filename: %s", record.fileName);
return [];
}
return [{
source: "curseforge",
id: String(record.id),
url: "https://www.curseforge.com/minecraft/mc-mods/applied-energistics-2/files/" + record.id,
modVersion,
releaseType: convertReleaseType(record.releaseType),
gameVersions: getGameVersions(record),
modLoaders: getModLoaders(record),
published: new Date(record.dateCreated).getTime(),
totalDownloads: record.totalDownloads,
assets: {
[ReleaseAssetType.MOD]: {
filename: record.fileName,
size: record.fileLength,
browser_download_url: "https://www.curseforge.com/minecraft/mc-mods/applied-energistics-2/download/" + record.id
}
}
} satisfies CurseforgeRelease
];
});
}
const releases = await fetchReleases();
writeFileSync(
"caches/curseforge_releases.json",
JSON.stringify(releases, null, 2),
{
encoding: "utf-8"
}
);