Icon caching

This commit is contained in:
2026-06-26 20:52:54 -04:00
parent aeffa30417
commit 73f1453daf
15 changed files with 29 additions and 60 deletions

View File

@@ -1,48 +1,22 @@
import {existsSync, mkdirSync, writeFileSync} from 'fs';
import {resolve, dirname} from 'path';
import {fileURLToPath} from 'url';
import * as path from 'node:path';
const DIR = resolve(dirname(fileURLToPath(import.meta.url)), 'public', 'icons');
const DIR = resolve(dirname(fileURLToPath(import.meta.url)), '../data/icons');
export const WMO = {
0: {label: 'Clear Sky', icon: '01d'},
1: {label: 'Mainly Clear', icon: '01d'},
2: {label: 'Partly Cloudy', icon: '02d'},
3: {label: 'Overcast', icon: '04d'},
45: {label: 'Fog', icon: '50d'},
48: {label: 'Icy Fog', icon: '50d'},
51: {label: 'Light Drizzle', icon: '09d'},
53: {label: 'Drizzle', icon: '09d'},
55: {label: 'Heavy Drizzle', icon: '09d'},
61: {label: 'Light Rain', icon: '10d'},
63: {label: 'Rain', icon: '10d'},
65: {label: 'Heavy Rain', icon: '10d'},
71: {label: 'Light Snow', icon: '13d'},
73: {label: 'Snow', icon: '13d'},
75: {label: 'Heavy Snow', icon: '13d'},
77: {label: 'Snow Grains', icon: '13d'},
80: {label: 'Light Showers', icon: '09d'},
81: {label: 'Showers', icon: '09d'},
82: {label: 'Heavy Showers', icon: '09d'},
85: {label: 'Snow Showers', icon: '13d'},
86: {label: 'Heavy Snow Showers', icon: '13d'},
95: {label: 'Thunderstorm', icon: '11d'},
96: {label: 'Thunderstorm w/ Hail', icon: '11d'},
99: {label: 'Thunderstorm w/ Hail', icon: '11d'},
};
export async function downloadIcon(icon, path) {
if(!existsSync(DIR)) mkdirSync(dirname(path), {recursive: true});
const res = await fetch(`https://openweathermap.org/img/wn/${icon}@2x.png`);
const buf = Buffer.from(await res.arrayBuffer());
writeFileSync(path, buf);
}
export async function downloadIcons() {
if(!existsSync(DIR)) mkdirSync(DIR, {recursive: true});
const icons = new Set(Object.values(WMO).map(w => [w.icon, w.icon.replace('d', 'n')]).flat());
await Promise.all([...icons].map(async icon => {
const path = resolve(DIR, `${icon}.png`);
if(existsSync(path)) return;
const res = await fetch(`https://openweathermap.org/img/wn/${icon}@2x.png`);
const buf = Buffer.from(await res.arrayBuffer());
writeFileSync(path, buf);
console.log(` 📥 Downloaded icon: ${icon}.png`);
}));
console.log('✅ Weather icons ready');
export async function fetchIcon(icon) {
const iconPath = path.join(DIR, icon + '.png');
if(!existsSync(DIR) || !existsSync(iconPath))
await downloadIcon(icon, iconPath);
return iconPath;
}
export function getWeatherCondition(data) {