meme: use urls

This commit is contained in:
Chris Sexton 2020-04-28 14:58:23 -04:00
parent 2e746f7b80
commit 5a1e572645
1 changed files with 12 additions and 8 deletions

View File

@ -2,7 +2,6 @@ package meme
import ( import (
"bytes" "bytes"
"fmt"
"image" "image"
"image/png" "image/png"
"net/http" "net/http"
@ -87,16 +86,15 @@ func (p *MemePlugin) registerWeb(c bot.Connector) {
}) })
} }
func DownloadTemplate(file string) image.Image { func DownloadTemplate(u *url.URL) image.Image {
url := fmt.Sprintf("https://imgflip.com/s/meme/%s", file) res, err := http.Get(u.String())
res, err := http.Get(url)
if err != nil { if err != nil {
log.Error().Msgf("%s template from %s failed because of %v", file, url, err) log.Error().Msgf("template from %s failed because of %v", u.String(), err)
} }
defer res.Body.Close() defer res.Body.Close()
image, _, err := image.Decode(res.Body) image, _, err := image.Decode(res.Body)
if err != nil { if err != nil {
log.Error().Msgf("Could not decode %s because of %v", file, err) log.Error().Msgf("Could not decode %v because of %v", u, err)
} }
return image return image
} }
@ -122,7 +120,13 @@ func (p *MemePlugin) genMeme(meme, text string) string {
if !ok { if !ok {
imgName = meme imgName = meme
} }
img := DownloadTemplate(imgName)
u, err := url.Parse(imgName)
if err != nil {
u, _ = url.Parse("https://imgflip.com/s/meme/" + imgName)
}
img := DownloadTemplate(u)
r := img.Bounds() r := img.Bounds()
w := r.Dx() w := r.Dx()
h := r.Dy() h := r.Dy()
@ -130,7 +134,7 @@ func (p *MemePlugin) genMeme(meme, text string) string {
m := gg.NewContext(w, h) m := gg.NewContext(w, h)
m.DrawImage(img, 0, 0) m.DrawImage(img, 0, 0)
fontLocation := p.c.Get("meme.font", "impact.ttf") fontLocation := p.c.Get("meme.font", "impact.ttf")
err := m.LoadFontFace(fontLocation, fontSize) // problem err = m.LoadFontFace(fontLocation, fontSize) // problem
if err != nil { if err != nil {
log.Error().Err(err).Msg("could not load font") log.Error().Err(err).Msg("could not load font")
} }