Finishing a few things, still need to clean it up

This commit is contained in:
Chris Sexton 2013-08-31 15:58:15 -04:00
parent a9fcee9a34
commit b97886fdde
1 changed files with 40 additions and 27 deletions

View File

@ -145,20 +145,34 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
} }
if message.Command && parts[0] == "reguntappd" { if message.Command && parts[0] == "reguntappd" {
chanNick := message.User.Name
channel := message.Channel
if len(parts) < 2 { if len(parts) < 2 {
p.Bot.SendMessage(channel, "You must also provide a user name.") p.Bot.SendMessage(channel, "You must also provide a user name.")
} else if len(parts) == 3 {
chanNick = parts[2]
} else if len(parts) == 4 {
chanNick = parts[2]
channel = parts[3]
} }
u := untappdUser{ u := untappdUser{
UntappdUser: parts[1], UntappdUser: parts[1],
ChanNick: message.User.Name, ChanNick: chanNick,
Channel: message.Channel, Channel: channel,
} }
p.Coll.Upsert(bson.M{"untappduser": u.UntappdUser}, user) log.Println("Creating Untappd user:", u.UntappdUser, "nick:", u.ChanNick)
p.getUntappdCheckins() _, err := p.Coll.Upsert(bson.M{"untappduser": u.UntappdUser}, u)
if err != nil {
log.Println("ERROR!!!:", err)
}
p.Bot.SendMessage(channel, "I'll be watching you.") p.Bot.SendMessage(channel, "I'll be watching you.")
p.checkUntappd(channel)
return true return true
} }
@ -272,6 +286,7 @@ type Beers struct {
} }
type untappdUser struct { type untappdUser struct {
Id bson.ObjectId `bson:"_id,omitempty"`
UntappdUser string UntappdUser string
Channel string Channel string
LastCheckin int LastCheckin int
@ -279,7 +294,7 @@ type untappdUser struct {
KnownCheckins [5]int KnownCheckins [5]int
} }
func (p *BeersPlugin) pullUntappd() (Beers, error) { func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
access_token := "?access_token=" + p.Bot.Config.UntappdToken access_token := "?access_token=" + p.Bot.Config.UntappdToken
baseUrl := "http://api.untappd.com/v4/checkin/recent/" baseUrl := "http://api.untappd.com/v4/checkin/recent/"
@ -291,32 +306,20 @@ func (p *BeersPlugin) pullUntappd() (Beers, error) {
panic(err) panic(err)
} }
if resp.StatusCode == 500 { if resp.StatusCode == 500 {
return Beers{}, errors.New(resp.Status) 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) panic(err)
} }
// fmt.Println(string(body))
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 Beers{}, errors.New("Unable to parse JSON") return []checkin{}, errors.New("Unable to parse JSON")
} }
return beers, nil return beers.Response.Checkins.Items, nil
}
func (p *BeersPlugin) getUntappdCheckins() ([]checkin, error) {
beers, err := p.pullUntappd()
if err != nil {
return nil, err
}
chks := beers.Response.Checkins.Items
return chks, nil
} }
func (p *BeersPlugin) checkUntappd(channel string) { func (p *BeersPlugin) checkUntappd(channel string) {
@ -335,9 +338,16 @@ func (p *BeersPlugin) checkUntappd(channel string) {
userMap[u.UntappdUser] = u userMap[u.UntappdUser] = u
} }
chks, err := p.getUntappdCheckins() chks, err := p.pullUntappd()
for _, checkin := range chks { for i := len(chks); i > 0; i-- {
checkin := chks[i-1]
if err != nil { if err != nil {
log.Println("ERROR!:", err)
continue
}
if checkin.Checkin_id <= userMap[checkin.User.User_name].LastCheckin {
continue continue
} }
@ -362,12 +372,13 @@ func (p *BeersPlugin) checkUntappd(channel string) {
msg, checkin.Checkin_comment) msg, checkin.Checkin_comment)
} }
p.Coll.Update( user.LastCheckin = checkin.Checkin_id
bson.M{"untappduser": user.UntappdUser}, err := p.Coll.Update(bson.M{"_id": user.Id}, user)
bson.M{"set": bson.M{"lastcheckin": checkin.Checkin_id}}, if err != nil {
) log.Println("UPDATE ERROR!:", err)
}
fmt.Println(msg) log.Println("checkin id:", checkin.Checkin_id, "Message:", msg)
p.Bot.SendMessage(channel, msg) p.Bot.SendMessage(channel, msg)
} }
} }
@ -375,6 +386,8 @@ 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.UntappdFreq
log.Println("Checking every ", frequency, " seconds")
for { for {
time.Sleep(time.Duration(frequency) * time.Second) time.Sleep(time.Duration(frequency) * time.Second)
p.checkUntappd(channel) p.checkUntappd(channel)