Created
September 9, 2014 09:28
-
-
Save andeemarks/b400006b0d927110cc66 to your computer and use it in GitHub Desktop.
Getting upcoming Meetup events in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "net/http" | |
import "log" | |
import "os" | |
import "io/ioutil" | |
import "encoding/json" | |
// import "time" | |
type MeetupEventCollection struct { | |
Results []MeetupEvent | |
meta EventResultsMetaData | |
} | |
type EventResultsMetaData struct { | |
Lon string | |
Count int | |
Description string | |
Id string | |
Lat string | |
Link string | |
Method string | |
Next string | |
Title string | |
Total_count int | |
Updated float64 | |
Url string | |
} | |
type MeetupEvent struct { | |
id string | |
Name string | |
description string | |
rsvp_limit int | |
status string | |
visibility string | |
maybe_rsvp_count int | |
Venue EventVenue | |
fee EventFee | |
group EventGroup | |
Utc_offset float64 | |
Time float64 | |
waitlist_count int | |
updated float64 | |
yes_rsvp_count int | |
created float64 | |
Event_url string | |
headcount int | |
} | |
type EventVenue struct { | |
id float64 | |
lon float64 | |
lat float64 | |
repinned bool | |
Name string | |
state string | |
Address_1 string | |
City string | |
country string | |
} | |
type EventFee struct { | |
Amount float64 | |
Description string | |
Label string | |
Required string | |
Accepts string | |
Currency string | |
} | |
type EventGroup struct { | |
Id float64 | |
Group_Lat float64 | |
Group_Lon float64 | |
Name string | |
Join_Mode string | |
Urlname string | |
Who string | |
} | |
const MEETUP_API_KEY = "<YOUR_MEETUP_API_KEY>" | |
const MEETUP_MEMBER_ID = "<YOUR_MEETUP_MEMBER_KEY>" | |
const TIMEFRAME = "0,1w" | |
func main() { | |
jsonResponse := getEvents() | |
showEvents(jsonResponse) | |
// log.Printf("%d", time.Parse(jsonResponse.Results[0].Time) | |
} | |
func getEvents() MeetupEventCollection { | |
client := &http.Client{} | |
url := "https://api.meetup.com/2/events?key="+ MEETUP_API_KEY + "&member_id=" + MEETUP_MEMBER_ID + "&time=" + TIMEFRAME | |
req, err := http.NewRequest("GET", url, nil) | |
resp, err := client.Do(req) | |
handleErrorDuring("send request", err) | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
handleErrorDuring("read response", err) | |
var jsonResponse MeetupEventCollection | |
json.Unmarshal([]byte(body), &jsonResponse) | |
return jsonResponse | |
} | |
func showEvents(jsonResponse MeetupEventCollection) { | |
prettyResponse, err := json.MarshalIndent(jsonResponse, "", " ") | |
handleErrorDuring("marshal response", err) | |
os.Stdout.Write(prettyResponse) | |
} | |
func handleErrorDuring(context string, err error) { | |
if err != nil { | |
log.Fatal("Failed to %s: %s", context, err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment