Update pika-hyprland-settings/etc/skel/.config/ags/widgets/weather.js

This commit is contained in:
ferreo 2024-08-27 11:47:30 +02:00
parent 34ecee9a2b
commit afda55fa52

View File

@ -1,7 +1,7 @@
const { Widget } = ags; const { Widget } = ags;
const { execAsync } = ags.Utils; const { execAsync } = ags.Utils;
import barConfig from '../barConfig.js'; import barConfig from '../barConfig.js';
import { getWeatherSymbol, getTemp } from '../lib.js'; import { getWeatherSymbol, getTemp, getWeatherDesc } from '../lib.js';
export const Weather = () => Widget.Box({ export const Weather = () => Widget.Box({
halign: 'end', halign: 'end',
@ -23,15 +23,27 @@ export const Weather = () => Widget.Box({
}), }),
], ],
connections: [[barConfig?.weatherUpdateInterval * 1000, async box => { connections: [[barConfig?.weatherUpdateInterval * 1000, async box => {
setTimeout(() => { getWeather(box);
execAsync(`curl https://wttr.in/${barConfig?.city}?format=j1`) setTimeout(() => { getWeather(box) }, 10000);
.then(output => {
const weather = JSON.parse(output);
const weatherCode = weather.current_condition[0].weatherCode;
box.tooltipText = weather.current_condition[0].weatherDesc[0].value;
box.children[0].label = getWeatherSymbol(weatherCode);
box.children[1].label = getTemp(weather.current_condition[0].temp_C);
}).catch(console.error)
}, 5000);
}]], }]],
}); });
function getWeather(box) {
let latLng = [0, 0];
execAsync(`curl https://geocoding-api.open-meteo.com/v1/search?name=${barConfig?.city}&count=1&language=en&format=json`)
.then(output => {
const geocoding = JSON.parse(output);
if (geocoding.results.length === 0) {
return;
}
latLng = [geocoding.results[0].latitude, geocoding.results[0].longitude];
execAsync(`curl https://api.open-meteo.com/v1/forecast?latitude=${latLng[0]}&longitude=${latLng[1]}&current=temperature_2m,is_day,weather_code`)
.then(output => {
const weather = JSON.parse(output);
const weatherCode = weather.current.weather_code;
box.tooltipText = getWeatherDesc(weatherCode);
box.children[0].label = getWeatherSymbol(weatherCode);
box.children[1].label = getTemp(Math.round(weather.current.temperature_2m) + "");
}).catch(console.error)
}).catch(console.error)
}