Added daytime property
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { existsSync, mkdirSync, writeFileSync } from 'fs'
|
||||
import { resolve, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
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')
|
||||
const DIR = resolve(dirname(fileURLToPath(import.meta.url)), 'public', 'icons');
|
||||
|
||||
export const WMO = {
|
||||
0: {label: 'Clear Sky', icon: '01d'},
|
||||
@@ -29,48 +29,49 @@ export const WMO = {
|
||||
95: {label: 'Thunderstorm', icon: '11d'},
|
||||
96: {label: 'Thunderstorm w/ Hail', icon: '11d'},
|
||||
99: {label: 'Thunderstorm w/ Hail', icon: '11d'},
|
||||
}
|
||||
};
|
||||
|
||||
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())
|
||||
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')
|
||||
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 function getWeatherCondition(data) {
|
||||
// Determine primary condition
|
||||
if (data.lightning_rate > 0 && data.raining === "True") {
|
||||
return { label: "Thunderstorm", code: 211, icon: "11d" };
|
||||
if(data.lightning_rate > 0 && data.raining === 'True') {
|
||||
return {label: 'Thunderstorm', code: 211, icon: '11d'};
|
||||
}
|
||||
|
||||
if (data.raining === "True") {
|
||||
if (data.precipitation > 7.6) return { label: "Heavy Rain", code: 502, icon: "10d" };
|
||||
if (data.precipitation > 2.5) return { label: "Moderate Rain", code: 501, icon: "10d" };
|
||||
return { label: "Light Rain", code: 500, icon: "09d" };
|
||||
if(data.raining === 'True') {
|
||||
if(data.precipitation > 7.6) return {label: 'Heavy Rain', code: 502, icon: '10d'};
|
||||
if(data.precipitation > 2.5) return {label: 'Moderate Rain', code: 501, icon: '10d'};
|
||||
return {label: 'Light Rain', code: 500, icon: '09d'};
|
||||
}
|
||||
|
||||
if(data.visibility < 10) {
|
||||
return { label: "Mist", code: 701, icon: "50d" };
|
||||
return {label: 'Mist', code: 701, icon: '50d'};
|
||||
}
|
||||
|
||||
console.log('daytime:',data.daytime);
|
||||
// Cloud-based conditions
|
||||
if(data.daytime == null)
|
||||
data.daytime = data.time.getTime() >= new Date(data.sunrise).getTime() && data.time.getTime() <= new Date(data.sunset).getTime();
|
||||
const isDay = data.daytime;
|
||||
const dayNight = isDay ? "d" : "n";
|
||||
console.log(data.time, data.sunrise, data.sunset, data.daytime);
|
||||
const dayNight = data.daytime ? 'd' : 'n';
|
||||
|
||||
if (data.clouds > 85) return { label: "Overcast Clouds", code: 804, icon: `04${dayNight}` };
|
||||
if (data.clouds > 50) return { label: "Broken Clouds", code: 803, icon: `04${dayNight}` };
|
||||
if (data.clouds > 25) return { label: "Scattered Clouds",code: 802, icon: `03${dayNight}` };
|
||||
if (data.clouds > 10) return { label: "Few Clouds", code: 801, icon: `02${dayNight}` };
|
||||
if(data.clouds > 85) return {label: 'Overcast Clouds', code: 804, icon: `04${dayNight}`};
|
||||
if(data.clouds > 50) return {label: 'Broken Clouds', code: 803, icon: `04${dayNight}`};
|
||||
if(data.clouds > 25) return {label: 'Scattered Clouds', code: 802, icon: `03${dayNight}`};
|
||||
if(data.clouds > 10) return {label: 'Few Clouds', code: 801, icon: `02${dayNight}`};
|
||||
|
||||
return { label: "Clear Sky", code: 800, icon: `01${dayNight}` };
|
||||
return {label: 'Clear Sky', code: 800, icon: `01${dayNight}`};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user