mcchunkie/plugins/covid.go

84 lines
1.9 KiB
Go
Raw Permalink Normal View History

2020-03-17 18:52:53 -06:00
package plugins
import (
"fmt"
"regexp"
"strings"
"github.com/matrix-org/gomatrix"
)
// Covid responds to covid requests
type Covid struct {
}
// State represents a individual state from the json api
type State struct {
Confirmed int `json:"confirmed"`
Recovered int `json:"recovered"`
Deaths int `json:"deaths"`
}
// Descr describes this plugin
func (h *Covid) Descr() string {
return "Queries [thebigboard.cc](http://www.thebigboard.cc)'s api for information on COVID-19."
}
// Re returns the beer matching string
func (h *Covid) Re() string {
return `(?i)^covid: (.+)$`
}
func (h *Covid) fix(msg string) string {
re := regexp.MustCompile(h.Re())
return re.ReplaceAllString(msg, "$1")
}
// Match determines if we should call the response for Covid
2020-05-13 16:53:31 -06:00
func (h *Covid) Match(_, msg string) bool {
2020-03-17 18:52:53 -06:00
re := regexp.MustCompile(h.Re())
return re.MatchString(msg)
}
// SetStore we don't need a store here.
2020-05-13 16:53:31 -06:00
func (h *Covid) SetStore(_ PluginStore) {}
2020-03-17 18:52:53 -06:00
// Process does the heavy lifting
func (h *Covid) Process(from, msg string) string {
state := h.fix(msg)
2020-03-17 18:52:53 -06:00
if state != "" {
var states = make(map[string]State)
req := HTTPRequest{
Method: "GET",
ResBody: &states,
URL: "http://www.thebigboard.cc/feeds/v1/us.json",
}
_ = req.DoJSON()
// updated and source cause some issues here
//if err != nil {
// SendText(c, ev.RoomID, fmt.Sprintf("Computer says no: %s", err))
//}
var s State
for i, p := range states {
2020-03-17 18:54:22 -06:00
if strings.EqualFold(i, state) {
2020-03-17 18:52:53 -06:00
s = p
state = i
}
}
return fmt.Sprintf("_%s_: confirmed cases: **%d**, recovered: _%d_, deaths: _%d_", state, s.Confirmed, s.Recovered, s.Deaths)
2020-03-17 18:52:53 -06:00
}
return fmt.Sprintf("invalid state: %q", state)
}
// RespondText to looking up of beer requests
func (h *Covid) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
return SendMD(c, ev.RoomID, h.Process(ev.Sender, post))
2020-03-17 18:52:53 -06:00
}
// Name Covid!
func (h *Covid) Name() string {
return "Covid"
}