-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheads.ts
More file actions
79 lines (71 loc) · 2.02 KB
/
heads.ts
File metadata and controls
79 lines (71 loc) · 2.02 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
// Download the entire database of minecraft-heads.com as a JSON file
import { parse } from "https://deno.land/std@0.113.0/flags/mod.ts";
const Config = {
defaultFile: `heads.json`,
textureUrlPrefix: "http://textures.minecraft.net/texture/",
categories: [
"alphabet",
"animals",
"blocks",
"decoration",
"food-drinks",
"humans",
"humanoid",
"miscellaneous",
"monsters",
"plants",
],
};
async function main(args: string[]) {
const flags = parse(args, {
default: { outFile: Config.defaultFile },
alias: { outFile: "o" },
});
Deno.createSync(flags.outFile);
console.log(`Data will be written to ${flags.outFile}`);
const data = [];
for (const category of Config.categories) {
console.log(`Downloading data for ${category}...`);
const res = await fetch(
`https://minecraft-heads.com/scripts/api.php?cat=${category}&tags=true`,
);
if (!res.ok) {
throw Error(`Received HTTP ${res.status} ${res.statusText} from API.`);
}
const jsonResult = await res.json();
if (jsonResult != null) {
for (const entry of jsonResult) {
data.push({
name: entry["name"],
uuid: entry["uuid"],
category: category,
value: entry["value"],
hash: extractHash(entry["value"]),
tags: formatTags(entry["tags"]),
});
}
}
}
console.log("Writing to file...");
Deno.writeTextFile(flags.outFile, JSON.stringify(data));
console.log(`Done! ${data.length} entries written to ${flags.outFile}.`);
}
function extractHash(base64data: string) {
const obj = JSON.parse(atob(base64data));
const url = obj["textures"]["SKIN"]["url"];
if (!url.startsWith(Config.textureUrlPrefix)) {
throw Error("Unexpected URL format: " + url);
}
return url.replaceAll(Config.textureUrlPrefix, "");
}
function formatTags(tags: string | null) {
if (tags == null) {
return "";
} else {
return tags.split(",");
}
}
main(Deno.args).catch((e) => {
console.error(`[ERROR] ${e}`);
Deno.exit(1);
});