Skip to content

Commit

Permalink
Democracy 2024
Browse files Browse the repository at this point in the history
  • Loading branch information
rcy committed Nov 5, 2024
1 parent 3a40f13 commit 6efb989
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
80 changes: 80 additions & 0 deletions handlers/usvote2024/usvote2024.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6efb989

Please sign in to comment.