-
-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5d30e09
commit 21ee436
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.DS_Store | ||
gcal/client_secret.json | ||
#intellij idea | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters