From 039bb2c2b1c8e6ad712cb95ca86923a6e6fdccd6 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 2 Sep 2022 16:31:25 +0200 Subject: [PATCH] add block weather --- README.md | 14 ++++++++++- blocks/weather/main.go | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 blocks/weather/main.go diff --git a/README.md b/README.md index ef1e85e..228cd20 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ interval=1000 ### Date -Show the time using given format. +Show the time using the given format. ``` [time] @@ -83,6 +83,18 @@ markup=pango interval=30 ``` +### Weather + +Show the weather of the given location ([https://fr.wttr.in/](https://fr.wttr.in/)). + +``` +[weather] +command=/path/to/weather -loc=Paris +format=json +markup=pango +interval=1800 +``` + ### IP Show the IP of the given iface. diff --git a/blocks/weather/main.go b/blocks/weather/main.go new file mode 100644 index 0000000..1f30dc8 --- /dev/null +++ b/blocks/weather/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "flag" + "fmt" + r "gitnet.fr/deblan/i3-blocks-go/rendering" + "io" + "net/http" + "os" + "os/exec" + "strings" + "sync" +) + +func openBrowser(wg *sync.WaitGroup, url string) { + defer wg.Done() + command := exec.Command("tmux", "new", "-d", "xdg-open", url) + command.Run() +} + +func main() { + var wg sync.WaitGroup + + argLocation := flag.String("loc", "", "location") + flag.Parse() + + url := fmt.Sprintf("https://fr.wttr.in/%s?format=%%l+%%c+%%t+%%m", *argLocation) + + if os.Getenv("BLOCK_BUTTON") == "1" { + wg.Add(1) + go openBrowser(&wg, url) + } + + resp, err := http.Get(url) + + if err != nil { + return + } + + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + content := string(body) + + fb := r.FB{ + Foreground: r.Color("white1"), + Background: r.Color("black2"), + } + + content = strings.ReplaceAll(content, " ", " ") + options := r.NewBlockOptions() + options.FullText = r.TextWithPadding(content, fb) + block := r.Block("meteo", options) + + fmt.Println(block) + wg.Wait() +}