diff --git a/handlers.go b/handlers.go index 635f9c1..40f5646 100644 --- a/handlers.go +++ b/handlers.go @@ -17,6 +17,7 @@ import ( "goirc/handlers/kinfonet" "goirc/handlers/linkpool" "goirc/handlers/mlb" + "goirc/handlers/usvote2024" "goirc/handlers/weather" db "goirc/model" "goirc/web" @@ -68,6 +69,8 @@ func addHandlers(b *bot.Bot) { b.Handle(`^!election`, election.Handle) b.Handle(`^annie:?(.+)$`, annie.Handle) b.Handle(`^(.+),? annie.?$`, annie.Handle) + b.Handle(`^!trump`, usvote2024.HandleTrump) + b.Handle(`^!harris`, usvote2024.HandleHarris) b.Repeat(10*time.Second, handlers.DoRemind) b.IdleRepeatAfterReset(8*time.Hour, handlers.POM) diff --git a/handlers/usvote2024/usvote2024.go b/handlers/usvote2024/usvote2024.go new file mode 100644 index 0000000..8d95916 --- /dev/null +++ b/handlers/usvote2024/usvote2024.go @@ -0,0 +1,80 @@ +package usvote2024 + +import ( + "fmt" + "goirc/bot" + "strings" + + "github.com/gocolly/colly" +) + +type party struct { + Name string + Tally string + Votes string +} + +func (p party) String() string { + return fmt.Sprintf("%s %s %s", p.Name, p.Tally, p.Votes) +} + +type results struct { + Rep party + Dem party +} + +func count() (*results, error) { + base := "https://www.reuters.com/graphics/USA-ELECTION/RESULTS/zjpqnemxwvx/" + c := colly.NewCollector() + + results := results{} + + c.OnHTML("div.div-rep", func(e *colly.HTMLElement) { + results.Rep.Tally = e.ChildText("div.tally-numbers") + results.Rep.Name = e.ChildText("div.tally-party") + }) + c.OnHTML("p.div-rep", func(e *colly.HTMLElement) { + results.Rep.Votes = strings.TrimSuffix(strings.TrimSpace(e.Text), " votes") + }) + + c.OnHTML("div.div-dem", func(e *colly.HTMLElement) { + results.Dem.Tally = e.ChildText("div.tally-numbers") + results.Dem.Name = e.ChildText("div.tally-party") + }) + c.OnHTML("p.div-dem", func(e *colly.HTMLElement) { + results.Dem.Votes = strings.TrimSuffix(strings.TrimSpace(e.Text), " votes") + }) + + err := c.Visit(base) + if err != nil { + return nil, err + } + + return &results, nil +} + +func HandleTrump(params bot.HandlerParams) error { + results, err := count() + if err != nil { + return err + } + + params.Privmsgf(params.Target, "%s (%s) %s | %s (%s) %s", + results.Rep.Name, results.Rep.Votes, results.Rep.Tally, results.Dem.Tally, results.Dem.Votes, results.Dem.Name, + ) + + return nil +} + +func HandleHarris(params bot.HandlerParams) error { + results, err := count() + if err != nil { + return err + } + + params.Privmsgf(params.Target, "%s (%s) %s | %s (%s) %s", + results.Dem.Name, results.Dem.Votes, results.Dem.Tally, results.Rep.Tally, results.Rep.Votes, results.Rep.Name, + ) + + return nil +}