Add Weather widget and integrate OpenWeatherMap API for dynamic updates

This commit is contained in:
2025-12-29 00:57:38 +01:00
parent e77e1d72b8
commit 57f7898d65
4 changed files with 424 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
package openWeatherMap
import (
"ArinDash/config"
"encoding/json"
"io"
"log"
"net/http"
)
const apiBaseURL = "https://api.openweathermap.org/data/2.5/weather"
type configFile struct {
OpenWeatherMap openWeatherMapConfig
}
type openWeatherMapConfig struct {
LocationId string
ApiKey string
}
func FetchCurrentWeather() CurrentWeather {
cfg := &configFile{}
config.LoadConfig(cfg)
client := &http.Client{}
currentWeather := &CurrentWeather{}
req, err := http.NewRequest("GET", apiBaseURL+"?id="+cfg.OpenWeatherMap.LocationId+"&units=metric&lang=en&APPID="+cfg.OpenWeatherMap.ApiKey, nil)
if err != nil {
panic(err)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(respBody, currentWeather)
return *currentWeather
}