Mercurial > code > home > repos > gcalendarwatch
annotate calsync/gcalclient/gcalclient.go @ 53:f248f018a663
refactor; dead code
author | drewp@bigasterisk.com |
---|---|
date | Mon, 19 Aug 2024 19:28:50 -0700 |
parents | 5f7c393577e9 |
children | 3a12a3ac9164 |
rev | line source |
---|---|
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
1 package gcalclient |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
2 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
3 import ( |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
4 "context" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
5 "log" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
6 "net/url" |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
7 "strings" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
8 "time" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
9 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
10 "bigasterisk.com/go/gcalendarwatch/mongoclient" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
11 "google.golang.org/api/calendar/v3" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
12 ) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
13 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
14 type GCalClient struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
15 ctx context.Context |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
16 srv *calendar.Service |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
17 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
18 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
19 // Same as calendar.Event, but includes our urls |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
20 type CalendarEvent struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
21 *calendar.Event |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
22 CalendarUrl string |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
23 EventUrl string |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
24 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
25 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
26 func MakeCalUrl(calId string) string { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
27 return "http://bigasterisk.com/calendar/" + url.QueryEscape(calId) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
28 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
29 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
30 func MakeEventUrl(calUrl string, evId string) string { |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
31 return calUrl + "/" + url.QueryEscape(evId) |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
32 } |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
33 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
34 func New(ctx context.Context) (*GCalClient, error) { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
35 err, srv := newService(ctx) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
36 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
37 log.Fatalf("Unable to retrieve Calendar client: %v", err) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
38 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
39 return &GCalClient{ctx, srv}, nil |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
40 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
41 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
42 func (gc *GCalClient) Close() { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
43 // todo: disconnect watches if possible |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
44 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
45 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
46 func (gc *GCalClient) AllCalendars(maxResults int64) ([]*calendar.CalendarListEntry, error) { |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
47 // todo: pagination |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
48 list, err := gc.srv.CalendarList.List().MaxResults(maxResults).Do() |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
49 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
50 return nil, err |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
51 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
52 list.Items = list.Items[:4] |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
53 return list.Items, nil |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
54 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
55 |
52 | 56 type FindEventsMessage struct { |
57 // either non-nil this: | |
58 Event *CalendarEvent | |
59 // or these: | |
60 CalId string | |
61 OlderThanThisIsDeletable time.Time | |
62 } | |
63 | |
64 // FindEvents considers all calendars. It runs forever. | |
65 func (gc *GCalClient) FindEvents( | |
66 mc *mongoclient.MongoClient, | |
67 // For each calendar, after events in this time range have been sent to | |
68 // `out`, the chan will get the other kind of FindEventsMessage (CalId, | |
69 // ...). That message signals that the caller may cull old events on the given | |
70 // calendar. After that point, all events will be updates (including | |
71 // deletes). | |
72 initialFillStart, initialFillEnd time.Time, | |
73 out chan *FindEventsMessage, | |
74 ) error { | |
75 | |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
76 cals, err := mc.GetAllCals() |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
77 if err != nil { |
52 | 78 return err |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
79 } |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
80 log.Println("reading", len(cals), "calendars") |
52 | 81 for calNum, cal := range cals { |
82 t := time.Now() | |
83 log.Println(" cal", calNum, cal.Url) | |
84 log.Println(" cal", calNum, "readEventsInRange", "from", initialFillStart, "to", initialFillEnd) | |
85 syncToken, err := gc.readEventsInRange(&cal, initialFillStart, initialFillEnd, out) | |
86 if err != nil { | |
87 return err | |
88 } | |
89 | |
90 out <- &FindEventsMessage{nil, cal.GoogleId, t} | |
91 | |
92 ew := gc.NewEventWatch(&cal, t, syncToken, out) | |
93 | |
94 for loop := 0; loop < 30; loop++ { | |
95 log.Println("") | |
96 log.Println("tail loop", loop, "for", cal.Url) | |
97 err := ew.GetMoreEvents() | |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
98 if err != nil { |
52 | 99 return err |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
100 } |
52 | 101 time.Sleep(2 * time.Second) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
102 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
103 } |
52 | 104 |
105 return nil | |
106 } | |
107 | |
108 // Synchronous. | |
109 func (gc *GCalClient) readEventsInRange( | |
110 cal *mongoclient.MongoCal, | |
111 initialFillStart, initialFillEnd time.Time, | |
112 out chan *FindEventsMessage, | |
113 ) (string, error) { | |
114 log.Println( | |
115 " get initial events for", cal.Url, "between", | |
116 initialFillStart, "and", initialFillEnd) | |
117 | |
118 pageToken := "" | |
119 syncToken := "" | |
120 | |
121 for { | |
122 log.Println(" getting another page", pageToken) | |
123 events, err := rangedEventsCall(gc.srv, cal.GoogleId, initialFillStart, initialFillEnd, pageToken).Do() | |
124 if err != nil { | |
125 return "", err | |
126 } | |
127 | |
128 log.Println(" got", len(events.Items), "events, sync=", events.NextSyncToken) | |
129 if len(events.Items) == 0 { | |
130 break | |
131 } | |
132 | |
133 sendEvents(events, cal, out) | |
134 | |
135 syncToken = events.NextSyncToken | |
136 if events.NextPageToken == "" { | |
137 break | |
138 } | |
139 pageToken = events.NextPageToken | |
140 } | |
141 return syncToken, nil | |
142 } | |
143 | |
144 // Send a page of calendar.Events over a channel, as CalendarEvent structs. | |
145 func sendEvents(events *calendar.Events, cal *mongoclient.MongoCal, out chan *FindEventsMessage) { | |
146 for _, event := range events.Items { | |
147 if event.Status == "cancelled" { | |
148 log.Fatal("todo") | |
149 } | |
150 out <- &FindEventsMessage{ | |
151 Event: &CalendarEvent{ | |
152 Event: event, | |
153 CalendarUrl: cal.Url, | |
154 EventUrl: MakeEventUrl(cal.Url, event.Id), | |
155 }} | |
156 } | |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
157 } |
52 | 158 |
159 type eventWatch struct { | |
160 gc *GCalClient | |
161 cal *mongoclient.MongoCal | |
162 nextSyncToken string | |
163 nextPageToken string | |
164 modSince time.Time | |
165 out chan *FindEventsMessage | |
166 } | |
167 | |
168 func (gc *GCalClient) NewEventWatch( | |
169 cal *mongoclient.MongoCal, | |
170 modSince time.Time, | |
171 syncToken string, | |
172 out chan *FindEventsMessage, | |
173 ) *eventWatch { | |
174 ew := &eventWatch{gc, cal, syncToken, "", modSince, out} | |
175 return ew | |
176 } | |
177 | |
178 // Call this when there are likely new changes to sync. | |
179 func (w *eventWatch) GetMoreEvents() error { | |
180 call := syncEventsCall(w.gc.srv, w.cal.GoogleId) | |
181 log.Println("listing events on", w.cal.GoogleId, "with") | |
182 | |
183 if w.nextPageToken != "" { | |
184 call = call.PageToken(w.nextPageToken) | |
185 log.Println(" pageToken", w.nextPageToken) | |
186 } else if w.nextSyncToken != "" { | |
187 call = call.SyncToken(w.nextSyncToken) | |
188 log.Println(" syncToken", w.nextSyncToken) | |
189 } else { | |
190 call = call.UpdatedMin((w.modSince.Format(time.RFC3339))) | |
191 log.Println(" updatedMin", w.modSince.Format(time.RFC3339)) | |
192 } | |
193 ret, err := call.Do() | |
194 if err != nil { | |
195 return err | |
196 } | |
197 w.nextSyncToken = ret.NextSyncToken | |
198 w.nextPageToken = ret.NextPageToken | |
199 log.Println(len(ret.Items), "more events received") | |
200 sendEvents(ret, w.cal, w.out) | |
201 log.Println("got nextSyncToken=", w.nextSyncToken) | |
202 log.Println("got nextPageToken=", w.nextPageToken) | |
203 return err | |
204 } |