2016-01-17 18:00:44 +00:00
|
|
|
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
|
2013-12-10 23:37:07 +00:00
|
|
|
|
2016-03-24 17:32:40 +00:00
|
|
|
package beers
|
2012-08-17 23:22:37 +00:00
|
|
|
|
2012-08-18 01:39:26 +00:00
|
|
|
import (
|
2020-05-25 18:42:56 +00:00
|
|
|
"bytes"
|
2012-12-01 23:01:03 +00:00
|
|
|
"encoding/json"
|
2012-12-01 23:58:21 +00:00
|
|
|
"errors"
|
2012-08-18 01:39:26 +00:00
|
|
|
"fmt"
|
2020-05-25 18:42:56 +00:00
|
|
|
"image"
|
|
|
|
"image/png"
|
2012-12-01 23:01:03 +00:00
|
|
|
"io/ioutil"
|
2012-08-25 04:49:48 +00:00
|
|
|
"math/rand"
|
2012-12-01 23:01:03 +00:00
|
|
|
"net/http"
|
2020-05-25 18:42:56 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2021-02-01 20:14:18 +00:00
|
|
|
"regexp"
|
2012-08-18 01:39:26 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2014-04-20 19:24:45 +00:00
|
|
|
|
2021-07-28 15:32:59 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-05-25 18:42:56 +00:00
|
|
|
"github.com/google/uuid"
|
2016-03-19 18:02:46 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-05-25 18:42:56 +00:00
|
|
|
"github.com/nfnt/resize"
|
2019-03-07 16:35:42 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-05-04 17:35:31 +00:00
|
|
|
|
2016-01-17 18:00:44 +00:00
|
|
|
"github.com/velour/catbase/bot"
|
2016-04-01 14:20:03 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
2020-05-25 18:42:56 +00:00
|
|
|
"github.com/velour/catbase/config"
|
2016-03-19 18:27:02 +00:00
|
|
|
"github.com/velour/catbase/plugins/counter"
|
2012-08-18 01:39:26 +00:00
|
|
|
)
|
2012-08-17 23:22:37 +00:00
|
|
|
|
|
|
|
// This is a skeleton plugin to serve as an example and quick copy/paste for new plugins.
|
|
|
|
|
2018-01-05 21:54:12 +00:00
|
|
|
const itemName = ":beer:"
|
|
|
|
|
2020-05-25 18:42:56 +00:00
|
|
|
var cachedImages = map[string][]byte{}
|
|
|
|
|
2012-08-17 23:22:37 +00:00
|
|
|
type BeersPlugin struct {
|
2020-05-25 18:42:56 +00:00
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
db *sqlx.DB
|
2020-05-04 17:35:31 +00:00
|
|
|
|
|
|
|
untapdCache map[int]bool
|
2021-02-01 20:14:18 +00:00
|
|
|
handlers bot.HandlerTable
|
2016-01-15 06:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type untappdUser struct {
|
|
|
|
id int64
|
|
|
|
untappdUser string
|
|
|
|
channel string
|
|
|
|
lastCheckin int
|
|
|
|
chanNick string
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 20:21:26 +00:00
|
|
|
// New BeersPlugin creates a new BeersPlugin with the Plugin interface
|
2019-02-05 19:41:38 +00:00
|
|
|
func New(b bot.Bot) *BeersPlugin {
|
|
|
|
if _, err := b.DB().Exec(`create table if not exists untappd (
|
2016-01-15 06:12:26 +00:00
|
|
|
id integer primary key,
|
2016-03-11 17:48:41 +00:00
|
|
|
untappdUser string,
|
|
|
|
channel string,
|
|
|
|
lastCheckin integer,
|
2016-01-15 06:12:26 +00:00
|
|
|
chanNick string
|
|
|
|
);`); err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Err(err)
|
2016-01-15 06:12:26 +00:00
|
|
|
}
|
2019-02-15 20:12:09 +00:00
|
|
|
p := &BeersPlugin{
|
2020-05-25 18:42:56 +00:00
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
db: b.DB(),
|
2020-05-04 17:35:31 +00:00
|
|
|
|
|
|
|
untapdCache: make(map[int]bool),
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
2020-05-25 18:42:56 +00:00
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
p.register()
|
2019-02-05 19:41:38 +00:00
|
|
|
b.Register(p, bot.Help, p.help)
|
2020-05-25 18:42:56 +00:00
|
|
|
|
|
|
|
p.registerWeb()
|
|
|
|
|
2020-05-26 15:38:55 +00:00
|
|
|
token := p.c.Get("Untappd.Token", "NONE")
|
|
|
|
if token == "NONE" || token == "" {
|
|
|
|
log.Error().Msgf("No untappd token. Checking disabled.")
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-05-25 18:42:56 +00:00
|
|
|
for _, channel := range p.c.GetArray("Untappd.Channels", []string{}) {
|
|
|
|
go p.untappdLoop(b.DefaultConnector(), channel)
|
|
|
|
}
|
|
|
|
|
2019-02-15 20:12:09 +00:00
|
|
|
return p
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
func (p *BeersPlugin) register() {
|
|
|
|
p.handlers = bot.HandlerTable{
|
|
|
|
{Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^beers?\s?(?P<operator>(\+=|-=|=))\s?(?P<amount>\d+)$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
op := r.Values["operator"]
|
|
|
|
count, _ := strconv.Atoi(r.Values["amount"])
|
|
|
|
nick := r.Msg.User.Name
|
2021-02-14 21:57:22 +00:00
|
|
|
id := r.Msg.User.ID
|
2021-02-01 20:14:18 +00:00
|
|
|
|
|
|
|
switch op {
|
|
|
|
case "=":
|
|
|
|
if count == 0 {
|
2021-06-17 17:59:29 +00:00
|
|
|
p.puke(r)
|
2021-02-01 20:14:18 +00:00
|
|
|
} else {
|
2021-06-17 17:59:29 +00:00
|
|
|
p.setBeers(&r, nick, id, count)
|
2021-02-01 20:14:18 +00:00
|
|
|
p.randomReply(r.Conn, r.Msg.Channel)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
case "+=":
|
2021-06-17 17:59:29 +00:00
|
|
|
p.addBeers(&r, nick, id, count)
|
2021-02-01 20:14:18 +00:00
|
|
|
p.randomReply(r.Conn, r.Msg.Channel)
|
|
|
|
return true
|
|
|
|
case "-=":
|
2021-06-17 17:59:29 +00:00
|
|
|
p.addBeers(&r, nick, id, -count)
|
2021-02-01 20:14:18 +00:00
|
|
|
p.randomReply(r.Conn, r.Msg.Channel)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: false,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^beers?\s?(?P<operator>(\+\+|--))$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
op := r.Values["operator"]
|
|
|
|
nick := r.Msg.User.Name
|
2021-02-14 21:57:22 +00:00
|
|
|
id := r.Msg.User.ID
|
2021-02-01 20:14:18 +00:00
|
|
|
if op == "++" {
|
2021-06-17 17:59:29 +00:00
|
|
|
p.addBeers(&r, nick, id, 1)
|
2021-02-01 20:14:18 +00:00
|
|
|
} else {
|
2021-06-17 17:59:29 +00:00
|
|
|
p.addBeers(&r, nick, id, -1)
|
2021-02-01 20:14:18 +00:00
|
|
|
}
|
|
|
|
p.randomReply(r.Conn, r.Msg.Channel)
|
2016-01-15 14:44:22 +00:00
|
|
|
return true
|
2021-02-01 20:14:18 +00:00
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^beers( (?P<who>\S+))?$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
who := r.Values["who"]
|
|
|
|
if who == "" {
|
|
|
|
who = r.Msg.User.Name
|
|
|
|
}
|
2021-02-14 21:57:22 +00:00
|
|
|
if p.doIKnow(who, "") {
|
|
|
|
p.reportCount(r.Conn, who, "", r.Msg.Channel, false)
|
2012-08-18 01:39:26 +00:00
|
|
|
} else {
|
2021-02-01 20:14:18 +00:00
|
|
|
msg := fmt.Sprintf("Sorry, I don't know %s.", who)
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^puke$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
2021-06-17 17:59:29 +00:00
|
|
|
p.puke(r)
|
2021-02-01 20:14:18 +00:00
|
|
|
return true
|
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: true,
|
2021-02-01 20:18:36 +00:00
|
|
|
Regex: regexp.MustCompile(`(?i)^` +
|
|
|
|
strings.Join(p.c.GetArray("beers.imbibewords", []string{"imbibe", "quaff"}), "|") + `$`),
|
2021-02-01 20:14:18 +00:00
|
|
|
Handler: func(r bot.Request) bool {
|
2021-06-17 17:59:29 +00:00
|
|
|
p.addBeers(&r, r.Msg.User.Name, r.Msg.User.ID, 1)
|
2021-02-01 20:14:18 +00:00
|
|
|
p.randomReply(r.Conn, r.Msg.Channel)
|
|
|
|
return true
|
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^reguntappd (?P<who>\S+)$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
chanNick := r.Msg.User.Name
|
|
|
|
channel := r.Msg.Channel
|
|
|
|
untappdNick := r.Values["who"]
|
|
|
|
|
|
|
|
u := untappdUser{
|
|
|
|
untappdUser: untappdNick,
|
|
|
|
chanNick: chanNick,
|
|
|
|
channel: channel,
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
2012-12-01 23:01:03 +00:00
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
log.Info().
|
|
|
|
Str("untappdUser", u.untappdUser).
|
|
|
|
Str("nick", u.chanNick).
|
|
|
|
Msg("Creating Untappd user")
|
2013-08-31 04:11:54 +00:00
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
var count int
|
|
|
|
err := p.db.QueryRow(`select count(*) from untappd
|
2016-01-15 06:12:26 +00:00
|
|
|
where untappdUser = ?`, u.untappdUser).Scan(&count)
|
2021-02-01 20:14:18 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Error registering untappd")
|
|
|
|
}
|
|
|
|
if count > 0 {
|
|
|
|
p.b.Send(r.Conn, bot.Message, channel, "I'm already watching you.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, err = p.db.Exec(`insert into untappd (
|
|
|
|
untappdUser,
|
|
|
|
channel,
|
|
|
|
lastCheckin,
|
|
|
|
chanNick
|
|
|
|
) values (?, ?, ?, ?);`,
|
|
|
|
u.untappdUser,
|
|
|
|
u.channel,
|
|
|
|
0,
|
|
|
|
u.chanNick,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Error registering untappd")
|
|
|
|
p.b.Send(r.Conn, bot.Message, channel, "I can't see.")
|
|
|
|
return true
|
|
|
|
}
|
2012-12-01 23:58:21 +00:00
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
p.b.Send(r.Conn, bot.Message, channel, "I'll be watching you.")
|
2013-08-31 19:58:15 +00:00
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
p.checkUntappd(r.Conn, channel)
|
2013-08-31 19:58:15 +00:00
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
return true
|
|
|
|
}},
|
|
|
|
{Kind: bot.Message, IsCmd: true,
|
|
|
|
Regex: regexp.MustCompile(`(?i)^checkuntappd$`),
|
|
|
|
Handler: func(r bot.Request) bool {
|
|
|
|
log.Info().
|
|
|
|
Str("user", r.Msg.User.Name).
|
|
|
|
Msgf("Checking untappd at request of user.")
|
|
|
|
p.checkUntappd(r.Conn, r.Msg.Channel)
|
|
|
|
return true
|
|
|
|
}},
|
2012-12-01 23:01:03 +00:00
|
|
|
}
|
2021-02-01 20:14:18 +00:00
|
|
|
p.b.RegisterTable(p, p.handlers)
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 20:14:18 +00:00
|
|
|
// Message responds to the bot hook on recieving messages.
|
|
|
|
// This function returns true if the plugin responds in a meaningful way to the users message.
|
|
|
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
2012-08-17 23:22:37 +00:00
|
|
|
// Help responds to help requests. Every plugin must implement a help function.
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
2012-08-18 03:11:36 +00:00
|
|
|
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 " +
|
|
|
|
"to reset, just !puke it all up!"
|
2020-05-25 18:42:56 +00:00
|
|
|
p.b.Send(c, bot.Message, message.Channel, msg)
|
2019-02-05 19:41:38 +00:00
|
|
|
return true
|
2012-08-17 23:22:37 +00:00
|
|
|
}
|
2012-08-18 01:39:26 +00:00
|
|
|
|
2021-02-14 21:57:22 +00:00
|
|
|
func getUserBeers(db *sqlx.DB, user, id string) counter.Item {
|
|
|
|
// TODO: really ought to have an ID here
|
|
|
|
booze, _ := counter.GetUserItem(db, user, id, itemName)
|
2016-03-19 18:27:02 +00:00
|
|
|
return booze
|
|
|
|
}
|
|
|
|
|
2021-06-17 17:59:29 +00:00
|
|
|
func (p *BeersPlugin) setBeers(r *bot.Request, user, id string, amount int) {
|
2021-02-14 21:57:22 +00:00
|
|
|
ub := getUserBeers(p.db, user, id)
|
2021-06-17 17:59:29 +00:00
|
|
|
err := ub.Update(r, amount)
|
2016-03-19 18:27:02 +00:00
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msgf("Error saving beers")
|
2016-01-15 14:44:22 +00:00
|
|
|
}
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 17:59:29 +00:00
|
|
|
func (p *BeersPlugin) addBeers(r *bot.Request, user, id string, delta int) {
|
2021-02-14 21:57:22 +00:00
|
|
|
ub := getUserBeers(p.db, user, id)
|
2021-06-17 17:59:29 +00:00
|
|
|
err := ub.UpdateDelta(r, delta)
|
2016-03-19 18:27:02 +00:00
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msgf("Error saving beers")
|
2016-03-19 18:27:02 +00:00
|
|
|
}
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 21:57:22 +00:00
|
|
|
func (p *BeersPlugin) getBeers(nick, id string) int {
|
|
|
|
ub := getUserBeers(p.db, nick, id)
|
2016-03-19 18:27:02 +00:00
|
|
|
return ub.Count
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 21:57:22 +00:00
|
|
|
func (p *BeersPlugin) reportCount(c bot.Connector, nick, id, channel string, himself bool) {
|
|
|
|
beers := p.getBeers(nick, id)
|
2012-08-18 02:30:04 +00:00
|
|
|
msg := fmt.Sprintf("%s has had %d beers so far.", nick, beers)
|
|
|
|
if himself {
|
2012-08-18 02:53:39 +00:00
|
|
|
if beers == 0 {
|
|
|
|
msg = fmt.Sprintf("You really need to get drinkin, %s!", nick)
|
|
|
|
} else {
|
|
|
|
msg = fmt.Sprintf("You've had %d beers so far, %s.", beers, nick)
|
|
|
|
}
|
2012-08-18 02:30:04 +00:00
|
|
|
}
|
2020-05-25 18:42:56 +00:00
|
|
|
p.b.Send(c, bot.Message, channel, msg)
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 17:59:29 +00:00
|
|
|
func (p *BeersPlugin) puke(r bot.Request) {
|
|
|
|
p.setBeers(&r, r.Msg.User.Name, r.Msg.User.ID, 0)
|
|
|
|
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", r.Msg.User.Name)
|
|
|
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 21:57:22 +00:00
|
|
|
func (p *BeersPlugin) doIKnow(nick, id string) bool {
|
|
|
|
count := p.getBeers(nick, id)
|
2012-08-18 02:30:04 +00:00
|
|
|
return count > 0
|
2012-08-18 01:39:26 +00:00
|
|
|
}
|
2012-08-25 01:52:15 +00:00
|
|
|
|
|
|
|
// Sends random affirmation to the channel. This could be better (with a datastore for sayings)
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *BeersPlugin) randomReply(c bot.Connector, channel string) {
|
2012-08-25 01:52:15 +00:00
|
|
|
replies := []string{"ZIGGY! ZAGGY!", "HIC!", "Stay thirsty, my friend!"}
|
2020-05-25 18:42:56 +00:00
|
|
|
p.b.Send(c, bot.Message, channel, replies[rand.Intn(len(replies))])
|
2012-08-25 04:49:48 +00:00
|
|
|
}
|
2012-12-01 23:01:03 +00:00
|
|
|
|
|
|
|
type checkin struct {
|
|
|
|
Checkin_id int
|
|
|
|
Created_at string
|
|
|
|
Checkin_comment string
|
2016-04-25 00:07:01 +00:00
|
|
|
Rating_score float64
|
|
|
|
Beer map[string]interface{}
|
|
|
|
Brewery map[string]interface{}
|
|
|
|
Venue interface{}
|
|
|
|
User mrUntappd
|
2016-05-01 20:56:58 +00:00
|
|
|
Media struct {
|
|
|
|
Count int
|
|
|
|
Items []struct {
|
|
|
|
Photo_id int
|
|
|
|
Photo struct {
|
2017-10-05 19:00:23 +00:00
|
|
|
Photo_img_sm string
|
|
|
|
Photo_img_md string
|
|
|
|
Photo_img_lg string
|
2016-05-01 20:56:58 +00:00
|
|
|
Photo_img_og string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 17:14:15 +00:00
|
|
|
Badges struct {
|
|
|
|
Count int
|
|
|
|
Items []struct {
|
|
|
|
BadgeName string `json:"badge_name"`
|
|
|
|
BadgeDescription string `json:"badge_description"`
|
2019-09-28 17:45:53 +00:00
|
|
|
BadgeImage struct {
|
|
|
|
Sm string
|
|
|
|
Md string
|
|
|
|
Lg string
|
|
|
|
} `json:"badge_image"`
|
2019-09-28 17:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-31 04:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type mrUntappd struct {
|
|
|
|
Uid int
|
|
|
|
User_name string
|
|
|
|
Relationship string
|
2012-12-01 23:01:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type checkins struct {
|
|
|
|
Count int
|
|
|
|
Items []checkin
|
|
|
|
}
|
|
|
|
|
|
|
|
type resp struct {
|
|
|
|
Checkins checkins
|
|
|
|
}
|
|
|
|
|
|
|
|
type Beers struct {
|
|
|
|
Response resp
|
|
|
|
}
|
|
|
|
|
2013-08-31 19:58:15 +00:00
|
|
|
func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
|
2020-05-25 18:42:56 +00:00
|
|
|
token := p.c.Get("Untappd.Token", "NONE")
|
2019-10-26 19:01:29 +00:00
|
|
|
if token == "NONE" || token == "" {
|
2019-01-21 19:24:03 +00:00
|
|
|
return []checkin{}, fmt.Errorf("No untappd token")
|
|
|
|
}
|
|
|
|
|
|
|
|
access_token := "?access_token=" + token
|
2014-04-20 19:24:45 +00:00
|
|
|
baseUrl := "https://api.untappd.com/v4/checkin/recent/"
|
2012-12-01 23:58:21 +00:00
|
|
|
|
2013-08-31 04:11:54 +00:00
|
|
|
url := baseUrl + access_token + "&limit=25"
|
2012-12-01 23:58:21 +00:00
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
2016-03-29 14:20:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return []checkin{}, err
|
|
|
|
}
|
2016-03-11 17:48:41 +00:00
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2012-12-01 23:58:21 +00:00
|
|
|
if err != nil {
|
2016-03-11 17:48:41 +00:00
|
|
|
return []checkin{}, err
|
2012-12-01 23:58:21 +00:00
|
|
|
}
|
2016-03-11 17:48:41 +00:00
|
|
|
|
2012-12-01 23:58:21 +00:00
|
|
|
if resp.StatusCode == 500 {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Msgf("Error querying untappd: %s, %s", resp.Status, body)
|
2013-08-31 19:58:15 +00:00
|
|
|
return []checkin{}, errors.New(resp.Status)
|
2012-12-01 23:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var beers Beers
|
|
|
|
err = json.Unmarshal(body, &beers)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err)
|
2016-03-11 17:48:41 +00:00
|
|
|
return []checkin{}, err
|
2012-12-01 23:58:21 +00:00
|
|
|
}
|
2013-08-31 19:58:15 +00:00
|
|
|
return beers.Response.Checkins.Items, nil
|
2012-12-01 23:01:03 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
|
2020-05-25 18:42:56 +00:00
|
|
|
token := p.c.Get("Untappd.Token", "NONE")
|
2019-01-22 00:16:57 +00:00
|
|
|
if token == "NONE" {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Info().
|
|
|
|
Msg(`Set config value "untappd.token" if you wish to enable untappd`)
|
2012-12-01 23:01:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-11 17:48:41 +00:00
|
|
|
userMap := make(map[string]untappdUser)
|
|
|
|
rows, err := p.db.Query(`select id, untappdUser, channel, lastCheckin, chanNick from untappd;`)
|
2016-01-15 06:12:26 +00:00
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Error getting untappd users")
|
2016-01-15 06:12:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
u := untappdUser{}
|
2016-03-11 17:48:41 +00:00
|
|
|
err := rows.Scan(&u.id, &u.untappdUser, &u.channel, &u.lastCheckin, &u.chanNick)
|
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Err(err)
|
2016-03-11 17:48:41 +00:00
|
|
|
}
|
2016-01-15 06:12:26 +00:00
|
|
|
userMap[u.untappdUser] = u
|
2016-03-11 17:48:41 +00:00
|
|
|
if u.chanNick == "" {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Fatal().Msg("Empty chanNick for no good reason.")
|
2016-03-11 17:48:41 +00:00
|
|
|
}
|
2013-08-31 04:11:54 +00:00
|
|
|
}
|
2012-12-02 00:00:51 +00:00
|
|
|
|
2013-08-31 19:58:15 +00:00
|
|
|
chks, err := p.pullUntappd()
|
2016-03-11 17:48:41 +00:00
|
|
|
if err != nil {
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Error().Err(err).Msg("Untappd ERROR")
|
2016-03-28 19:34:40 +00:00
|
|
|
return
|
2016-03-11 17:48:41 +00:00
|
|
|
}
|
2013-08-31 19:58:15 +00:00
|
|
|
for i := len(chks); i > 0; i-- {
|
|
|
|
checkin := chks[i-1]
|
|
|
|
|
2016-01-15 06:12:26 +00:00
|
|
|
if checkin.Checkin_id <= userMap[checkin.User.User_name].lastCheckin {
|
2013-08-31 04:11:54 +00:00
|
|
|
continue
|
|
|
|
}
|
2012-12-01 23:01:03 +00:00
|
|
|
|
2013-08-31 04:11:54 +00:00
|
|
|
user, ok := userMap[checkin.User.User_name]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2018-12-25 02:59:30 +00:00
|
|
|
|
2020-05-25 18:42:56 +00:00
|
|
|
p.sendCheckin(c, channel, user, checkin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BeersPlugin) sendCheckin(c bot.Connector, channel string, user untappdUser, checkin checkin) {
|
|
|
|
venue := ""
|
|
|
|
switch v := checkin.Venue.(type) {
|
|
|
|
case map[string]interface{}:
|
|
|
|
venue = " at " + v["venue_name"].(string)
|
|
|
|
}
|
|
|
|
beerName := checkin.Beer["beer_name"].(string)
|
|
|
|
breweryName := checkin.Brewery["brewery_name"].(string)
|
|
|
|
|
|
|
|
log.Debug().
|
|
|
|
Msgf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s",
|
|
|
|
user.chanNick, user.untappdUser, checkin.User.User_name)
|
|
|
|
|
2020-06-17 18:24:34 +00:00
|
|
|
args := []interface{}{}
|
2020-05-25 18:42:56 +00:00
|
|
|
if checkin.Badges.Count > 0 {
|
|
|
|
for _, b := range checkin.Badges.Items {
|
2019-03-10 03:40:03 +00:00
|
|
|
args = append(args, bot.ImageAttachment{
|
2020-05-25 18:42:56 +00:00
|
|
|
URL: b.BadgeImage.Sm,
|
|
|
|
AltTxt: b.BadgeName,
|
2019-03-10 03:40:03 +00:00
|
|
|
})
|
2016-05-01 20:56:58 +00:00
|
|
|
}
|
2020-05-25 18:42:56 +00:00
|
|
|
}
|
|
|
|
if checkin.Media.Count > 0 {
|
|
|
|
if strings.Contains(checkin.Media.Items[0].Photo.Photo_img_lg, "photos-processing") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
mediaURL := p.getMedia(checkin.Media.Items[0].Photo.Photo_img_lg)
|
|
|
|
args = append(args, bot.ImageAttachment{
|
|
|
|
URL: mediaURL,
|
|
|
|
AltTxt: "Here's a photo",
|
|
|
|
})
|
|
|
|
} else if !p.untapdCache[checkin.Checkin_id] {
|
|
|
|
// Mark checkin as "seen" but not complete, continue to next checkin
|
|
|
|
log.Debug().Msgf("Deferring checkin: %#v", checkin)
|
|
|
|
p.untapdCache[checkin.Checkin_id] = true
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
// We've seen this checkin, so unmark and accept that there's no media
|
|
|
|
delete(p.untapdCache, checkin.Checkin_id)
|
|
|
|
}
|
2016-05-01 20:56:58 +00:00
|
|
|
|
2020-06-17 18:24:34 +00:00
|
|
|
// Don't add beers till after a photo has been detected (or failed once)
|
2021-06-17 17:59:29 +00:00
|
|
|
p.addBeers(nil, user.chanNick, "", 1)
|
2021-02-14 21:57:22 +00:00
|
|
|
drunken := p.getBeers(user.chanNick, "")
|
2020-06-17 18:24:34 +00:00
|
|
|
|
|
|
|
msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d",
|
|
|
|
user.chanNick, beerName, breweryName, venue, drunken)
|
|
|
|
if checkin.Rating_score > 0 {
|
|
|
|
msg = fmt.Sprintf("%s. Rating: %.2f", msg, checkin.Rating_score)
|
|
|
|
}
|
|
|
|
if checkin.Checkin_comment != "" {
|
|
|
|
msg = fmt.Sprintf("%s -- %s",
|
|
|
|
msg, checkin.Checkin_comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
args = append([]interface{}{channel, msg}, args...)
|
|
|
|
|
2020-05-25 18:42:56 +00:00
|
|
|
user.lastCheckin = checkin.Checkin_id
|
|
|
|
_, err := p.db.Exec(`update untappd set
|
2016-01-15 06:12:26 +00:00
|
|
|
lastCheckin = ?
|
|
|
|
where id = ?`, user.lastCheckin, user.id)
|
2020-05-25 18:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("UPDATE ERROR!")
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug().
|
|
|
|
Int("checkin_id", checkin.Checkin_id).
|
|
|
|
Str("msg", msg).
|
|
|
|
Interface("args", args).
|
|
|
|
Msg("checkin")
|
|
|
|
|
|
|
|
p.b.Send(c, bot.Message, args...)
|
|
|
|
}
|
2012-12-01 23:01:03 +00:00
|
|
|
|
2020-05-25 18:42:56 +00:00
|
|
|
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
|
2013-08-31 04:11:54 +00:00
|
|
|
}
|
2020-05-25 18:42:56 +00:00
|
|
|
|
|
|
|
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
|
2013-08-31 04:11:54 +00:00
|
|
|
}
|
2012-12-01 23:01:03 +00:00
|
|
|
|
2019-05-27 23:21:53 +00:00
|
|
|
func (p *BeersPlugin) untappdLoop(c bot.Connector, channel string) {
|
2020-05-25 18:42:56 +00:00
|
|
|
frequency := p.c.GetInt("Untappd.Freq", 120)
|
2019-01-21 19:24:03 +00:00
|
|
|
if frequency == 0 {
|
|
|
|
return
|
|
|
|
}
|
2012-12-01 23:01:03 +00:00
|
|
|
|
2019-03-07 16:35:42 +00:00
|
|
|
log.Info().Msgf("Checking every %v seconds", frequency)
|
2013-08-31 19:58:15 +00:00
|
|
|
|
2013-08-31 04:11:54 +00:00
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(frequency) * time.Second)
|
2019-05-27 23:21:53 +00:00
|
|
|
p.checkUntappd(c, channel)
|
2012-12-01 23:01:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 18:42:56 +00:00
|
|
|
|
|
|
|
func (p *BeersPlugin) registerWeb() {
|
2021-07-28 15:32:59 +00:00
|
|
|
r := chi.NewRouter()
|
2021-08-05 14:01:35 +00:00
|
|
|
r.HandleFunc("/img/{id}", p.img)
|
2021-07-28 15:32:59 +00:00
|
|
|
p.b.RegisterWeb(r, "/beers")
|
2020-05-25 18:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BeersPlugin) img(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 14:01:35 +00:00
|
|
|
id := chi.URLParam(r, "id")
|
2020-05-25 18:42:56 +00:00
|
|
|
if img, ok := cachedImages[id]; ok {
|
|
|
|
w.Write(img)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Write([]byte("not found"))
|
|
|
|
}
|
|
|
|
}
|