2020-02-01 15:14:46 -07:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-02-01 20:45:42 -07:00
|
|
|
"math/rand"
|
2020-02-01 15:14:46 -07:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
2020-02-01 20:45:42 -07:00
|
|
|
"time"
|
2020-02-01 15:14:46 -07:00
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrix"
|
|
|
|
)
|
|
|
|
|
2020-02-01 20:47:14 -07:00
|
|
|
// Beer responds to beer requests
|
2020-02-01 15:14:46 -07:00
|
|
|
type Beer struct {
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:45:42 -07:00
|
|
|
// BeerResp represents our response from the api
|
|
|
|
type BeerResp struct {
|
|
|
|
Nhits int `json:"nhits"`
|
|
|
|
Parameters Parameters `json:"parameters"`
|
|
|
|
Records []Records `json:"records"`
|
2020-02-01 15:31:56 -07:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:45:42 -07:00
|
|
|
// Parameters are the meta information
|
|
|
|
type Parameters struct {
|
|
|
|
Dataset []string `json:"dataset"`
|
|
|
|
Timezone string `json:"timezone"`
|
|
|
|
Q string `json:"q"`
|
|
|
|
Rows int `json:"rows"`
|
|
|
|
Format string `json:"format"`
|
2020-02-01 15:31:56 -07:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:45:42 -07:00
|
|
|
// Fields are the bits of info we care about
|
|
|
|
type Fields struct {
|
|
|
|
Website string `json:"website"`
|
|
|
|
City string `json:"city"`
|
|
|
|
StyleID string `json:"style_id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Country string `json:"country"`
|
|
|
|
CatID string `json:"cat_id"`
|
|
|
|
BreweryID string `json:"brewery_id"`
|
|
|
|
Descript string `json:"descript"`
|
|
|
|
Upc int `json:"upc"`
|
|
|
|
Coordinates []float64 `json:"coordinates"`
|
|
|
|
Ibu int `json:"ibu"`
|
|
|
|
CatName string `json:"cat_name"`
|
|
|
|
LastMod time.Time `json:"last_mod"`
|
|
|
|
State string `json:"state"`
|
|
|
|
StyleName string `json:"style_name"`
|
|
|
|
Abv float64 `json:"abv"`
|
|
|
|
Address1 string `json:"address1"`
|
|
|
|
NameBreweries string `json:"name_breweries"`
|
|
|
|
Srm int `json:"srm"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
AddUser string `json:"add_user"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Geometry is basically useless
|
|
|
|
type Geometry struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Coordinates []float64 `json:"coordinates"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Records holds our fileds
|
|
|
|
type Records struct {
|
|
|
|
Datasetid string `json:"datasetid"`
|
|
|
|
Recordid string `json:"recordid"`
|
|
|
|
Fields Fields `json:"fields"`
|
|
|
|
Geometry Geometry `json:"geometry"`
|
|
|
|
RecordTimestamp time.Time `json:"record_timestamp"`
|
|
|
|
}
|
2020-02-01 15:31:56 -07:00
|
|
|
|
2020-02-11 07:56:19 -07:00
|
|
|
// Descr describes this plugin
|
|
|
|
func (h *Beer) Descr() string {
|
|
|
|
return "Queries [OpenDataSoft](https://public-us.opendatasoft.com/explore/dataset/open-beer-database/table/)'s beer database for a given beer."
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re returns the beer matching string
|
|
|
|
func (h *Beer) Re() string {
|
2020-02-10 16:08:33 -07:00
|
|
|
return `(?i)^beer: `
|
|
|
|
}
|
|
|
|
|
2020-02-01 15:31:56 -07:00
|
|
|
func (h *Beer) fix(msg string) string {
|
2020-02-11 07:56:19 -07:00
|
|
|
re := regexp.MustCompile(h.Re())
|
2020-02-01 15:14:46 -07:00
|
|
|
return re.ReplaceAllString(msg, "$1")
|
|
|
|
}
|
|
|
|
|
2020-02-10 17:10:57 -07:00
|
|
|
// Match determines if we should call the response for Beer
|
2020-05-13 17:05:01 -06:00
|
|
|
func (h *Beer) Match(_, msg string) bool {
|
2020-02-11 07:56:19 -07:00
|
|
|
re := regexp.MustCompile(h.Re())
|
2020-02-01 15:31:56 -07:00
|
|
|
return re.MatchString(msg)
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:45:42 -07:00
|
|
|
func (h *Beer) pretty(b BeerResp, random bool) string {
|
|
|
|
idx := 0
|
|
|
|
|
|
|
|
if random {
|
|
|
|
idx = rand.Intn(len(b.Records))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s (%s) by %s from %s, %s - IBU: %d, ABV: %.1f %s\n%s",
|
|
|
|
b.Records[idx].Fields.Name,
|
|
|
|
b.Records[idx].Fields.StyleName,
|
|
|
|
b.Records[idx].Fields.NameBreweries,
|
|
|
|
b.Records[idx].Fields.City,
|
|
|
|
b.Records[idx].Fields.State,
|
|
|
|
b.Records[idx].Fields.Ibu,
|
|
|
|
b.Records[idx].Fields.Abv,
|
|
|
|
b.Records[idx].Fields.Website,
|
|
|
|
b.Records[idx].Fields.Descript,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:04:04 -07:00
|
|
|
// SetStore we don't need a store here.
|
2020-05-13 17:05:01 -06:00
|
|
|
func (h *Beer) SetStore(_ PluginStore) {}
|
2020-02-05 22:04:04 -07:00
|
|
|
|
2021-04-01 16:15:25 -06:00
|
|
|
func (h *Beer) Process(from, msg string) string {
|
|
|
|
beer := h.fix(msg)
|
|
|
|
resp := "¯\\_(ツ)_/¯"
|
2020-02-10 17:10:57 -07:00
|
|
|
if beer != "" {
|
2020-02-19 18:03:29 -07:00
|
|
|
var beers = &BeerResp{}
|
|
|
|
u := fmt.Sprintf("%s%s",
|
|
|
|
"https://data.opendatasoft.com/api/records/1.0/search?dataset=open-beer-database%40public-us&q=",
|
|
|
|
url.PathEscape(beer),
|
|
|
|
)
|
|
|
|
req := HTTPRequest{
|
|
|
|
Method: "GET",
|
|
|
|
ResBody: beers,
|
|
|
|
URL: u,
|
|
|
|
}
|
|
|
|
err := req.DoJSON()
|
2020-02-10 17:10:57 -07:00
|
|
|
if err != nil {
|
2021-04-01 16:15:25 -06:00
|
|
|
return fmt.Sprintf("sorry %s, I can't look for beer. (%s)", from, err)
|
2020-02-10 17:10:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2020-02-19 18:03:29 -07:00
|
|
|
case beers.Nhits == 1:
|
2021-04-01 16:15:25 -06:00
|
|
|
resp = h.pretty(*beers, false)
|
2020-02-19 18:03:29 -07:00
|
|
|
case beers.Nhits > 1:
|
2021-04-01 16:15:25 -06:00
|
|
|
resp = fmt.Sprintf("Found %d beers, here is a random one:\n%s", beers.Nhits, h.pretty(*beers, true))
|
2020-02-01 15:14:46 -07:00
|
|
|
}
|
2021-04-01 16:15:25 -06:00
|
|
|
|
2020-02-01 15:14:46 -07:00
|
|
|
}
|
2021-04-01 16:15:25 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
// RespondText to looking up of beer requests
|
|
|
|
func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
|
|
|
|
return SendText(c, ev.RoomID, h.Process(ev.Sender, post))
|
2020-02-01 15:14:46 -07:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:47:14 -07:00
|
|
|
// Name Beer!
|
2020-02-01 15:14:46 -07:00
|
|
|
func (h *Beer) Name() string {
|
|
|
|
return "Beer"
|
|
|
|
}
|