This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
groups.go
186 lines (154 loc) · 4.55 KB
/
groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2014-2015 Chadev. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/chadev/Chadev_ircbot/meetup"
"github.com/danryan/hal"
"github.com/texttheater/golang-levenshtein/levenshtein"
)
// Groups contains data on the various dev groups.
type Groups struct {
Group []Group `json:"groups"`
}
// Group contains data from the "groups" array in the JSON object.
type Group struct {
Name string `json:"name"`
GitHub string `json:"github"`
CodeofConduct string `json:"code-of-conduct"`
Urls []URLs `json:"urls"`
meetup string
}
// URLs contains the name and url to the group website
type URLs struct {
Name string `json:"name"`
URL string `json:"url"`
}
func parseGroups() (Groups, error) {
var g Groups
r, err := http.Get("http://chadev.com/groups.json")
if err != nil {
return g, err
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return g, err
}
err = json.Unmarshal(b, &g)
if err != nil {
return Groups{}, err
}
return g, nil
}
var groupListHandler = hear(`(groups|meetups) list`, "(groups|meetups) list", "Lists all groups that are known to Ash", func(res *hal.Response) error {
g, err := parseGroups()
if err != nil {
hal.Logger.Errorf("failed parsing group list: %v", err)
res.Send("Sorry, I encountered an error while parsing the groups list")
return err
}
var gn []string
for _, val := range g.Group {
gn = append(gn, val.Name)
}
names := strings.Join(gn, ", ")
return res.Send(fmt.Sprintf("Here is a list of groups: %s", names))
})
var groupDetailsHandler = hear(`(group|meetup) details (.+)`, "(group|meetup) details [group name]", "Returns details about a group", func(res *hal.Response) error {
name := res.Match[2]
g, err := parseGroups()
if err != nil {
hal.Logger.Errorf("failed parsing group list: %v", err)
res.Send("Sorry, I encountered an error while parsing the groups list")
return err
}
group := searchGroups(g, name)
for _, u := range group.Urls {
if u.Name == "website" || u.Name == "meetup" {
m := parseMeetupName(u.URL)
if m != "" {
group.meetup = m
}
}
}
var nextEvent string
if group.meetup != "" {
nextEvent, err = meetup.GetNextMeetup(group.meetup)
if err != nil {
hal.Logger.Errorf("failed fetching event from meetup.com: %v", err)
}
}
var urls []string
for _, u := range group.Urls {
urls = append(urls, u.URL)
}
ul := strings.Join(urls, ", ")
res.Send(fmt.Sprintf("Group name: %s URL: %s", group.Name, ul))
if nextEvent != "" {
res.Send(nextEvent)
}
return nil
})
var groupRSVPHandler = hear(`(group|meetup) rsvps (.+)`, "(group|meetup) rsvps [group name]", "Gets the RSVP count for the named group's next meeting", func(res *hal.Response) error {
name := res.Match[2]
g, err := parseGroups()
if err != nil {
hal.Logger.Errorf("failed parsing group list: %v", err)
res.Send("Sorry, I encountered an error while parsing the groups list")
return err
}
group := searchGroups(g, name)
for _, u := range group.Urls {
if u.Name == "website" || u.Name == "meetup" {
m := parseMeetupName(u.URL)
if m != "" {
group.meetup = m
}
}
}
if group.meetup == "" {
res.Send(fmt.Sprintf("%s is using an unsupported event system, can't fetch RSVP information", group.Name))
return nil
}
rsvp, err := meetup.GetMeetupRSVP(group.meetup)
if err != nil {
hal.Logger.Errorf("failed fetching RSVP information: %v", err)
res.Send("I was unable to fetch the latest RSVP informaion for this group")
return err
}
if rsvp != "" {
res.Send(fmt.Sprintf("%s RSVP breakdown: %s", group.Name, rsvp))
} else {
res.Send("There are either no upcoming events or no RSVP for the event yet")
}
return nil
})
func parseMeetupName(u string) string {
// meetup URLs are structured as www.meetup.com/(group name)
u = strings.TrimSuffix(u, "/") // trim trailing slash if present
parts := strings.Split(u, "/")
return parts[len(parts)-1]
}
func searchGroups(g Groups, n string) Group {
distance := int(^uint(0) >> 1) // nitialize to "infinity"
var idx int
n = strings.ToUpper(strings.TrimSpace(n))
for i := 0; i < len(g.Group); i++ {
cleanGroup := strings.ToUpper(strings.TrimSpace(g.Group[i].Name))
if n == cleanGroup {
return g.Group[i]
}
newdistance := levenshtein.DistanceForStrings([]rune(n), []rune(cleanGroup), levenshtein.DefaultOptions)
if newdistance < distance {
distance = newdistance
idx = i
}
}
return g.Group[idx]
}