Mercurial > code > home > repos > gcalendarwatch
annotate calsync/gcalclient/gcalclient.go @ 61:8aee4f5c4bdd
receive notifications and route them to calendar sync functions
author | drewp@bigasterisk.com |
---|---|
date | Fri, 06 Sep 2024 16:43:14 -0700 |
parents | 3b0595c2bf03 |
children | 0f09464d4974 |
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 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
3 /* |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
4 Note: this module keeps gcal *paging* private (we fetch all the pages), but |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
5 *sync* is public. At least, callers may receive a syncToken and have to pass it |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
6 back in. |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
7 */ |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
8 import ( |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
9 "context" |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
10 "crypto/md5" |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
11 "fmt" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
12 "log" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
13 "net/url" |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
14 "strings" |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
15 "time" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
16 |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
17 "bigasterisk.com/go/gcalendarwatch/mongoclienttypes" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
18 "google.golang.org/api/calendar/v3" |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
19 ) |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
20 |
54
3a12a3ac9164
refactor. run 30s forever. doesn't work on 2+ cals
drewp@bigasterisk.com
parents:
53
diff
changeset
|
21 const urlBase = "http://bigasterisk.com/calendar/" |
3a12a3ac9164
refactor. run 30s forever. doesn't work on 2+ cals
drewp@bigasterisk.com
parents:
53
diff
changeset
|
22 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
23 type GCalClient struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
24 ctx context.Context |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
25 srv *calendar.Service |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
26 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
27 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
28 // 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
|
29 type CalendarEvent struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
30 *calendar.Event |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
31 CalendarUrl string |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
32 EventUrl string |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
33 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
34 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
35 func MakeCalUrl(calId string) string { |
54
3a12a3ac9164
refactor. run 30s forever. doesn't work on 2+ cals
drewp@bigasterisk.com
parents:
53
diff
changeset
|
36 return urlBase + url.QueryEscape(calId) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
37 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
38 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
39 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
|
40 return calUrl + "/" + url.QueryEscape(evId) |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
41 } |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
42 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
43 func New(ctx context.Context) (*GCalClient, error) { |
58 | 44 srv, err := newService(ctx) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
45 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
46 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
|
47 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
48 return &GCalClient{ctx, srv}, nil |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
49 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
50 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
51 func (gc *GCalClient) Close() { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
52 // todo: disconnect watches if possible |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
53 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
54 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
55 func (gc *GCalClient) AllCalendars() ([]*calendar.CalendarListEntry, error) { |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
56 // todo: pagination |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
57 list, err := gc.srv.CalendarList.List().MaxResults( /*maxResults*/ 100).Do() |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
58 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
59 return nil, err |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
60 } |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
61 |
58 | 62 // todo: do not submit |
60 | 63 // debugFilterCals(list) |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
64 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
65 return list.Items, nil |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
66 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
67 |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
68 func debugFilterCals(list *calendar.CalendarList) { |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
69 log.Println("filtering cal list") |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
70 ret := make([]*calendar.CalendarListEntry, 0) |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
71 for _, cal := range list.Items { |
60 | 72 if strings.Contains(cal.Id, "drewp") || strings.Contains(cal.Id, "ast") { |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
73 ret = append(ret, cal) |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
74 } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
75 } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
76 list.Items = ret |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
77 } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
78 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
79 func shortDebugHash(pageToken string) string { |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
80 if pageToken == "" { |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
81 return "(empty)" |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
82 } |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
83 return fmt.Sprintf("%x", md5.Sum([]byte(pageToken))) |
52 | 84 } |
61
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
85 |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
86 func (gc *GCalClient) WatchEvents(cal *mongoclienttypes.MongoCal, watchId string, expire time.Duration) error { |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
87 _, err := watchEventsCall(gc.srv, cal.GoogleId, expire, watchId).Do() |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
88 return err |
8aee4f5c4bdd
receive notifications and route them to calendar sync functions
drewp@bigasterisk.com
parents:
60
diff
changeset
|
89 } |