package main import ( "flag" "fmt" "io/ioutil" "log" "time" "golang.org/x/oauth2/google" "google.golang.org/api/calendar/v3" ) var calName = flag.String("calendar", "", "name of calendar") var students = []string{ "oliver.park@heliosns.org", } var classes = []Class { Class{ Summary: "Test 2", Start: "22:00", End: "22:30", Days: []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday}, Zoom: "https://us02web.zoom.us/j/2274643506?pwd=Nm5NUXMwOVJKbEUzNE5VSkZCQzJ2UT09", }, } type Class struct { Summary string Start string End string Students []string Days []time.Weekday Zoom string } func main() { flag.Parse() b, err := ioutil.ReadFile("credentials.json") if err != nil { log.Fatal(err) } // If modifying these scopes, delete your previously saved token.json. config, err := google.ConfigFromJSON(b, calendar.CalendarScope) if err != nil { log.Fatal(err) } client, err := getClient(config) if err != nil { log.Fatal(err) } srv, err := calendar.New(client) if err != nil { log.Fatal(err) } calendars, err := srv.CalendarList.List().Do() if err != nil { log.Fatal(err) } var myCal *calendar.CalendarListEntry for _, item := range calendars.Items { if item.Summary == *calName { myCal = item break } } if myCal == nil { log.Fatalf("calendar '%s' not found", *calName) } loc, err := time.LoadLocation("US/Pacific") if err != nil { log.Fatal(err) } now := time.Now().In(loc) dateStr := now.Format("2006-01-02") // TOOD: loop through days for _, class := range classes { found := false for _, day := range class.Days { if day == now.Weekday() { found = true break } } if !found { continue } ev := &calendar.Event{ Summary: class.Summary, Start: &calendar.EventDateTime{ DateTime: fmt.Sprintf("%sT%s:00", dateStr, class.Start), TimeZone: "US/Pacific", }, End: &calendar.EventDateTime{ DateTime: fmt.Sprintf("%sT%s:00", dateStr, class.End), TimeZone: "US/Pacific", }, Attendees: []*calendar.EventAttendee{}, } attendees := class.Students if len(attendees) == 0 { attendees = students } for _, student := range attendees { ev.Attendees = append( ev.Attendees, &calendar.EventAttendee{ Email: student, }, ) } if class.Zoom != "" { ev.Description = fmt.Sprintf(`Zoom: %s`, class.Zoom) } _, err := srv.Events.Insert(myCal.Id, ev).Do() if err != nil { log.Fatal(err) } } /* ev := &calendar.Event{ Summary: "Test event", Start: &calendar.EventDateTime{ DateTime: "2020-09-09T16:00:00", TimeZone: "US/Pacific", }, End: &calendar.EventDateTime{ DateTime: "2020-09-09T17:00:00", TimeZone: "US/Pacific", }, Attendees: []*calendar.EventAttendee{ &calendar.EventAttendee{ Email: "oliver.park@heliosns.org", }, }, } res, err := srv.Events.Insert(myCal.Id, ev).Do() if err != nil { log.Fatal(err) } log.Printf("%#v", res) */ /* t := time.Now().Format(time.RFC3339) events, err := srv.Events.List(myCal.Id).ShowDeleted(false). SingleEvents(true).TimeMin(t).MaxResults(250).OrderBy("startTime").Do() if err != nil { log.Fatal(err) } for _, item := range events.Items { date := item.Start.DateTime if date == "" { date = item.Start.Date } log.Printf("%v (%v)", item.Summary, date) } */ }