Skip to content

Commit

Permalink
Added PrettyWeather mod
Browse files Browse the repository at this point in the history
  • Loading branch information
FarhadF authored and senorprogrammer committed Jun 2, 2018
1 parent 5d30e09 commit 21ee436
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
gcal/client_secret.json
#intellij idea
.idea/
66 changes: 66 additions & 0 deletions prettyweather/widget.go
Original file line number Diff line number Diff line change
@@ -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),
}

widget.View.SetWrap(true)

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 {
fmt.Println(err)
}

req.Header.Set("User-Agent", "curl")
response, err := client.Do(req)
if err != nil {
fmt.Printf("%s", err)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
}
widget.result = fmt.Sprintf("%s", strings.TrimSpace(string(contents)))
}
}
5 changes: 4 additions & 1 deletion wtf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ 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"
"github.com/senorprogrammer/wtf/textfile"
"github.com/senorprogrammer/wtf/todo"
"github.com/senorprogrammer/wtf/weather"
"github.com/senorprogrammer/wtf/wtf"
"github.com/senorprogrammer/wtf/ipinfo"
)

/* -------------------- Functions -------------------- */
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down

0 comments on commit 21ee436

Please sign in to comment.