Last active
December 12, 2015 07:28
-
-
Save suapapa/4736991 to your computer and use it in GitHub Desktop.
Practice to retrieve and parse GDG chapter's event feed.
This file contains hidden or 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 ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"time" | |
) | |
type Event struct { | |
End string `json:"end,omitempty"` | |
Description string `json:"description,omitempty"` | |
Title string `json:"title,omitempty"` | |
TemporalRelation string `json:"temporalRelation,omitempty"` | |
Start string `json:"start,omitempty"` | |
Link string `json:"link,omitempty"` | |
Location string `json:"location,omitempty"` | |
Id string `json:"id,omitempty"` | |
} | |
const ( | |
TF_GDG_EVENT = "02 Jan 2006 15:04 -0700" | |
TF_CALENDAR = "20060102" | |
) | |
func (e Event) PrintSummary() { | |
st, _ := time.Parse(TF_GDG_EVENT, e.Start) | |
et, _ := time.Parse(TF_GDG_EVENT, e.End) | |
fmt.Println(st.Format(TF_CALENDAR), "~", et.Format(TF_CALENDAR), e.Title) | |
} | |
func FatalIf(err error) { | |
if err != nil { | |
log.Fatalln(err) | |
} | |
} | |
func getGDGEvents(cid string, start, end time.Time) []Event { | |
/* log.Println(start, end) */ | |
base := "https://developers.google.com/events/feed/json" | |
requestURL := base + fmt.Sprintf("?group=%s", cid) | |
requestURL += fmt.Sprintf("&start=%d", start.Unix()) | |
if end.After(start) { | |
requestURL += fmt.Sprintf("&end=%d", end.Unix()) | |
} | |
/* log.Println(requestURL) */ | |
resp, err := http.Get(requestURL) | |
FatalIf(err) | |
defer resp.Body.Close() | |
body, err := ioutil.ReadAll(resp.Body) | |
FatalIf(err) | |
var evts []Event | |
json.Unmarshal(body, &evts) | |
return evts | |
} | |
func main() { | |
/* cid := "102751345660146384940" // chapter ID of Czech Republic Uber */ | |
/* cid := "12714242728066184635" // chapter ID for GDG Golnag Korea */ | |
if len(os.Args) < 3 { | |
fmt.Printf("%s: CHAPTERID YEAR\n", os.Args[0]) | |
os.Exit(1) | |
} | |
cid := os.Args[1] | |
year := os.Args[2] | |
st, _ := time.Parse(TF_CALENDAR, year+"0101") | |
et, _ := time.Parse(TF_CALENDAR, year+"1231") | |
for _, e := range getGDGEvents(cid, st.UTC(), et.UTC()) { | |
e.PrintSummary() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ go run gdg_events.go 116593433820963567116 2012
20120314 ~ 20120314 Tea Time
20120327 ~ 20120327 Tea Time 2nd
20120427 ~ 20120527 GDG Suwon Android Pre-Hackathon 2012
20120428 ~ 20120428 GDG Suwon Android Hackathon 2012
20120501 ~ 20120501 Tea Time 3rd
20120512 ~ 20120512 DevFestX Korea 2012
20121117 ~ 20121118 GDG Lounge on Google HackFair
20121126 ~ 20121126 GDG Suwon - 6th Tea Time