53 lines
2.4 KiB
JavaScript
53 lines
2.4 KiB
JavaScript
import { existsSync, mkdirSync, writeFileSync } from 'fs'
|
|
import { resolve, dirname } from 'path'
|
|
import { fileURLToPath } from 'url'
|
|
|
|
const DIR = resolve(dirname(fileURLToPath(import.meta.url)), 'public', '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 function weatherFromCode(code, isDay = true) {
|
|
const w = WMO[code] ?? { label: 'Unknown', icon: '01d' }
|
|
const icon = isDay ? w.icon : w.icon.replace('d', 'n')
|
|
return { weather_code: code, weather_label: w.label, weather_icon: `/icons/${icon}.png` }
|
|
}
|
|
|
|
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')
|
|
}
|