add block weather
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Simon Vieille 2022-09-02 16:31:25 +02:00
parent 251a5a11e3
commit 039bb2c2b1
Signed by: deblan
GPG key ID: 579388D585F70417
2 changed files with 69 additions and 1 deletions

View file

@ -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.

56
blocks/weather/main.go Normal file
View file

@ -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()
}