-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.go
75 lines (58 loc) · 1.54 KB
/
calendar.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
package main
import (
"google.golang.org/api/calendar/v3"
"log"
"time"
"net/http"
)
const username = "[email protected]"
type EventInformation struct {
date string
title string
description string
location string
}
func insertEventToCalendar(service *calendar.Service, information EventInformation) {
eventMap := getEventListInCalendar(service)
event := &calendar.Event{
Summary:information.title,
Location:information.location,
Description:information.description,
Start:&calendar.EventDateTime{DateTime:information.date},
End:&calendar.EventDateTime{DateTime:information.date},
}
isFuture := isFutureDate(information.date)
if isFuture {
for id, date := range eventMap {
if date == information.date {
service.Events.Update(username, id, event).Do()
return
}
}
service.Events.Insert(username, event).Do()
}
}
func getEventListInCalendar(service *calendar.Service) map[string]string {
t := time.Now().Format(time.RFC3339)
events, err := service.Events.List("primary").ShowDeleted(false).
SingleEvents(true).TimeMin(t).Do()
whenMap := make(map[string]string)
if err != nil {
log.Fatalf("Unable to retrieve next ten of the user's events. %v", err)
}
if len(events.Items) > 0 {
for _, i := range events.Items {
var when string
if i.Start.DateTime != "" {
when = i.Start.DateTime
} else {
when = i.Start.Date
}
whenMap[i.Id] = when
}
}
return whenMap
}
func createCalendarService(client *http.Client) (*calendar.Service, error) {
return calendar.New(client)
}