i3-blocks-go/blocks/weather/main.go
Simon Vieille 039bb2c2b1
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
add block weather
2022-09-02 16:31:25 +02:00

57 lines
988 B
Go

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