This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
where.go
157 lines (133 loc) · 3.82 KB
/
where.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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"net/http"
"strings"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/calendar/v3"
)
type zcache struct {
Location string
When time.Time
DefaultLocation string
}
// getClient uses a Context and Config to retrieve a Token
// then generate a Client. It returns the generated Client.
func getClient(ctx context.Context, config *jwt.Config) *http.Client {
c := config.Client(ctx)
return c
}
func GetTimeFromEventTime(e *calendar.EventDateTime) (time.Time, error) {
if e.DateTime != "" {
return time.Parse(time.RFC3339, e.DateTime)
} else {
return time.Parse("2006-01-02", e.Date)
}
}
type ServiceAccountKey struct {
ClientEmail string `json:"client_email,omitempty"`
}
func main() {
var cid string
var keyPath string
var defaultLocation string
var verbose bool
flag.StringVar(&cid, "calendar", "primary", "Zakir travel calendar ID")
flag.StringVar(&keyPath, "key", "where-is-zakir-key.json", "path to JSON key")
flag.StringVar(&defaultLocation, "default-location", "Unknown", "displays when nothing is listed on the calendar")
flag.BoolVar(&verbose, "verbose", false, "debug-level logging")
flag.Parse()
if verbose {
log.SetLevel(log.DebugLevel)
}
ctx := context.Background()
b, err := ioutil.ReadFile(keyPath)
if err != nil {
log.Fatalf("Unable to read client secret file: %s", err)
}
var k ServiceAccountKey
if err := json.Unmarshal(b, &k); err != nil {
log.Fatalf("Could not deserialize key json: %s", err)
}
config, err := google.JWTConfigFromJSON(b, calendar.CalendarReadonlyScope)
if err != nil {
log.Fatalf("unable to load jwt config: %v", err)
}
config.Subject = "[email protected]"
client := getClient(ctx, config)
srv, err := calendar.New(client)
if err != nil {
log.Fatalf("Unable to retrieve calendar Client %v", err)
}
cache := new(zcache)
cache.DefaultLocation = defaultLocation
handler := func(w http.ResponseWriter, r *http.Request) {
obj := struct {
Location string `json:"location"`
}{
Location: where(srv, cid, cache),
}
b, err := json.Marshal(obj)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
http.HandleFunc("/", handler)
http.HandleFunc("/where", handler)
http.ListenAndServe("localhost:9720", nil)
}
func where(srv *calendar.Service, cid string, cache *zcache) string {
n := time.Now()
expiry := cache.When.Add(time.Minute)
if n.Before(expiry) {
log.Debugf("using cache (expiry %d)", expiry)
return cache.Location
}
t := n.Format(time.RFC3339)
events, err := srv.Events.List(cid).ShowDeleted(false).
SingleEvents(true).TimeMin(t).MaxResults(10).OrderBy("startTime").Do()
current := ""
if err == nil && len(events.Items) > 0 {
log.Debugf("got %d events", len(events.Items))
for _, i := range events.Items {
log.Debugf("checking event \"%s\"", i.Summary)
start, err := GetTimeFromEventTime(i.Start)
if err != nil {
log.Fatalf("could not get start time: %v", err)
}
log.Debugf("start: %d", start)
end, err := GetTimeFromEventTime(i.End)
if err != nil {
log.Fatalf("could not get end time: %v", err)
}
log.Debugf("end: %d", end)
parts := strings.Split(i.Summary, "(")
name := parts[0]
name = strings.Trim(name, " ")
if start.Before(n) && end.After(n) {
log.Debug("using event as current!")
current = name
}
}
} else if err != nil {
log.Errorf("error talking to calendar: %s", err)
} else {
log.Debug("no events received")
}
if current == "" {
log.Debugf("couldn't find event, using default %s", cache.DefaultLocation)
current = cache.DefaultLocation
}
cache.Location = current
cache.When = n
return current
}