-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalendar.go
308 lines (262 loc) · 6.66 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package speech
import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"time"
"github.com/tiechui1994/tool/util"
"golang.org/x/oauth2"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
const (
timeFormat = "2006-01-02T15:04:05+08:00"
ZoneShanghai = "Asia/Shanghai"
)
func getClient(token *oauth2.Token) *http.Client {
config := &oauth2.Config{}
return config.Client(context.Background(), token)
}
type EventDateTime struct {
DateTime time.Time `json:"dateTime,omitempty"`
TimeZone string `json:"timeZone,omitempty"`
}
type Recurrence []string
type Request struct {
EventID string `json:"eventID"`
URL string `json:"url"`
Method string `json:"method"`
Headers map[string]string `json:"headers"`
}
type Event struct {
TimeAt *time.Time
TimeZone string
Title string
Recurrence Recurrence
Body json.RawMessage
Request Request
}
type EventOption interface {
apply() []string
}
type eventOption struct {
f func() []string
}
func (o *eventOption) apply() []string {
return o.f()
}
func newEventOption(f func() []string) *eventOption {
return &eventOption{f: f}
}
type Cron struct {
Minute []int
Hour []int
}
// UNTIL=20110701T170000Z
// BYHOUR=
// BYMINUTE=
// BYSECOND=
//
// INTERVAL=2, 每间隔的时长
// COUNT=10, 总共10次
// WKST=MO, 每一天
func withCron(cron Cron, callback func() string) *eventOption {
return newEventOption(func() []string {
if len(cron.Hour) == 0 {
cron.Hour = []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}
}
var result []string
for _, hour := range cron.Hour {
for _, min := range cron.Minute {
v := fmt.Sprintf("RRULE:FREQ=DAILY;BYHOUR=%v;BYMINUTE=%v;%v", hour, min, callback())
result = append(result, v)
}
}
return result
})
}
func WithEmpty() Recurrence {
return newEventOption(func() []string {
return []string{}
}).apply()
}
func WithCron(c Cron, interval ...int) Recurrence {
interval = append(interval, 1)
return withCron(c, func() string {
return fmt.Sprintf("COUNT=1;INTERVAL=%v", interval[0])
}).apply()
}
func WithCount(c Cron, count int, interval ...int) Recurrence {
interval = append(interval, 1)
return withCron(c, func() string {
return fmt.Sprintf("COUNT=%v;INTERVAL=%v", count, interval[0])
}).apply()
}
func WithUntil(c Cron, until time.Time, interval ...int) Recurrence {
interval = append(interval, 1)
return withCron(c, func() string {
return fmt.Sprintf("UNTIL=%v;INTERVAL=%v", until.Format("20060102T150405Z"), interval[0])
}).apply()
}
func WithForever(c Cron, interval ...int) Recurrence {
interval = append(interval, 1)
return withCron(c, func() string {
return fmt.Sprintf("WKST=MO;INTERVAL=%v", interval[0])
}).apply()
}
func DeleteEvents(token oauth2.Token, start, end string) error {
service, err := calendar.NewService(context.Background(),
option.WithHTTPClient(getClient(&token)))
if err != nil {
return err
}
calendarId := "primary"
list, err := service.Events.List(calendarId).
TimeMin(start).TimeMax(end).TimeZone(ZoneShanghai).MaxResults(512).Do()
if err != nil {
return err
}
for _, item := range list.Items {
if len(item.Recurrence) == 0 {
_ = delEvent(token, []string{item.Id})
continue
}
instances, err := service.Events.Instances(calendarId, item.Id).
TimeMin(start).TimeMax(end).TimeZone(ZoneShanghai).MaxResults(512).Do()
if err != nil {
continue
}
eventIdList := make([]string, 0, len(instances.Items))
for _, it := range instances.Items {
eventIdList = append(eventIdList, it.Id)
}
_ = delEvent(token, eventIdList)
}
return err
}
func DeleteEvent(token oauth2.Token, eventID string) error {
service, err := calendar.NewService(context.Background(),
option.WithHTTPClient(getClient(&token)))
if err != nil {
return err
}
calendarId := "primary"
instances, err := service.Events.Instances(calendarId, eventID).
MaxResults(512).Do()
if err != nil {
return err
}
if len(instances.Items) == 0 {
return delEvent(token, []string{eventID})
}
zone, err := time.LoadLocation(ZoneShanghai)
if err != nil {
return err
}
eventIdList := make([]string, 0)
now := time.Now().In(zone).Format(timeFormat)
for _, item := range instances.Items {
if item.Start.DateTime < now {
eventIdList = append(eventIdList, item.Id)
}
}
return delEvent(token, eventIdList)
}
func delEvent(token oauth2.Token, eventIdList []string) error {
service, err := calendar.NewService(context.Background(),
option.WithHTTPClient(getClient(&token)))
if err != nil {
return err
}
calendarId := "primary"
if len(eventIdList) == 1 {
return service.Events.Delete(calendarId, eventIdList[0]).Do()
}
in := make(chan string, 2)
wg := sync.WaitGroup{}
go func() {
defer close(in)
for _, eventId := range eventIdList {
in <- eventId
}
}()
for i := 0; i < 2; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
eventId, ok := <-in
if !ok {
return
}
err = service.Events.Delete(calendarId, eventId).Do()
if err != nil {
log.Printf("DEL failed: %v", err)
}
}
}()
}
wg.Wait()
return nil
}
func InsertEvent(event Event, token oauth2.Token) (err error) {
if event.TimeAt == nil {
return fmt.Errorf("attr TimeAt must be set")
}
service, err := calendar.NewService(context.Background(),
option.WithHTTPClient(getClient(&token)))
if err != nil {
return err
}
if event.TimeZone == "" {
event.TimeZone = ZoneShanghai
}
if event.Title == "" {
event.Title = "Event_" + time.Now().Format("0102150405")
}
uid := hex.EncodeToString(util.MD5(fmt.Sprintf("%v", time.Now().UnixNano())))
event.Request.EventID = uid
request, _ := json.Marshal(event.Request)
zone, err := time.LoadLocation(event.TimeZone)
if err != nil {
return err
}
at := event.TimeAt.In(zone)
ev := &calendar.Event{
Id: uid,
Summary: event.Title,
Description: base64.StdEncoding.EncodeToString(event.Body),
Start: &calendar.EventDateTime{
DateTime: at.Format(timeFormat),
TimeZone: event.TimeZone,
},
End: &calendar.EventDateTime{
DateTime: at.Add(time.Minute).Format(timeFormat),
TimeZone: event.TimeZone,
},
Recurrence: event.Recurrence,
Location: base64.StdEncoding.EncodeToString(request),
Reminders: &calendar.EventReminders{
Overrides: []*calendar.EventReminder{
{
Method: "email",
Minutes: 0,
ForceSendFields: []string{"Minutes"},
},
},
UseDefault: false,
ForceSendFields: []string{"UseDefault"},
},
}
calendarId := "primary"
ev, err = service.Events.Insert(calendarId, ev).Do()
if err != nil {
return err
}
return nil
}