From 8079e5c610cd9367a3bcfb66886ca94d988c9823 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Tue, 17 Mar 2020 18:52:53 -0600 Subject: [PATCH] +covid info --- plugins/covid.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/plugins.go | 1 + 2 files changed, 77 insertions(+) create mode 100644 plugins/covid.go diff --git a/plugins/covid.go b/plugins/covid.go new file mode 100644 index 0000000..cebb5d2 --- /dev/null +++ b/plugins/covid.go @@ -0,0 +1,76 @@ +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 +func (h *Covid) Match(user, msg string) bool { + re := regexp.MustCompile(h.Re()) + return re.MatchString(msg) +} + +// SetStore we don't need a store here. +func (h *Covid) SetStore(s PluginStore) {} + +// RespondText to looking up of beer requests +func (h *Covid) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + state := h.fix(post) + 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 { + if strings.ToLower(i) == strings.ToLower(state) { + s = p + state = i + } + } + SendMD(c, ev.RoomID, fmt.Sprintf("_%s_: confirmed cases: **%d**, recovered: _%d_, deaths: _%d_", state, s.Confirmed, s.Recovered, s.Deaths)) + } +} + +// Name Covid! +func (h *Covid) Name() string { + return "Covid" +} diff --git a/plugins/plugins.go b/plugins/plugins.go index 941a6aa..cbfd580 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -182,6 +182,7 @@ var Plugs = Plugins{ &Beat{}, &Beer{}, &BotSnack{}, + &Covid{}, &Feder{}, &Ham{}, &HighFive{},