Fix untappd integration

This commit is contained in:
Chris Sexton 2016-03-11 12:48:41 -05:00
parent c379087c7e
commit df0fee0237
4 changed files with 61 additions and 46 deletions

View File

@ -34,9 +34,11 @@ type Config struct {
LogLength int LogLength int
Admins []string Admins []string
HttpAddr string HttpAddr string
UntappdToken string Untappd struct {
UntappdFreq int Token string
UntappdChannels []string Freq int
Channels []string
}
EnforceNicks bool EnforceNicks bool
WelcomeMsgs []string WelcomeMsgs []string
TwitterConsumerKey string TwitterConsumerKey string

View File

@ -24,9 +24,11 @@
"Admins": ["<Admin Nick>"], "Admins": ["<Admin Nick>"],
"HttpAddr": "127.0.0.1:1337", "HttpAddr": "127.0.0.1:1337",
"UntappdToken": "<Your Token>", "Untappd": {
"UntappdFreq": 3600, "Token": "<Your Token>",
"UntappdChannels": [], "Freq": 3600,
"Channels": []
},
"TwitterConsumerKey": "<Consumer Key>", "TwitterConsumerKey": "<Consumer Key>",
"TwitterConsumerSecret": "<Consumer Secret>", "TwitterConsumerSecret": "<Consumer Secret>",

View File

@ -48,16 +48,16 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
id integer primary key, id integer primary key,
nick string, nick string,
count integer, count integer,
lastDrunk datetime lastDrunk integer
);`); err != nil { );`); err != nil {
log.Fatal(err) log.Fatal(err)
} }
if _, err := bot.DB.Exec(`create table if not exists untappd ( if _, err := bot.DB.Exec(`create table if not exists untappd (
id integer primary key, id integer primary key,
untappdUser string untappdUser string,
channel string channel string,
lastCheckin datetime lastCheckin integer,
chanNick string chanNick string
);`); err != nil { );`); err != nil {
log.Fatal(err) log.Fatal(err)
@ -68,7 +68,7 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
db: bot.DB, db: bot.DB,
} }
p.LoadData() p.LoadData()
for _, channel := range bot.Config.UntappdChannels { for _, channel := range bot.Config.Untappd.Channels {
go p.untappdLoop(channel) go p.untappdLoop(channel)
} }
return &p return &p
@ -243,7 +243,12 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
channel, channel,
lastCheckin, lastCheckin,
chanNick chanNick
) values (?, ?, ?, ?);`) ) values (?, ?, ?, ?);`,
u.untappdUser,
u.channel,
0,
u.chanNick,
)
if err != nil { if err != nil {
log.Println("Error registering untappd: ", err) log.Println("Error registering untappd: ", err)
p.Bot.SendMessage(channel, "I can't see.") p.Bot.SendMessage(channel, "I can't see.")
@ -258,6 +263,7 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
} }
if message.Command && parts[0] == "checkuntappd" { if message.Command && parts[0] == "checkuntappd" {
log.Println("Checking untappd at request of user.")
p.checkUntappd(channel) p.checkUntappd(channel)
return true return true
} }
@ -368,65 +374,66 @@ type Beers struct {
} }
func (p *BeersPlugin) pullUntappd() ([]checkin, error) { func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
access_token := "?access_token=" + p.Bot.Config.UntappdToken access_token := "?access_token=" + p.Bot.Config.Untappd.Token
baseUrl := "https://api.untappd.com/v4/checkin/recent/" baseUrl := "https://api.untappd.com/v4/checkin/recent/"
url := baseUrl + access_token + "&limit=25" url := baseUrl + access_token + "&limit=25"
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil {
panic(err)
}
if resp.StatusCode == 500 {
return []checkin{}, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
panic(err) return []checkin{}, err
}
if resp.StatusCode == 500 {
log.Printf("Error querying untappd: %s, %s", resp.Status, body)
return []checkin{}, errors.New(resp.Status)
} }
var beers Beers var beers Beers
err = json.Unmarshal(body, &beers) err = json.Unmarshal(body, &beers)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return []checkin{}, errors.New("Unable to parse JSON") return []checkin{}, err
} }
return beers.Response.Checkins.Items, nil return beers.Response.Checkins.Items, nil
} }
func (p *BeersPlugin) checkUntappd(channel string) { func (p *BeersPlugin) checkUntappd(channel string) {
if p.Bot.Config.UntappdToken == "<Your Token>" { if p.Bot.Config.Untappd.Token == "<Your Token>" {
log.Println("No Untappd token, cannot enable plugin.") log.Println("No Untappd token, cannot enable plugin.")
return return
} }
var users []untappdUser userMap := make(map[string]untappdUser)
rows, err := p.db.Query(`select *from untappd`) rows, err := p.db.Query(`select id, untappdUser, channel, lastCheckin, chanNick from untappd;`)
if err != nil { if err != nil {
log.Println("Error getting untappd users: ", err) log.Println("Error getting untappd users: ", err)
return return
} }
for rows.Next() { for rows.Next() {
u := untappdUser{} u := untappdUser{}
rows.Scan(&u.id, &u.untappdUser, &u.channel, &u.lastCheckin, &u.chanNick) err := rows.Scan(&u.id, &u.untappdUser, &u.channel, &u.lastCheckin, &u.chanNick)
users = append(users, u) if err != nil {
log.Fatal(err)
} }
userMap := make(map[string]untappdUser)
for _, u := range users {
userMap[u.untappdUser] = u userMap[u.untappdUser] = u
log.Printf("Found untappd user: %#v", u)
if u.chanNick == "" {
log.Fatal("Empty chanNick for no good reason.")
}
} }
chks, err := p.pullUntappd() chks, err := p.pullUntappd()
if err != nil {
log.Println("Untappd ERROR: ", err)
}
for i := len(chks); i > 0; i-- { for i := len(chks); i > 0; i-- {
checkin := chks[i-1] checkin := chks[i-1]
if err != nil {
log.Println("ERROR!:", err)
continue
}
if checkin.Checkin_id <= userMap[checkin.User.User_name].lastCheckin { if checkin.Checkin_id <= userMap[checkin.User.User_name].lastCheckin {
log.Printf("User %s already check in >%d", checkin.User.User_name, checkin.Checkin_id)
continue continue
} }
@ -441,6 +448,8 @@ func (p *BeersPlugin) checkUntappd(channel string) {
if !ok { if !ok {
continue continue
} }
log.Printf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s",
user.chanNick, user.untappdUser, checkin.User.User_name)
p.addBeers(user.chanNick) p.addBeers(user.chanNick)
drunken := p.getBeers(user.chanNick) drunken := p.getBeers(user.chanNick)
@ -465,7 +474,7 @@ func (p *BeersPlugin) checkUntappd(channel string) {
} }
func (p *BeersPlugin) untappdLoop(channel string) { func (p *BeersPlugin) untappdLoop(channel string) {
frequency := p.Bot.Config.UntappdFreq frequency := p.Bot.Config.Untappd.Freq
log.Println("Checking every ", frequency, " seconds") log.Println("Checking every ", frequency, " seconds")

View File

@ -93,10 +93,12 @@ func (s *Slack) SendMessageType(channel, messageType, subType, message string) e
} }
func (s *Slack) SendMessage(channel, message string) { func (s *Slack) SendMessage(channel, message string) {
log.Printf("Sending message to %s: %s", channel, message)
s.SendMessageType(channel, "message", "", message) s.SendMessageType(channel, "message", "", message)
} }
func (s *Slack) SendAction(channel, message string) { func (s *Slack) SendAction(channel, message string) {
log.Printf("Sending action to %s: %s", channel, message)
s.SendMessageType(channel, "message", "me_message", "_"+message+"_") s.SendMessageType(channel, "message", "me_message", "_"+message+"_")
} }