Fixed offset checking (broken on Untappd's end

This commit is contained in:
Chris Sexton 2012-12-01 18:58:21 -05:00
parent 396992f838
commit 62bef551a6
1 changed files with 61 additions and 42 deletions

View File

@ -3,6 +3,7 @@ package plugins
import ( import (
"bitbucket.org/phlyingpenguin/godeepintir/bot" "bitbucket.org/phlyingpenguin/godeepintir/bot"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"labix.org/v2/mgo" "labix.org/v2/mgo"
@ -143,7 +144,8 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
ChanNick: message.User.Name, ChanNick: message.User.Name,
} }
p.Coll.Upsert(bson.M{"untappduser": parts[1]}, u) p.getUntappdCheckins(u)
p.Bot.SendMessage(channel, "I'll be watching you.") p.Bot.SendMessage(channel, "I'll be watching you.")
return true return true
} }
@ -254,39 +256,23 @@ type untappdUser struct {
UntappdUser string UntappdUser string
LastCheckin int LastCheckin int
ChanNick string ChanNick string
KnownCheckins [5]int
} }
func (p *BeersPlugin) checkUntappd(channel string) { func (p *BeersPlugin) pullUntappd(user untappdUser) (Beers, error) {
if p.Bot.Config.UntappdToken == "<Your Token>" {
log.Println("No Untappd token, cannot enable plugin.")
return
}
access_token := "?access_token=" + p.Bot.Config.UntappdToken access_token := "?access_token=" + p.Bot.Config.UntappdToken
baseUrl := "http://api.untappd.com/v4/user/checkins/" baseUrl := "http://api.untappd.com/v4/user/checkins/"
frequency := p.Bot.Config.UntappdFreq
// users := []string{"Tir"} url := baseUrl + user.UntappdUser + access_token + "&limit=5"
log.Println("Request:", url)
for {
var users []untappdUser
p.Coll.Find(bson.M{"untappduser": bson.M{"$exists": true}}).All(&users)
log.Println("Found ", len(users), " untappd users")
for _, user := range users {
url := baseUrl + user.UntappdUser + access_token
if user.LastCheckin > 0 {
url = url + "?offset=" + strconv.Itoa(user.LastCheckin)
}
resp, err := http.Get(url) resp, err := http.Get(url)
if err != nil { if err != nil {
panic(err) panic(err)
} }
fmt.Println("User: ", user) if resp.StatusCode == 500 {
return Beers{}, errors.New(resp.Status)
}
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
panic(err) panic(err)
@ -297,24 +283,57 @@ func (p *BeersPlugin) checkUntappd(channel string) {
err = json.Unmarshal(body, &beers) err = json.Unmarshal(body, &beers)
if err != nil { if err != nil {
log.Println(user, err) log.Println(user, err)
continue return Beers{}, errors.New("Unable to parse JSON")
}
return beers, nil
} }
log.Printf("%+v\n\n%+v\n", resp, string(body)) func (p *BeersPlugin) getUntappdCheckins(user untappdUser) ([]checkin, error) {
beers, err := p.pullUntappd(user)
if err != nil {
return nil, err
}
// fmt.Println(beers.Response) var output []checkin
chks := beers.Response.Checkins.Items
checkins := beers.Response.Checkins.Items for _, chk := range chks {
// count := beers.Response.Checkins.Count if chk.Checkin_id > user.LastCheckin {
output = append(output, chk)
}
}
// fmt.Println("Tir has ", count, " checkins:") if len(chks) > 0 {
user.LastCheckin = chks[0].Checkin_id
if len(checkins) > 0 {
user.LastCheckin = checkins[0].Checkin_id
p.Coll.Upsert(bson.M{"untappduser": user.UntappdUser}, user) p.Coll.Upsert(bson.M{"untappduser": user.UntappdUser}, user)
} }
for _, checkin := range checkins { return output, nil
}
func (p *BeersPlugin) checkUntappd(channel string) {
if p.Bot.Config.UntappdToken == "<Your Token>" {
log.Println("No Untappd token, cannot enable plugin.")
return
}
frequency := p.Bot.Config.UntappdFreq
// users := []string{"Tir"}
for {
var users []untappdUser
p.Coll.Find(bson.M{"untappduser": bson.M{"$exists": true}}).All(&users)
log.Println("Found ", len(users), " untappd users")
for _, user := range users {
chks, err := p.getUntappdCheckins(user)
if err != nil {
continue
}
for _, checkin := range chks {
venue := "" venue := ""
switch v := checkin.Venue.(type) { switch v := checkin.Venue.(type) {
case map[string]interface{}: case map[string]interface{}: