mirror of https://github.com/velour/catbase.git
Compare commits
4 Commits
15645526d5
...
dcb6c3740d
Author | SHA1 | Date |
---|---|---|
|
dcb6c3740d | |
|
49991e530a | |
|
bcb5d43ea6 | |
|
a5f0380585 |
|
@ -71,3 +71,4 @@ run.sh
|
|||
.idea
|
||||
logs
|
||||
util/files
|
||||
impact.ttf
|
||||
|
|
1
go.mod
1
go.mod
|
@ -27,7 +27,6 @@ require (
|
|||
github.com/gonum/floats v0.0.0-20181209220543-c233463c7e82 // indirect
|
||||
github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 // indirect
|
||||
github.com/google/uuid v1.1.1
|
||||
github.com/gorilla/mux v1.7.4
|
||||
github.com/gorilla/websocket v1.4.1 // indirect
|
||||
github.com/james-bowman/nlp v0.0.0-20191016091239-d9dbfaff30c6
|
||||
github.com/james-bowman/sparse v0.0.0-20190423065201-80c6877364c7 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -66,8 +66,6 @@ github.com/gonum/internal v0.0.0-20181124074243-f884aa714029 h1:8jtTdc+Nfj9AR+0s
|
|||
github.com/gonum/internal v0.0.0-20181124074243-f884aa714029/go.mod h1:Pu4dmpkhSyOzRwuXkOgAvijx4o+4YMUJJo9OvPYMkks=
|
||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
|
||||
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
|
||||
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/fogleman/gg"
|
||||
"github.com/google/uuid"
|
||||
|
@ -16,6 +17,7 @@ import (
|
|||
|
||||
"github.com/velour/catbase/bot"
|
||||
"github.com/velour/catbase/bot/msg"
|
||||
"github.com/velour/catbase/bot/user"
|
||||
"github.com/velour/catbase/config"
|
||||
)
|
||||
|
||||
|
@ -23,16 +25,35 @@ type MemePlugin struct {
|
|||
bot bot.Bot
|
||||
c *config.Config
|
||||
|
||||
images map[string][]byte
|
||||
images cachedImages
|
||||
}
|
||||
|
||||
type cachedImage struct {
|
||||
created time.Time
|
||||
repr []byte
|
||||
}
|
||||
|
||||
var horizon = 24 * 7
|
||||
|
||||
type cachedImages map[string]*cachedImage
|
||||
|
||||
func (ci cachedImages) cleanup() {
|
||||
for key, img := range ci {
|
||||
if time.Now().After(img.created.Add(time.Hour * time.Duration(horizon))) {
|
||||
delete(ci, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func New(b bot.Bot) *MemePlugin {
|
||||
mp := &MemePlugin{
|
||||
bot: b,
|
||||
c: b.Config(),
|
||||
images: make(map[string][]byte),
|
||||
images: make(cachedImages),
|
||||
}
|
||||
|
||||
horizon = mp.c.GetInt("meme.horizon", horizon)
|
||||
|
||||
b.Register(mp, bot.Message, mp.message)
|
||||
b.Register(mp, bot.Help, mp.help)
|
||||
mp.registerWeb(b.DefaultConnector())
|
||||
|
@ -59,14 +80,24 @@ func (p *MemePlugin) registerWeb(c bot.Connector) {
|
|||
r.ParseForm()
|
||||
log.Debug().Msgf("Meme:\n%+v", r.PostForm.Get("text"))
|
||||
channel := r.PostForm.Get("channel_id")
|
||||
user := r.PostForm.Get("user_name")
|
||||
channelName := r.PostForm.Get("channel_name")
|
||||
from := r.PostForm.Get("user_name")
|
||||
log.Debug().Msgf("channel: %s", channel)
|
||||
|
||||
parts := strings.SplitN(r.PostForm.Get("text"), " ", 2)
|
||||
isCmd, message := bot.IsCmd(p.c, parts[1])
|
||||
format := parts[0]
|
||||
|
||||
log.Debug().Strs("parts", parts).Msgf("Meme:\n%+v", r.PostForm.Get("text"))
|
||||
w.WriteHeader(200)
|
||||
|
||||
id := p.genMeme(parts[0], parts[1])
|
||||
top, bottom := "", message
|
||||
parts = strings.Split(message, "\n")
|
||||
if len(parts) > 1 {
|
||||
top, bottom = parts[0], parts[1]
|
||||
}
|
||||
|
||||
id := p.genMeme(format, top, bottom)
|
||||
baseURL := p.c.Get("BaseURL", `https://catbase.velour.ninja`)
|
||||
u, _ := url.Parse(baseURL)
|
||||
u.Path = path.Join(u.Path, "meme", "img", id)
|
||||
|
@ -74,16 +105,35 @@ func (p *MemePlugin) registerWeb(c bot.Connector) {
|
|||
log.Debug().Msgf("image is at %s", u.String())
|
||||
p.bot.Send(c, bot.Message, channel, "", bot.ImageAttachment{
|
||||
URL: u.String(),
|
||||
AltTxt: fmt.Sprintf("%s: %s", user, parts[1]),
|
||||
AltTxt: fmt.Sprintf("%s: %s", from, message),
|
||||
})
|
||||
w.Write(nil)
|
||||
m := msg.Message{
|
||||
User: &user.User{
|
||||
ID: from,
|
||||
Name: from,
|
||||
Admin: false,
|
||||
},
|
||||
Channel: channel,
|
||||
ChannelName: channelName,
|
||||
Body: message,
|
||||
Command: isCmd,
|
||||
Time: time.Now(),
|
||||
}
|
||||
|
||||
p.bot.Receive(c, bot.Message, m)
|
||||
})
|
||||
|
||||
http.HandleFunc("/meme/img/", func(w http.ResponseWriter, r *http.Request) {
|
||||
_, file := path.Split(r.URL.Path)
|
||||
id := file
|
||||
img := p.images[id]
|
||||
w.Write(img)
|
||||
if img, ok := p.images[id]; ok {
|
||||
w.Write(img.repr)
|
||||
} else {
|
||||
w.WriteHeader(404)
|
||||
w.Write([]byte("not found"))
|
||||
}
|
||||
p.images.cleanup()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -110,8 +160,9 @@ var defaultFormats = map[string]string{
|
|||
"raptor": "Philosoraptor.jpg",
|
||||
}
|
||||
|
||||
func (p *MemePlugin) genMeme(meme, text string) string {
|
||||
const fontSize = 36
|
||||
func (p *MemePlugin) genMeme(meme, top, bottom string) string {
|
||||
fontSizes := []float64{48, 36, 24, 16, 12}
|
||||
fontSize := fontSizes[0]
|
||||
|
||||
formats := p.c.GetMap("meme.memes", defaultFormats)
|
||||
|
||||
|
@ -138,9 +189,17 @@ func (p *MemePlugin) genMeme(meme, text string) string {
|
|||
m := gg.NewContext(w, h)
|
||||
m.DrawImage(img, 0, 0)
|
||||
fontLocation := p.c.Get("meme.font", "impact.ttf")
|
||||
err = m.LoadFontFace(fontLocation, fontSize) // problem
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("could not load font")
|
||||
for _, sz := range fontSizes {
|
||||
err = m.LoadFontFace(fontLocation, sz) // problem
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("could not load font")
|
||||
}
|
||||
topW, _ := m.MeasureString(top)
|
||||
botW, _ := m.MeasureString(bottom)
|
||||
if topW < float64(w) && botW < float64(w) {
|
||||
fontSize = sz
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Apply black stroke
|
||||
|
@ -153,18 +212,21 @@ func (p *MemePlugin) genMeme(meme, text string) string {
|
|||
continue
|
||||
}
|
||||
x := float64(w/2 + dx)
|
||||
y := float64(h - fontSize + dy)
|
||||
m.DrawStringAnchored(text, x, y, 0.5, 0.5)
|
||||
y := float64(h) - fontSize + float64(dy)
|
||||
y0 := fontSize + float64(dy)
|
||||
m.DrawStringAnchored(top, x, y0, 0.5, 0.5)
|
||||
m.DrawStringAnchored(bottom, x, y, 0.5, 0.5)
|
||||
}
|
||||
}
|
||||
|
||||
// Apply white fill
|
||||
m.SetHexColor("#FFF")
|
||||
m.DrawStringAnchored(text, float64(w)/2, float64(h)-fontSize, 0.5, 0.5)
|
||||
m.DrawStringAnchored(top, float64(w)/2, fontSize, 0.5, 0.5)
|
||||
m.DrawStringAnchored(bottom, float64(w)/2, float64(h)-fontSize, 0.5, 0.5)
|
||||
|
||||
i := bytes.Buffer{}
|
||||
png.Encode(&i, m.Image())
|
||||
p.images[path] = i.Bytes()
|
||||
p.images[path] = &cachedImage{time.Now(), i.Bytes()}
|
||||
|
||||
log.Debug().Msgf("Saved to %s\n", path)
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ func (p *TalkerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messa
|
|||
if message.Command && strings.HasPrefix(lowermessage, "cowsay") {
|
||||
msg, err := p.cowSay(strings.TrimPrefix(message.Body, "cowsay "))
|
||||
if err != nil {
|
||||
p.bot.Send(c, bot.Message, channel, "Error running cowsay: %s", err)
|
||||
p.bot.Send(c, bot.Message, channel, fmt.Sprintf("Error running cowsay: %s", err))
|
||||
return true
|
||||
}
|
||||
p.bot.Send(c, bot.Message, channel, msg)
|
||||
|
|
Loading…
Reference in New Issue