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

261
widgets/weather.go Normal file
View File

@@ -0,0 +1,261 @@
package widgets
import (
"ArinDash/apis/openweathermap"
"ArinDash/util"
"context"
"fmt"
"time"
"github.com/mum4k/termdash/cell"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/mum4k/termdash/widgetapi"
"github.com/mum4k/termdash/widgets/text"
)
type WeatherOptions struct {
}
func Weather() WeatherOptions {
widgetOptions["WeatherOptions"] = createWeather
return WeatherOptions{}
}
func createWeather(ctx context.Context, _ terminalapi.Terminal, _ interface{}) widgetapi.Widget {
widget := util.PanicOnErrorWithResult(text.New())
go util.Periodic(ctx, 1*time.Hour, func() error {
weather := openWeatherMap.FetchCurrentWeather()
widget.Reset()
weatherIcon := getIcon(weather.Weather[0].Icon)
if err := widget.Write(fmt.Sprintf("\n %s\n\n", weather.Name), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
return err
}
for index, line := range weatherIcon {
if err := widget.Write(fmt.Sprintf("%s ", line), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
return err
}
switch index {
case 0:
if err := printTemperature(weather.Main.Temp, widget); err != nil {
return err
}
if err := widget.Write(fmt.Sprint(" ")); err != nil {
return err
}
if err := widget.Write(weather.Weather[0].Description, text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
return err
}
break
case 1:
if err := widget.Write(fmt.Sprint("Feels like: ")); err != nil {
return err
}
if err := printTemperature(weather.Main.FeelsLike, widget); err != nil {
return err
}
break
case 2:
if err := widget.Write(fmt.Sprintf("\uE34B %s %.2f km/h", weather.CardinalWindDirection(), weather.Wind.Speed*3.6)); err != nil {
return err
}
break
case 3:
if weather.Rain.OneH > 0 {
if err := widget.Write(fmt.Sprintf("Rain: %.2f mm/h ", weather.Rain.OneH)); err != nil {
return err
}
}
if weather.Snow.OneH > 0 {
if err := widget.Write(fmt.Sprintf("Snow: %.2f mm/h ", weather.Snow.OneH)); err != nil {
return err
}
}
if weather.Clouds.All > 0 {
if err := widget.Write(fmt.Sprintf("Clouds: %d %% ", weather.Clouds.All)); err != nil {
return err
}
}
break
case 4:
if err := widget.Write(fmt.Sprintf("Pr: %d hPa | Hum: %d %%", weather.Main.Pressure, weather.Main.Humidity)); err != nil {
return err
}
break
}
if err := widget.Write(fmt.Sprint("\n")); err != nil {
return err
}
}
var riseSet string
if weather.IsDayTime() {
riseSet = `Sunset: ` + time.Unix(weather.Sys.Sunset, 0).Format("15:04")
} else {
riseSet = `Sunrise: ` + time.Unix(weather.Sys.Sunrise, 0).Format("15:04")
}
if err := widget.Write(fmt.Sprintf(" %s\n\n", riseSet), text.WriteCellOpts(cell.FgColor(cell.ColorWhite))); err != nil {
return err
}
return nil
})
return widget
}
func printTemperature(temp float64, widget *text.Text) error {
var color cell.Color
if temp < 0 {
color = cell.ColorBlue
} else if temp < 5 {
color = cell.ColorAqua
} else if temp < 15 {
color = cell.ColorGreen
} else if temp < 25 {
color = cell.ColorYellow
} else {
color = cell.ColorRed
}
if err := widget.Write(fmt.Sprintf("%.2f °C", temp), text.WriteCellOpts(cell.FgColor(color), cell.Bold())); err != nil {
return err
}
return nil
}
// ... existing code ...
func getIcon(name string) []string {
icon := map[string][]string{
"01d": { // wi-day-sunny
" \\ / ",
" .-. ",
" ― ( ) ― ",
" `- ",
" / \\ ",
},
"01n": { // wi-night-clear
" _ ",
" ( `\\ ",
" | | ",
" (_./ ",
" ",
},
"02d": { // wi-day-cloudy
" \\ / ",
" _ /\"\".-. ",
" \\_( ). ",
" /(___(__) ",
" ",
},
"02n": { // wi-night-cloudy
" _ ",
" ( `\\ .-. ",
" | _/( ). ",
" (_/(___(__) ",
" ",
},
"03d": { // wi-cloudy
" ",
" .--. ",
" .-( ). ",
" (___.__)__) ",
" ",
},
"03n": { // wi-night-cloudy (same as 02n/04n usually)
" _ ",
" ( `\\ .-. ",
" | _/( ). ",
" (_/(___(__) ",
" ",
},
"04d": { // wi-cloudy-windy
" .--. ",
" .-( ). __ ",
" (___.__)__) _ ",
" _ - _ - _ - ",
" ",
},
"04n": { // wi-night-cloudy
" _ ",
" ( `\\ .-. ",
" | _/( ). ",
" (_/(___(__) ",
" ",
},
"09d": { // wi-showers
" .-. ",
" ( ). ",
" (___(__) ",
" ",
" ",
},
"09n": { // wi-night-showers
" _ .-. ",
" ( `\\( ). ",
" (_/(___(__) ",
" ",
" ",
},
"10d": { // wi-rain
" .-. ",
" ( ). ",
" (___(__) ",
" ",
" ",
},
"10n": { // wi-night-rain
" _ .-. ",
" ( `\\( ). ",
" (_/(___(__) ",
" ",
" ",
},
"11d": { // wi-thunderstorm
" .-. ",
" ( ). ",
" (___(__) ",
" /_ /_ ",
" / / ",
},
"11n": { // wi-night-thunderstorm
" _ .-. ",
" ( `\\( ). ",
" (_/(___(__) ",
" /_ /_ ",
" / / ",
},
"13d": { // wi-snow
" .-. ",
" ( ). ",
" (___(__) ",
" * * * ",
" * * * ",
},
"13n": { // wi-night-snow
" _ .-. ",
" ( `\\( ). ",
" (_/(___(__) ",
" * * * ",
" * * * ",
},
"50d": { // wi-fog
" ",
" _ - _ - _ - ",
" _ - _ - _ ",
" _ - _ - _ - ",
" ",
},
"50n": { // wi-night-alt-cloudy-windy
" _ ",
" ( `\\ .-. ",
" | _/( ). ",
" (_/(___(__) ",
" _ - _ - _ ",
},
}
return icon[name]
}