i3-blocks-go/blocks/rss/main.go

72 lines
1.5 KiB
Go
Raw Normal View History

2022-08-28 22:21:31 +02:00
package main
import (
2022-08-30 15:42:28 +02:00
"flag"
2022-08-28 22:21:31 +02:00
"fmt"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"io"
"net/http"
"os"
"os/exec"
"strings"
"sync"
)
type Telemetry struct {
Progress int `json:"progress"`
TempNozzle int `json:"temp_nozzle"`
TempBed int `json:"temp_bed"`
TimeEst string `json:"time_est"`
}
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
2022-08-30 15:42:28 +02:00
argBlock := flag.String("block", "rss", "block name")
argFeed := flag.String("feed", "", "url of the feed")
argWebsite := flag.String("website", "", "website")
argEmptyColor := flag.String("empty-color", "#CCCCCC", "empty color")
argNonEmptyColor := flag.String("non-empty-color", "#B3FF6C", "when items color")
flag.Parse()
2022-08-28 22:21:31 +02:00
if os.Getenv("BLOCK_BUTTON") == "1" {
wg.Add(1)
2022-08-30 15:42:28 +02:00
go openBrowser(&wg, *argWebsite)
2022-08-28 22:21:31 +02:00
}
2022-08-30 15:42:28 +02:00
resp, err := http.Get(*argFeed)
2022-08-28 22:21:31 +02:00
if err != nil {
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
content := string(body)
count := strings.Count(content, "<item") + strings.Count(content, "<entry")
label := r.FontAwesome("\uf09e")
2022-08-28 22:21:31 +02:00
fb := r.FB{
2022-08-30 15:42:28 +02:00
Foreground: *argEmptyColor,
2022-08-28 22:21:31 +02:00
Background: r.Color("black2"),
}
if count > 0 {
label = fmt.Sprintf("%s +%d", label, count)
2022-08-30 15:42:28 +02:00
fb.Foreground = *argNonEmptyColor
2022-08-28 22:21:31 +02:00
}
options := r.NewBlockOptions()
options.FullText = r.TextWithPadding(label, fb)
2022-08-30 15:42:28 +02:00
block := r.Block(*argBlock, options)
2022-08-28 22:21:31 +02:00
fmt.Println(block)
wg.Wait()
}