1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Fetches, stores, and returns multiweather, with ticks marks in display

This commit is contained in:
Chris Cummer
2018-04-16 03:39:02 -07:00
committed by Chris Cummer
parent 2c4c220ea7
commit 6321182b7e
4 changed files with 91 additions and 29 deletions

View File

@@ -9,9 +9,17 @@ import (
/* -------------------- Exported Functions -------------------- */
func Fetch(cityID int) *owm.CurrentWeatherData {
func Fetch(cityids []int) []*owm.CurrentWeatherData {
apiKey := os.Getenv("WTF_OWM_API_KEY")
data, _ := currentWeather(apiKey, cityID)
data := []*owm.CurrentWeatherData{}
for _, cityID := range cityids {
result, err := currentWeather(apiKey, cityID)
if err == nil {
data = append(data, result)
}
}
return data
}

View File

@@ -14,11 +14,14 @@ var Config *config.Config
type Widget struct {
wtf.TextWidget
Current int
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Weather ", "weather"),
Current: 0,
}
return &widget
@@ -31,52 +34,95 @@ func (widget *Widget) Refresh() {
return
}
data := Fetch(Config.UInt("wtf.mods.weather.cityId", 6176823))
widget.View.SetTitle(fmt.Sprintf(" %s %s ", icon(data), data.Name))
data := Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes())))
widget.View.Clear()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
widget.contentFrom(data)
widget.RefreshedAt = time.Now()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(data *owm.CurrentWeatherData) string {
if len(data.Weather) == 0 {
return " Weather data is unavailable."
func (widget *Widget) contentFrom(data []*owm.CurrentWeatherData) {
cityData := widget.currentCityData(data)
if len(cityData.Weather) == 0 {
fmt.Fprintf(widget.View, "%s", " Weather data is unavailable.")
}
str := "\n"
widget.View.SetTitle(widget.contentTitle(cityData))
str := widget.contentTickMarks(data) + "\n"
str = str + widget.contentDescription(cityData) + "\n\n"
str = str + widget.contentTemperatures(cityData) + "\n"
str = str + widget.contentSunInfo(cityData)
fmt.Fprintf(widget.View, "%s", str)
}
func (widget *Widget) contentTickMarks(data []*owm.CurrentWeatherData) string {
str := ""
if len(data) > 1 {
tickMarks := strings.Repeat("*", len(data))
tickMarks = tickMarks[:widget.Current] + "_" + tickMarks[widget.Current+1:]
str = "[lightblue]" + fmt.Sprintf(wtf.RightAlignFormat(widget.View), tickMarks) + "[white]"
}
return str
}
func (widget *Widget) contentTitle(cityData *owm.CurrentWeatherData) string {
return fmt.Sprintf(" %s %s ", widget.icon(cityData), cityData.Name)
}
func (widget *Widget) contentDescription(cityData *owm.CurrentWeatherData) string {
descs := []string{}
for _, weather := range data.Weather {
for _, weather := range cityData.Weather {
descs = append(descs, fmt.Sprintf(" %s", weather.Description))
}
str = str + strings.Join(descs, ",") + "\n\n"
return strings.Join(descs, ",")
}
func (widget *Widget) contentTemperatures(cityData *owm.CurrentWeatherData) string {
tempUnit := Config.UString("wtf.mods.weather.tempUnit", "C")
str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "High", data.Main.TempMax, tempUnit)
str = str + fmt.Sprintf("%8s: [green]%4.1f° %s[white]\n", "Current", data.Main.Temp, tempUnit)
str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "Low", data.Main.TempMin, tempUnit)
str = str + "\n"
str = str + fmt.Sprintf(
" Sunrise: %s Sunset: %s\n",
wtf.UnixTime(int64(data.Sys.Sunrise)).Format("15:04"),
wtf.UnixTime(int64(data.Sys.Sunset)).Format("15:04"),
)
str := fmt.Sprintf("%8s: %4.1f° %s\n", "High", cityData.Main.TempMax, tempUnit)
str = str + fmt.Sprintf("%8s: [green]%4.1f° %s[white]\n", "Current", cityData.Main.Temp, tempUnit)
str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "Low", cityData.Main.TempMin, tempUnit)
return str
}
func (widget *Widget) contentSunInfo(cityData *owm.CurrentWeatherData) string {
return fmt.Sprintf(
" Rise: %s Set: %s\n",
wtf.UnixTime(int64(cityData.Sys.Sunrise)).Format("15:04 MST"),
wtf.UnixTime(int64(cityData.Sys.Sunset)).Format("15:04 MST"),
)
}
func (widget *Widget) currentCityData(data []*owm.CurrentWeatherData) *owm.CurrentWeatherData {
return data[widget.Current]
}
func (widget *Widget) defaultCityCodes() []interface{} {
defaultArr := []int{6176823, 360630, 3413829}
var defaults []interface{} = make([]interface{}, len(defaultArr))
for i, d := range defaultArr {
defaults[i] = d
}
return defaults
}
// icon returns an emoji for the current weather
// src: https://github.com/chubin/wttr.in/blob/master/share/translations/en.txt
// Note: these only work for English weather status. Sorry about that
func icon(data *owm.CurrentWeatherData) string {
func (widget *Widget) icon(data *owm.CurrentWeatherData) string {
var icon string
if len(data.Weather) == 0 {