mirror of https://github.com/velour/catbase.git
beers: host your own images
This commit is contained in:
parent
645a533f49
commit
b13385774e
|
@ -39,7 +39,7 @@ type Kind int
|
||||||
type Callback func(Connector, Kind, msg.Message, ...interface{}) bool
|
type Callback func(Connector, Kind, msg.Message, ...interface{}) bool
|
||||||
type CallbackMap map[string]map[Kind][]Callback
|
type CallbackMap map[string]map[Kind][]Callback
|
||||||
|
|
||||||
// Bot interface serves to allow mocking of the actual bot
|
// b interface serves to allow mocking of the actual bot
|
||||||
type Bot interface {
|
type Bot interface {
|
||||||
// Config allows access to the bot's configuration system
|
// Config allows access to the bot's configuration system
|
||||||
Config() *config.Config
|
Config() *config.Config
|
||||||
|
|
|
@ -3,21 +3,29 @@
|
||||||
package beers
|
package beers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"image"
|
||||||
|
"image/png"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"github.com/nfnt/resize"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
"github.com/velour/catbase/bot/msg"
|
"github.com/velour/catbase/bot/msg"
|
||||||
|
"github.com/velour/catbase/config"
|
||||||
"github.com/velour/catbase/plugins/counter"
|
"github.com/velour/catbase/plugins/counter"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,8 +33,11 @@ import (
|
||||||
|
|
||||||
const itemName = ":beer:"
|
const itemName = ":beer:"
|
||||||
|
|
||||||
|
var cachedImages = map[string][]byte{}
|
||||||
|
|
||||||
type BeersPlugin struct {
|
type BeersPlugin struct {
|
||||||
Bot bot.Bot
|
b bot.Bot
|
||||||
|
c *config.Config
|
||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
|
|
||||||
untapdCache map[int]bool
|
untapdCache map[int]bool
|
||||||
|
@ -52,16 +63,22 @@ func New(b bot.Bot) *BeersPlugin {
|
||||||
log.Fatal().Err(err)
|
log.Fatal().Err(err)
|
||||||
}
|
}
|
||||||
p := &BeersPlugin{
|
p := &BeersPlugin{
|
||||||
Bot: b,
|
b: b,
|
||||||
|
c: b.Config(),
|
||||||
db: b.DB(),
|
db: b.DB(),
|
||||||
|
|
||||||
untapdCache: make(map[int]bool),
|
untapdCache: make(map[int]bool),
|
||||||
}
|
}
|
||||||
for _, channel := range b.Config().GetArray("Untappd.Channels", []string{}) {
|
|
||||||
go p.untappdLoop(b.DefaultConnector(), channel)
|
|
||||||
}
|
|
||||||
b.Register(p, bot.Message, p.message)
|
b.Register(p, bot.Message, p.message)
|
||||||
b.Register(p, bot.Help, p.help)
|
b.Register(p, bot.Help, p.help)
|
||||||
|
|
||||||
|
p.registerWeb()
|
||||||
|
|
||||||
|
for _, channel := range p.c.GetArray("Untappd.Channels", []string{}) {
|
||||||
|
go p.untappdLoop(b.DefaultConnector(), channel)
|
||||||
|
}
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,13 +105,13 @@ func (p *BeersPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messag
|
||||||
count, err := strconv.Atoi(parts[2])
|
count, err := strconv.Atoi(parts[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// if it's not a number, maybe it's a nick!
|
// if it's not a number, maybe it's a nick!
|
||||||
p.Bot.Send(c, bot.Message, channel, "Sorry, that didn't make any sense.")
|
p.b.Send(c, bot.Message, channel, "Sorry, that didn't make any sense.")
|
||||||
}
|
}
|
||||||
|
|
||||||
if count < 0 {
|
if count < 0 {
|
||||||
// you can't be negative
|
// you can't be negative
|
||||||
msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick)
|
msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick)
|
||||||
p.Bot.Send(c, bot.Message, channel, msg)
|
p.b.Send(c, bot.Message, channel, msg)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if parts[1] == "+=" {
|
if parts[1] == "+=" {
|
||||||
|
@ -108,14 +125,14 @@ func (p *BeersPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messag
|
||||||
p.randomReply(c, channel)
|
p.randomReply(c, channel)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
p.Bot.Send(c, bot.Message, channel, "I don't know your math.")
|
p.b.Send(c, bot.Message, channel, "I don't know your math.")
|
||||||
}
|
}
|
||||||
} else if len(parts) == 2 {
|
} else if len(parts) == 2 {
|
||||||
if p.doIKnow(parts[1]) {
|
if p.doIKnow(parts[1]) {
|
||||||
p.reportCount(c, parts[1], channel, false)
|
p.reportCount(c, parts[1], channel, false)
|
||||||
} else {
|
} else {
|
||||||
msg := fmt.Sprintf("Sorry, I don't know %s.", parts[1])
|
msg := fmt.Sprintf("Sorry, I don't know %s.", parts[1])
|
||||||
p.Bot.Send(c, bot.Message, channel, msg)
|
p.b.Send(c, bot.Message, channel, msg)
|
||||||
}
|
}
|
||||||
} else if len(parts) == 1 {
|
} else if len(parts) == 1 {
|
||||||
p.reportCount(c, nick, channel, true)
|
p.reportCount(c, nick, channel, true)
|
||||||
|
@ -139,7 +156,7 @@ func (p *BeersPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messag
|
||||||
channel := message.Channel
|
channel := message.Channel
|
||||||
|
|
||||||
if len(parts) < 2 {
|
if len(parts) < 2 {
|
||||||
p.Bot.Send(c, bot.Message, channel, "You must also provide a user name.")
|
p.b.Send(c, bot.Message, channel, "You must also provide a user name.")
|
||||||
} else if len(parts) == 3 {
|
} else if len(parts) == 3 {
|
||||||
chanNick = parts[2]
|
chanNick = parts[2]
|
||||||
} else if len(parts) == 4 {
|
} else if len(parts) == 4 {
|
||||||
|
@ -164,7 +181,7 @@ func (p *BeersPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messag
|
||||||
log.Error().Err(err).Msgf("Error registering untappd")
|
log.Error().Err(err).Msgf("Error registering untappd")
|
||||||
}
|
}
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
p.Bot.Send(c, bot.Message, channel, "I'm already watching you.")
|
p.b.Send(c, bot.Message, channel, "I'm already watching you.")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
_, err = p.db.Exec(`insert into untappd (
|
_, err = p.db.Exec(`insert into untappd (
|
||||||
|
@ -180,11 +197,11 @@ func (p *BeersPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messag
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msgf("Error registering untappd")
|
log.Error().Err(err).Msgf("Error registering untappd")
|
||||||
p.Bot.Send(c, bot.Message, channel, "I can't see.")
|
p.b.Send(c, bot.Message, channel, "I can't see.")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
p.Bot.Send(c, bot.Message, channel, "I'll be watching you.")
|
p.b.Send(c, bot.Message, channel, "I'll be watching you.")
|
||||||
|
|
||||||
p.checkUntappd(c, channel)
|
p.checkUntappd(c, channel)
|
||||||
|
|
||||||
|
@ -207,7 +224,7 @@ func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message,
|
||||||
msg := "Beers: imbibe by using either beers +=,=,++ or with the !imbibe/drink " +
|
msg := "Beers: imbibe by using either beers +=,=,++ or with the !imbibe/drink " +
|
||||||
"commands. I'll keep a count of how many beers you've had and then if you want " +
|
"commands. I'll keep a count of how many beers you've had and then if you want " +
|
||||||
"to reset, just !puke it all up!"
|
"to reset, just !puke it all up!"
|
||||||
p.Bot.Send(c, bot.Message, message.Channel, msg)
|
p.b.Send(c, bot.Message, message.Channel, msg)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,13 +264,13 @@ func (p *BeersPlugin) reportCount(c bot.Connector, nick, channel string, himself
|
||||||
msg = fmt.Sprintf("You've had %d beers so far, %s.", beers, nick)
|
msg = fmt.Sprintf("You've had %d beers so far, %s.", beers, nick)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.Bot.Send(c, bot.Message, channel, msg)
|
p.b.Send(c, bot.Message, channel, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) puke(c bot.Connector, user string, channel string) {
|
func (p *BeersPlugin) puke(c bot.Connector, user string, channel string) {
|
||||||
p.setBeers(user, 0)
|
p.setBeers(user, 0)
|
||||||
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user)
|
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user)
|
||||||
p.Bot.Send(c, bot.Message, channel, msg)
|
p.b.Send(c, bot.Message, channel, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) doIKnow(nick string) bool {
|
func (p *BeersPlugin) doIKnow(nick string) bool {
|
||||||
|
@ -268,7 +285,7 @@ func (p *BeersPlugin) doIKnow(nick string) bool {
|
||||||
// Sends random affirmation to the channel. This could be better (with a datastore for sayings)
|
// Sends random affirmation to the channel. This could be better (with a datastore for sayings)
|
||||||
func (p *BeersPlugin) randomReply(c bot.Connector, channel string) {
|
func (p *BeersPlugin) randomReply(c bot.Connector, channel string) {
|
||||||
replies := []string{"ZIGGY! ZAGGY!", "HIC!", "Stay thirsty, my friend!"}
|
replies := []string{"ZIGGY! ZAGGY!", "HIC!", "Stay thirsty, my friend!"}
|
||||||
p.Bot.Send(c, bot.Message, channel, replies[rand.Intn(len(replies))])
|
p.b.Send(c, bot.Message, channel, replies[rand.Intn(len(replies))])
|
||||||
}
|
}
|
||||||
|
|
||||||
type checkin struct {
|
type checkin struct {
|
||||||
|
@ -326,7 +343,7 @@ type Beers struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
|
func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
|
||||||
token := p.Bot.Config().Get("Untappd.Token", "NONE")
|
token := p.c.Get("Untappd.Token", "NONE")
|
||||||
if token == "NONE" || token == "" {
|
if token == "NONE" || token == "" {
|
||||||
return []checkin{}, fmt.Errorf("No untappd token")
|
return []checkin{}, fmt.Errorf("No untappd token")
|
||||||
}
|
}
|
||||||
|
@ -361,7 +378,7 @@ func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
|
func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
|
||||||
token := p.Bot.Config().Get("Untappd.Token", "NONE")
|
token := p.c.Get("Untappd.Token", "NONE")
|
||||||
if token == "NONE" {
|
if token == "NONE" {
|
||||||
log.Info().
|
log.Info().
|
||||||
Msg(`Set config value "untappd.token" if you wish to enable untappd`)
|
Msg(`Set config value "untappd.token" if you wish to enable untappd`)
|
||||||
|
@ -398,6 +415,16 @@ func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user, ok := userMap[checkin.User.User_name]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
p.sendCheckin(c, channel, user, checkin)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *BeersPlugin) sendCheckin(c bot.Connector, channel string, user untappdUser, checkin checkin) {
|
||||||
venue := ""
|
venue := ""
|
||||||
switch v := checkin.Venue.(type) {
|
switch v := checkin.Venue.(type) {
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
|
@ -405,10 +432,7 @@ func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
|
||||||
}
|
}
|
||||||
beerName := checkin.Beer["beer_name"].(string)
|
beerName := checkin.Beer["beer_name"].(string)
|
||||||
breweryName := checkin.Brewery["brewery_name"].(string)
|
breweryName := checkin.Brewery["brewery_name"].(string)
|
||||||
user, ok := userMap[checkin.User.User_name]
|
|
||||||
if !ok {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Msgf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s",
|
Msgf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s",
|
||||||
user.chanNick, user.untappdUser, checkin.User.User_name)
|
user.chanNick, user.untappdUser, checkin.User.User_name)
|
||||||
|
@ -439,17 +463,18 @@ func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
|
||||||
}
|
}
|
||||||
if checkin.Media.Count > 0 {
|
if checkin.Media.Count > 0 {
|
||||||
if strings.Contains(checkin.Media.Items[0].Photo.Photo_img_lg, "photos-processing") {
|
if strings.Contains(checkin.Media.Items[0].Photo.Photo_img_lg, "photos-processing") {
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
mediaURL := p.getMedia(checkin.Media.Items[0].Photo.Photo_img_lg)
|
||||||
args = append(args, bot.ImageAttachment{
|
args = append(args, bot.ImageAttachment{
|
||||||
URL: checkin.Media.Items[0].Photo.Photo_img_lg,
|
URL: mediaURL,
|
||||||
AltTxt: "Here's a photo",
|
AltTxt: "Here's a photo",
|
||||||
})
|
})
|
||||||
} else if !p.untapdCache[checkin.Checkin_id] {
|
} else if !p.untapdCache[checkin.Checkin_id] {
|
||||||
// Mark checkin as "seen" but not complete, continue to next checkin
|
// Mark checkin as "seen" but not complete, continue to next checkin
|
||||||
log.Debug().Msgf("Deferring checkin: %#v", checkin)
|
log.Debug().Msgf("Deferring checkin: %#v", checkin)
|
||||||
p.untapdCache[checkin.Checkin_id] = true
|
p.untapdCache[checkin.Checkin_id] = true
|
||||||
continue
|
return
|
||||||
} else {
|
} else {
|
||||||
// We've seen this checkin, so unmark and accept that there's no media
|
// We've seen this checkin, so unmark and accept that there's no media
|
||||||
delete(p.untapdCache, checkin.Checkin_id)
|
delete(p.untapdCache, checkin.Checkin_id)
|
||||||
|
@ -466,13 +491,82 @@ func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Int("checkin_id", checkin.Checkin_id).
|
Int("checkin_id", checkin.Checkin_id).
|
||||||
Str("msg", msg).
|
Str("msg", msg).
|
||||||
|
Interface("args", args).
|
||||||
Msg("checkin")
|
Msg("checkin")
|
||||||
p.Bot.Send(c, bot.Message, args...)
|
|
||||||
|
p.b.Send(c, bot.Message, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *BeersPlugin) getMedia(src string) string {
|
||||||
|
u, err := url.Parse(src)
|
||||||
|
if err != nil {
|
||||||
|
return src
|
||||||
}
|
}
|
||||||
|
|
||||||
|
img, err := downloadMedia(u)
|
||||||
|
if err != nil {
|
||||||
|
return src
|
||||||
|
}
|
||||||
|
|
||||||
|
r := img.Bounds()
|
||||||
|
w := r.Dx()
|
||||||
|
h := r.Dy()
|
||||||
|
|
||||||
|
maxSz := p.c.GetFloat64("maxImgSz", 750.0)
|
||||||
|
|
||||||
|
if w > h {
|
||||||
|
scale := maxSz / float64(w)
|
||||||
|
w = int(float64(w) * scale)
|
||||||
|
h = int(float64(h) * scale)
|
||||||
|
} else {
|
||||||
|
scale := maxSz / float64(h)
|
||||||
|
w = int(float64(w) * scale)
|
||||||
|
h = int(float64(h) * scale)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug().Msgf("trynig to resize to %v, %v", w, h)
|
||||||
|
img = resize.Resize(uint(w), uint(h), img, resize.Lanczos3)
|
||||||
|
r = img.Bounds()
|
||||||
|
w = r.Dx()
|
||||||
|
h = r.Dy()
|
||||||
|
log.Debug().Msgf("resized to %v, %v", w, h)
|
||||||
|
|
||||||
|
buf := bytes.Buffer{}
|
||||||
|
err = png.Encode(&buf, img)
|
||||||
|
if err != nil {
|
||||||
|
return src
|
||||||
|
}
|
||||||
|
|
||||||
|
baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`)
|
||||||
|
id := uuid.New().String()
|
||||||
|
|
||||||
|
cachedImages[id] = buf.Bytes()
|
||||||
|
|
||||||
|
u, _ = url.Parse(baseURL)
|
||||||
|
u.Path = path.Join(u.Path, "beers", "img", id)
|
||||||
|
|
||||||
|
log.Debug().Msgf("New image at %s", u)
|
||||||
|
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func downloadMedia(u *url.URL) (image.Image, error) {
|
||||||
|
res, err := http.Get(u.String())
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Msgf("template from %s failed because of %v", u.String(), err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
image, _, err := image.Decode(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Msgf("Could not decode %v because of %v", u, err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return image, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) untappdLoop(c bot.Connector, channel string) {
|
func (p *BeersPlugin) untappdLoop(c bot.Connector, channel string) {
|
||||||
frequency := p.Bot.Config().GetInt("Untappd.Freq", 120)
|
frequency := p.c.GetInt("Untappd.Freq", 120)
|
||||||
if frequency == 0 {
|
if frequency == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -484,3 +578,18 @@ func (p *BeersPlugin) untappdLoop(c bot.Connector, channel string) {
|
||||||
p.checkUntappd(c, channel)
|
p.checkUntappd(c, channel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *BeersPlugin) registerWeb() {
|
||||||
|
http.HandleFunc("/beers/img/", p.img)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *BeersPlugin) img(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, file := path.Split(r.URL.Path)
|
||||||
|
id := file
|
||||||
|
if img, ok := cachedImages[id]; ok {
|
||||||
|
w.Write(img)
|
||||||
|
} else {
|
||||||
|
w.WriteHeader(404)
|
||||||
|
w.Write([]byte("not found"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -348,7 +348,7 @@ func (p *MemePlugin) genMeme(meme, top, bottom, bully string) (string, error) {
|
||||||
w := r.Dx()
|
w := r.Dx()
|
||||||
h := r.Dy()
|
h := r.Dy()
|
||||||
|
|
||||||
maxSz := 750.0
|
maxSz := p.c.GetFloat64("maxImgSz", 750.0)
|
||||||
|
|
||||||
if w > h {
|
if w > h {
|
||||||
scale := maxSz / float64(w)
|
scale := maxSz / float64(w)
|
||||||
|
|
|
@ -62,7 +62,7 @@ func (p *ReactionPlugin) message(c bot.Connector, kind bot.Kind, message msg.Mes
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bot will always react if a message contains a check word
|
// b will always react if a message contains a check word
|
||||||
// Note that reactions must not be enclosed in :
|
// Note that reactions must not be enclosed in :
|
||||||
func (p *ReactionPlugin) checkReactions(c bot.Connector, m msg.Message) {
|
func (p *ReactionPlugin) checkReactions(c bot.Connector, m msg.Message) {
|
||||||
checkWords := p.config.GetArray("reaction.checkwords", []string{})
|
checkWords := p.config.GetArray("reaction.checkwords", []string{})
|
||||||
|
|
Loading…
Reference in New Issue