Created
October 2, 2022 22:15
-
-
Save eminetto/ddea05c83fb4fc9a4899b7c735bd4ae2 to your computer and use it in GitHub Desktop.
eleicao_color.go
This file contains hidden or 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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"github.com/charmbracelet/lipgloss" | |
) | |
type resultado struct { | |
Dt string `json:"dt"` | |
HT string `json:"ht"` | |
Pst string `json:"pst"` | |
Cand []map[string]string `json:"cand"` | |
} | |
func main() { | |
for range time.Tick(time.Second * 30) { | |
fmt.Printf("\x1bc") | |
getResult() | |
} | |
} | |
func getResult() { | |
resp, err := http.Get("https://resultados.tse.jus.br/oficial/ele2022/544/dados-simplificados/br/br-c0001-e000544-r.json") | |
if err != nil { | |
log.Fatalln(err) | |
} | |
var result resultado | |
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { | |
log.Fatal("ooopsss! an error occurred, please try again") | |
} | |
fmt.Printf("%s das seções totalizadas %s\n", result.Pst, result.HT) | |
for _, r := range result.Cand { | |
s := fmt.Sprintf("Candidato: %s Votos: %s Percentual: %s \n", r["nm"], r["vap"], r["pvap"]) | |
switch r["nm"] { | |
case "JAIR BOLSONARO": | |
fmt.Println(lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#80FF00")).Background(lipgloss.Color("#000000")).BorderStyle(lipgloss.NormalBorder()).Render(s)) | |
case "LULA": | |
fmt.Println(lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#FF0000")).Background(lipgloss.Color("#000000")).BorderStyle(lipgloss.NormalBorder()).Render(s)) | |
default: | |
fmt.Println(lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("#FFFFFF")).Background(lipgloss.Color("#000000")).BorderStyle(lipgloss.NormalBorder()).Render(s)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment