-
Notifications
You must be signed in to change notification settings - Fork 1
/
meetup.go
127 lines (114 loc) · 2.85 KB
/
meetup.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// MeetupError represents an error of meetup API
type MeetupError struct {
Code string `json:"code"`
Message string `json:"message"`
}
func (e MeetupError) Error() string {
return e.Message
}
// MeetupErrors contains a list of MeetupErrors
type MeetupErrors struct {
Errors []MeetupError `json:"errors"`
}
func (e MeetupErrors) Error() string {
errs := make([]string, 0)
for _, err := range e.Errors {
errs = append(errs, err.Error())
}
return strings.Join(errs, " + ")
}
// MeetupEvent represents an event with minimal info
type MeetupEvent struct {
ID string `json:"id"`
Name string `json:"name"`
LocalDate string `json:"local_date"`
LocalTime string `json:"local_time"`
OKRsvpCount int `json:"yes_rsvp_count"`
}
// MeetupMember represents a member with minimal info
type MeetupMember struct {
Member struct {
ID int `json:"id"`
Name string `json:"name"`
Photo struct {
ID int `json:"id"`
PhotoLink string `json:"photo_link"`
} `json:"photo"`
} `json:"member"`
Response string `json:"response"`
}
const (
endpoint = "http://api.meetup.com/GDG-Golang-Korea"
)
// MeetupResvMembersOfLastEvent returns list of confirmed members of last event
func MeetupResvMembersOfLastEvent() ([]MeetupMember, error) {
evt, err := meetupGetLatestEvent()
if err != nil {
return nil, err
}
return meetupGetOKRsvpMembers(evt.ID)
}
// meetupGetLatestEvent get a latest event information
func meetupGetLatestEvent() (*MeetupEvent, error) {
url := meetupBuildURL("/events?status=past,upcoming&desc=true&page=1")
b, err := meetupRequest(url)
if err != nil {
return nil, err
}
var events []MeetupEvent
err = json.Unmarshal(b, &events)
if err := json.Unmarshal(b, &events); err != nil {
var aerr MeetupErrors
if err := json.Unmarshal(b, &aerr); err == nil {
return nil, aerr
}
return nil, err
}
return &events[0], nil
}
// meetupGetOKRsvpMembers get a list of confirmed members of an event
func meetupGetOKRsvpMembers(id string) ([]MeetupMember, error) {
url := meetupBuildURL(fmt.Sprintf("/events/%s/rsvps", id))
b, err := meetupRequest(url)
if err != nil {
return nil, err
}
var members []MeetupMember
if err := json.Unmarshal(b, &members); err != nil {
var aerr MeetupErrors
if err := json.Unmarshal(b, &aerr); err == nil {
return nil, aerr
}
return nil, err
}
var okMembers []MeetupMember
for _, m := range members {
if m.Response == "yes" {
okMembers = append(okMembers, m)
}
}
return okMembers, nil
}
func meetupRequest(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}
func meetupBuildURL(path string) string {
return endpoint + path
}