-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiconsDownload.ts
82 lines (71 loc) · 2.63 KB
/
iconsDownload.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
import { writeFile, mkdirSync, existsSync } from 'fs';
import { join, basename } from 'path';
import { request } from 'https';
import { request as httpRequest } from 'http';
const IMAGES = 'https://minehut-server-icons-live.s3.us-west-2.amazonaws.com';
const missingIcons = [
'BEDROCK'
]
const icons = await fetch('https://api.minehut.com/servers/icons').then(res => res.json());
let i = 0;
for (const icon of icons) {
i++;
let imageUrl = `${IMAGES}/${icon.icon_name}.png`;
if (await fileExists(`icons/${basename(imageUrl.toLowerCase())}`)) {
console.log(`Skipping ${i}/${icons.length} icons`);
continue;
}
if (missingIcons.includes(icon.icon_name)) {
console.log(`Skipping ${i}/${icons.length} icons`);
continue;
}
if (icon.icon_name === 'CAVE_VINES_PLANT') {
imageUrl = `${IMAGES}/CAVE_VINES.png`;
}
await downloadImage(imageUrl, 'icons');
console.log(`Downloaded ${i}/${icons.length} icons`);
await wait(2000);
}
async function downloadImage(imageUrl: string, saveFolder: string): Promise<void> {
const protocol = imageUrl.startsWith('https') ? request : httpRequest;
const fileName = basename(imageUrl);
const savePath = join(saveFolder, fileName.toLowerCase());
// Ensure the save folder exists
if (!existsSync(saveFolder)) {
mkdirSync(saveFolder, { recursive: true });
}
// Download the image
await new Promise<void>((resolve, reject) => {
const req = protocol(imageUrl, async (response) => {
if (response.statusCode !== 200) {
reject(new Error(`Failed to download image ${imageUrl}. Status code: ${response.statusCode}`));
return;
}
const chunks: Buffer[] = [];
response.on('data', (chunk) => chunks.push(chunk));
response.on('end', () => {
// @ts-ignore
const buffer = Buffer.concat(chunks);
// @ts-ignore
writeFile(savePath, buffer, (err) => {
if (err) {
reject(err);
} else {
console.log(`Image downloaded and saved to ${savePath}`);
resolve();
}
});
});
});
req.on('error', (err) => reject(err));
req.end();
});
}
async function fileExists(path: string): Promise<boolean> {
return new Promise(resolve => {
existsSync(path) ? resolve(true) : resolve(false);
});
}
async function wait(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}