From afda55fa5296059178b02acb718d90187b65ef3b Mon Sep 17 00:00:00 2001 From: ferreo Date: Tue, 27 Aug 2024 11:47:30 +0200 Subject: [PATCH] Update pika-hyprland-settings/etc/skel/.config/ags/widgets/weather.js --- .../etc/skel/.config/ags/widgets/weather.js | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/pika-hyprland-settings/etc/skel/.config/ags/widgets/weather.js b/pika-hyprland-settings/etc/skel/.config/ags/widgets/weather.js index ec1a608..b5da8e9 100644 --- a/pika-hyprland-settings/etc/skel/.config/ags/widgets/weather.js +++ b/pika-hyprland-settings/etc/skel/.config/ags/widgets/weather.js @@ -1,7 +1,7 @@ const { Widget } = ags; const { execAsync } = ags.Utils; import barConfig from '../barConfig.js'; -import { getWeatherSymbol, getTemp } from '../lib.js'; +import { getWeatherSymbol, getTemp, getWeatherDesc } from '../lib.js'; export const Weather = () => Widget.Box({ halign: 'end', @@ -23,15 +23,27 @@ export const Weather = () => Widget.Box({ }), ], connections: [[barConfig?.weatherUpdateInterval * 1000, async box => { - setTimeout(() => { - execAsync(`curl https://wttr.in/${barConfig?.city}?format=j1`) - .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); + getWeather(box); + setTimeout(() => { getWeather(box) }, 10000); }]], }); + +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]}¤t=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) +} \ No newline at end of file