mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-400 Weather extracted to new config format
This commit is contained in:
parent
d0faa3cb40
commit
cfdfb044da
3
main.go
3
main.go
@ -310,7 +310,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
|
|||||||
settings := victorops.NewSettingsFromYAML(wtf.Config)
|
settings := victorops.NewSettingsFromYAML(wtf.Config)
|
||||||
widget = victorops.NewWidget(app, settings)
|
widget = victorops.NewWidget(app, settings)
|
||||||
case "weather":
|
case "weather":
|
||||||
widget = weather.NewWidget(app, pages)
|
settings := weather.NewSettingsFromYAML(wtf.Config)
|
||||||
|
widget = weather.NewWidget(app, pages, settings)
|
||||||
case "zendesk":
|
case "zendesk":
|
||||||
widget = zendesk.NewWidget(app)
|
widget = zendesk.NewWidget(app)
|
||||||
default:
|
default:
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (widget *Widget) display() {
|
func (widget *Widget) display() {
|
||||||
|
|
||||||
if widget.apiKeyValid() == false {
|
if widget.apiKeyValid() == false {
|
||||||
widget.View.SetText(" Environment variable WTF_OWM_API_KEY is not set")
|
widget.View.SetText(" Environment variable WTF_OWM_API_KEY is not set")
|
||||||
return
|
return
|
||||||
@ -54,19 +53,17 @@ func (widget *Widget) sunInfo(cityData *owm.CurrentWeatherData) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) temperatures(cityData *owm.CurrentWeatherData) string {
|
func (widget *Widget) temperatures(cityData *owm.CurrentWeatherData) string {
|
||||||
tempUnit := wtf.Config.UString("wtf.mods.weather.tempUnit", "C")
|
str := fmt.Sprintf("%8s: %4.1f° %s\n", "High", cityData.Main.TempMax, widget.settings.tempUnit)
|
||||||
|
|
||||||
str := fmt.Sprintf("%8s: %4.1f° %s\n", "High", cityData.Main.TempMax, tempUnit)
|
|
||||||
|
|
||||||
str = str + fmt.Sprintf(
|
str = str + fmt.Sprintf(
|
||||||
"%8s: [%s]%4.1f° %s[white]\n",
|
"%8s: [%s]%4.1f° %s[white]\n",
|
||||||
"Current",
|
"Current",
|
||||||
wtf.Config.UString("wtf.mods.weather.colors.current", "green"),
|
widget.settings.colors.current,
|
||||||
cityData.Main.Temp,
|
cityData.Main.Temp,
|
||||||
tempUnit,
|
widget.settings.tempUnit,
|
||||||
)
|
)
|
||||||
|
|
||||||
str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "Low", cityData.Main.TempMin, tempUnit)
|
str = str + fmt.Sprintf("%8s: %4.1f° %s\n", "Low", cityData.Main.TempMin, widget.settings.tempUnit)
|
||||||
|
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
39
modules/weatherservices/weather/settings.go
Normal file
39
modules/weatherservices/weather/settings.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package weather
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/olebedev/config"
|
||||||
|
"github.com/wtfutil/wtf/cfg"
|
||||||
|
)
|
||||||
|
|
||||||
|
type colors struct {
|
||||||
|
current string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Settings struct {
|
||||||
|
colors
|
||||||
|
common *cfg.Common
|
||||||
|
|
||||||
|
apiKey string
|
||||||
|
cityIDs []interface{}
|
||||||
|
language string
|
||||||
|
tempUnit string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
|
||||||
|
localConfig, _ := ymlConfig.Get("wtf.mods.weather")
|
||||||
|
|
||||||
|
settings := Settings{
|
||||||
|
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
|
||||||
|
|
||||||
|
apiKey: localConfig.UString("apiKey", os.Getenv("WTF_OWM_API_KEY")),
|
||||||
|
cityIDs: localConfig.UList("cityids"),
|
||||||
|
language: localConfig.UString("language", "EN"),
|
||||||
|
tempUnit: localConfig.UString("tempUnit", "C"),
|
||||||
|
}
|
||||||
|
|
||||||
|
settings.colors.current = localConfig.UString("colors.current", "green")
|
||||||
|
|
||||||
|
return &settings
|
||||||
|
}
|
@ -1,8 +1,6 @@
|
|||||||
package weather
|
package weather
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
|
|
||||||
owm "github.com/briandowns/openweathermap"
|
owm "github.com/briandowns/openweathermap"
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
@ -25,22 +23,24 @@ type Widget struct {
|
|||||||
wtf.HelpfulWidget
|
wtf.HelpfulWidget
|
||||||
wtf.TextWidget
|
wtf.TextWidget
|
||||||
|
|
||||||
APIKey string
|
// APIKey string
|
||||||
Data []*owm.CurrentWeatherData
|
Data []*owm.CurrentWeatherData
|
||||||
Idx int
|
Idx int
|
||||||
|
settings *Settings
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWidget creates and returns a new instance of the weather Widget.
|
// NewWidget creates and returns a new instance of the weather Widget.
|
||||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||||
configKey := "weather"
|
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||||
TextWidget: wtf.NewTextWidget(app, "Weather", configKey, true),
|
TextWidget: wtf.NewTextWidget(app, "Weather", "weather", true),
|
||||||
|
|
||||||
Idx: 0,
|
Idx: 0,
|
||||||
|
settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.loadAPICredentials()
|
// widget.loadAPICredentials()
|
||||||
|
// widget.APIKey
|
||||||
|
|
||||||
widget.HelpfulWidget.SetView(widget.View)
|
widget.HelpfulWidget.SetView(widget.View)
|
||||||
widget.View.SetInputCapture(widget.keyboardIntercept)
|
widget.View.SetInputCapture(widget.keyboardIntercept)
|
||||||
@ -57,7 +57,7 @@ func (widget *Widget) Fetch(cityIDs []int) []*owm.CurrentWeatherData {
|
|||||||
data := []*owm.CurrentWeatherData{}
|
data := []*owm.CurrentWeatherData{}
|
||||||
|
|
||||||
for _, cityID := range cityIDs {
|
for _, cityID := range cityIDs {
|
||||||
result, err := widget.currentWeather(widget.APIKey, cityID)
|
result, err := widget.currentWeather(cityID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
data = append(data, result)
|
data = append(data, result)
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ func (widget *Widget) Fetch(cityIDs []int) []*owm.CurrentWeatherData {
|
|||||||
// widget's view for rendering
|
// widget's view for rendering
|
||||||
func (widget *Widget) Refresh() {
|
func (widget *Widget) Refresh() {
|
||||||
if widget.apiKeyValid() {
|
if widget.apiKeyValid() {
|
||||||
widget.Data = widget.Fetch(wtf.ToInts(wtf.Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes())))
|
widget.Data = widget.Fetch(wtf.ToInts(widget.settings.cityIDs))
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.display()
|
widget.display()
|
||||||
@ -101,11 +101,11 @@ func (widget *Widget) Prev() {
|
|||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *Widget) apiKeyValid() bool {
|
func (widget *Widget) apiKeyValid() bool {
|
||||||
if widget.APIKey == "" {
|
if widget.settings.apiKey == "" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(widget.APIKey) != 32 {
|
if len(widget.settings.apiKey) != 32 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,11 +124,11 @@ func (widget *Widget) currentData() *owm.CurrentWeatherData {
|
|||||||
return widget.Data[widget.Idx]
|
return widget.Data[widget.Idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) currentWeather(apiKey string, cityCode int) (*owm.CurrentWeatherData, error) {
|
func (widget *Widget) currentWeather(cityCode int) (*owm.CurrentWeatherData, error) {
|
||||||
weather, err := owm.NewCurrent(
|
weather, err := owm.NewCurrent(
|
||||||
wtf.Config.UString("wtf.mods.weather.tempUnit", "C"),
|
widget.settings.tempUnit,
|
||||||
wtf.Config.UString("wtf.mods.weather.language", "EN"),
|
widget.settings.language,
|
||||||
apiKey,
|
widget.settings.apiKey,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -142,17 +142,6 @@ func (widget *Widget) currentWeather(apiKey string, cityCode int) (*owm.CurrentW
|
|||||||
return weather, nil
|
return weather, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) defaultCityCodes() []interface{} {
|
|
||||||
defaultArr := []int{3370352}
|
|
||||||
|
|
||||||
var defaults = make([]interface{}, len(defaultArr))
|
|
||||||
for i, d := range defaultArr {
|
|
||||||
defaults[i] = d
|
|
||||||
}
|
|
||||||
|
|
||||||
return defaults
|
|
||||||
}
|
|
||||||
|
|
||||||
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||||
switch string(event.Rune()) {
|
switch string(event.Rune()) {
|
||||||
case "/":
|
case "/":
|
||||||
@ -177,12 +166,3 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadAPICredentials loads the API authentication credentials for this module
|
|
||||||
// First checks to see if they're in the config file. If not, checks the ENV var
|
|
||||||
func (widget *Widget) loadAPICredentials() {
|
|
||||||
widget.APIKey = wtf.Config.UString(
|
|
||||||
"wtf.mods.weather.apiKey",
|
|
||||||
os.Getenv("WTF_OWM_API_KEY"),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user