-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
168 lines (129 loc) · 2.88 KB
/
google.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"
"time"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
var (
outputFmt = "15:04"
)
type Entry struct {
Start time.Time
End time.Time
Title string
Location string
AllDay bool
}
func (e Entry) String() string {
sb := strings.Builder{}
sb.WriteString(e.StartEnd())
sb.WriteString(fmt.Sprintf(" - %s", e.Title))
if e.Location != "" {
sb.WriteString(fmt.Sprintf(" (%s)", e.LocationString()))
}
return sb.String()
}
func (e Entry) StartEnd() string {
if e.AllDay {
return "all day"
}
return fmt.Sprintf("%s - %s", e.Start.Format(outputFmt), e.End.Format(outputFmt))
}
func (e Entry) LocationString() string {
if e.Location == "" {
return ""
}
return fmt.Sprintf("@ %s", e.Location)
}
type Google struct {
calendar *calendar.Service
timezone *time.Location
}
func NewGoogle(creds, tokens, tz string) (g Google, err error) {
g.timezone, err = time.LoadLocation(tz)
if err != nil {
return
}
ctx := context.Background()
b, err := os.ReadFile(creds)
if err != nil {
return
}
config, err := google.ConfigFromJSON(b, calendar.CalendarReadonlyScope)
if err != nil {
return
}
client, err := getClient(tokens, config)
if err != nil {
return
}
g.calendar, err = calendar.NewService(ctx, option.WithHTTPClient(client))
return
}
func (g Google) Today() (e []Entry, err error) {
bod := g.bod()
min := bod.Format(time.RFC3339)
max := bod.Add(24 * time.Hour).Format(time.RFC3339)
events, err := g.calendar.Events.List("primary").ShowDeleted(false).
SingleEvents(true).TimeMin(min).TimeMax(max).MaxResults(10).OrderBy("startTime").Do()
if err != nil {
return
}
e = make([]Entry, len(events.Items))
for idx, item := range events.Items {
e[idx] = Entry{
Title: item.Summary,
Location: item.Location,
}
switch item.Start.DateTime {
case "":
e[idx].AllDay = true
e[idx].Start, err = time.Parse("2006-01-02", item.Start.Date)
if err != nil {
return
}
default:
e[idx].Start, err = time.Parse(time.RFC3339, item.Start.DateTime)
if err != nil {
return
}
e[idx].End, err = time.Parse(time.RFC3339, item.End.DateTime)
if err != nil {
return
}
}
}
return
}
// Return Beginning of Day
func (g Google) bod() (t time.Time) {
t = time.Now().In(g.timezone)
t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, g.timezone)
return
}
func getClient(tokens string, config *oauth2.Config) (c *http.Client, err error) {
tok, err := tokenFromFile(tokens)
if err != nil {
return
}
c = config.Client(context.Background(), tok)
return
}
func tokenFromFile(file string) (tok *oauth2.Token, err error) {
f, err := os.Open(file)
if err != nil {
return
}
defer f.Close()
tok = &oauth2.Token{}
err = json.NewDecoder(f).Decode(tok)
return
}