diff --git a/.gitignore b/.gitignore index 2b16d5cc..ffe95414 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ # Misc .DS_Store gcal/client_secret.json +#intellij idea +.idea/ diff --git a/prettyweather/widget.go b/prettyweather/widget.go new file mode 100644 index 00000000..0dcea050 --- /dev/null +++ b/prettyweather/widget.go @@ -0,0 +1,66 @@ +package prettyweather + +import ( + "fmt" + "github.com/olebedev/config" + "github.com/senorprogrammer/wtf/wtf" + "io/ioutil" + "net/http" + "strings" +) + +// Config is a pointer to the global config object +var Config *config.Config + +type Widget struct { + wtf.TextWidget + result string + unit string + city string +} + +func NewWidget() *Widget { + widget := Widget{ + TextWidget: wtf.NewTextWidget("Pretty Weather", "prettyweather", false), + } + + return &widget +} + +func (widget *Widget) Refresh() { + if widget.Disabled() { + return + } + + widget.UpdateRefreshedAt() + widget.prettyWeather() + widget.View.Clear() + widget.View.SetTitle(fmt.Sprintf(" %s ", widget.Name)) + + fmt.Fprintf(widget.View, "%s", widget.result) +} + +//this method reads the config and calls wttr.in for pretty weather +func (widget *Widget) prettyWeather() { + client := &http.Client{} + widget.unit, widget.city = Config.UString("wtf.mods.prettyweather.unit", "m"), Config.UString("wtf.mods.prettyweather.city", "") + req, err := http.NewRequest("GET", "https://wttr.in/"+widget.city+"?0"+"?"+widget.unit, nil) + if err != nil { + widget.result = fmt.Sprintf("%s", err.Error()) + return + } + req.Header.Set("User-Agent", "curl") + response, err := client.Do(req) + if err != nil { + widget.result = fmt.Sprintf("%s", err.Error()) + return + } + defer response.Body.Close() + contents, err := ioutil.ReadAll(response.Body) + if err != nil { + widget.result = fmt.Sprintf("%s", err.Error()) + return + } + widget.result = fmt.Sprintf("%s", strings.TrimSpace(string(contents))) + +} diff --git a/wtf.go b/wtf.go index 948870be..43e0a55a 100644 --- a/wtf.go +++ b/wtf.go @@ -17,10 +17,12 @@ import ( "github.com/senorprogrammer/wtf/git" "github.com/senorprogrammer/wtf/github" "github.com/senorprogrammer/wtf/help" + "github.com/senorprogrammer/wtf/ipinfo" "github.com/senorprogrammer/wtf/jira" "github.com/senorprogrammer/wtf/newrelic" "github.com/senorprogrammer/wtf/opsgenie" "github.com/senorprogrammer/wtf/power" + "github.com/senorprogrammer/wtf/prettyweather" "github.com/senorprogrammer/wtf/security" "github.com/senorprogrammer/wtf/status" "github.com/senorprogrammer/wtf/system" @@ -28,7 +30,6 @@ import ( "github.com/senorprogrammer/wtf/todo" "github.com/senorprogrammer/wtf/weather" "github.com/senorprogrammer/wtf/wtf" - "github.com/senorprogrammer/wtf/ipinfo" ) /* -------------------- Functions -------------------- */ @@ -170,6 +171,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) { textfile.Config = Config todo.Config = Config weather.Config = Config + prettyweather.Config = Config wtf.Config = Config Widgets = []wtf.Wtfable{ @@ -190,6 +192,7 @@ func makeWidgets(app *tview.Application, pages *tview.Pages) { textfile.NewWidget(app, pages), todo.NewWidget(app, pages), weather.NewWidget(app, pages), + prettyweather.NewWidget(), } FocusTracker = wtf.FocusTracker{