Add Weather widget and integrate OpenWeatherMap API for dynamic updates
This commit is contained in:
111
apis/openweathermap/currentWeather.go
Normal file
111
apis/openweathermap/currentWeather.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package openWeatherMap
|
||||
|
||||
import "time"
|
||||
|
||||
type CurrentWeather struct {
|
||||
Base string `json:"base"`
|
||||
Visibility int `json:"visibility"`
|
||||
Dt int `json:"dt"`
|
||||
Timezone int `json:"timezone"`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Cod int `json:"cod"`
|
||||
Coordinates Coordinates `json:"coord"`
|
||||
Weather []Weather `json:"weather"`
|
||||
Wind Wind `json:"wind"`
|
||||
Clouds Clouds `json:"clouds"`
|
||||
Rain Rain `json:"rain"`
|
||||
Snow Snow `json:"snow"`
|
||||
Sys Sys `json:"sys"`
|
||||
Main Main `json:"main"`
|
||||
}
|
||||
|
||||
type Coordinates struct {
|
||||
Lon float64 `json:"lon"`
|
||||
Lat float64 `json:"lat"`
|
||||
}
|
||||
|
||||
type Weather struct {
|
||||
Id int `json:"id"`
|
||||
Main string `json:"main"`
|
||||
Description string `json:"description"`
|
||||
Icon string `json:"icon"`
|
||||
}
|
||||
|
||||
type Wind struct {
|
||||
Speed float64 `json:"speed"`
|
||||
Deg float64 `json:"deg"`
|
||||
}
|
||||
|
||||
type Clouds struct {
|
||||
All int `json:"all"`
|
||||
}
|
||||
|
||||
type Rain struct {
|
||||
OneH float64 `json:"1h"`
|
||||
}
|
||||
|
||||
type Snow struct {
|
||||
OneH float64 `json:"1h"`
|
||||
}
|
||||
|
||||
type Sys struct {
|
||||
Type int `json:"type"`
|
||||
Id int `json:"id"`
|
||||
Country string `json:"country"`
|
||||
Sunrise int64 `json:"sunrise"`
|
||||
Sunset int64 `json:"sunset"`
|
||||
}
|
||||
|
||||
type Main struct {
|
||||
Temp float64 `json:"temp"`
|
||||
FeelsLike float64 `json:"feels_like"`
|
||||
TempMin float64 `json:"temp_min"`
|
||||
TempMax float64 `json:"temp_max"`
|
||||
Pressure int `json:"pressure"`
|
||||
Humidity int `json:"humidity"`
|
||||
SeaLevel int `json:"sea_level"`
|
||||
GrndLevel int `json:"grnd_level"`
|
||||
}
|
||||
|
||||
func (currentWeather *CurrentWeather) CardinalWindDirection() string {
|
||||
windDeg := currentWeather.Wind.Deg
|
||||
if windDeg > 11.25 && windDeg <= 33.75 {
|
||||
return `NNE`
|
||||
} else if windDeg > 33.75 && windDeg <= 56.25 {
|
||||
return "NE"
|
||||
} else if windDeg > 56.25 && windDeg <= 78.75 {
|
||||
return "ENE"
|
||||
} else if windDeg > 78.75 && windDeg <= 101.25 {
|
||||
return "E"
|
||||
} else if windDeg > 101.25 && windDeg <= 123.75 {
|
||||
return "ESE"
|
||||
} else if windDeg > 123.75 && windDeg <= 146.25 {
|
||||
return "SE"
|
||||
} else if windDeg > 146.25 && windDeg <= 168.75 {
|
||||
return "SSE"
|
||||
} else if windDeg > 168.75 && windDeg <= 191.25 {
|
||||
return "S"
|
||||
} else if windDeg > 191.25 && windDeg <= 213.75 {
|
||||
return "SSW"
|
||||
} else if windDeg > 213.75 && windDeg <= 236.25 {
|
||||
return "SW"
|
||||
} else if windDeg > 236.25 && windDeg <= 258.75 {
|
||||
return "WSW"
|
||||
} else if windDeg > 258.75 && windDeg <= 281.25 {
|
||||
return "W"
|
||||
} else if windDeg > 281.25 && windDeg <= 303.75 {
|
||||
return "WNW"
|
||||
} else if windDeg > 303.75 && windDeg <= 326.25 {
|
||||
return "NW"
|
||||
} else if windDeg > 326.25 && windDeg <= 348.75 {
|
||||
return "NNW"
|
||||
}
|
||||
|
||||
return `N`
|
||||
}
|
||||
|
||||
func (currentWeather *CurrentWeather) IsDayTime() bool {
|
||||
now := time.Now()
|
||||
return now.After(time.Unix(currentWeather.Sys.Sunrise, 0)) && now.Before(time.Unix(currentWeather.Sys.Sunset, 0))
|
||||
}
|
||||
44
apis/openweathermap/main.go
Normal file
44
apis/openweathermap/main.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user