Mercurial > code > home > repos > gcalendarwatch
annotate calsync/gcalclient/gcalclient.go @ 58:6c7151126a0b
logging and refactor
author | drewp@bigasterisk.com |
---|---|
date | Thu, 05 Sep 2024 17:01:23 -0700 |
parents | 24f662799710 |
children | 3b0595c2bf03 |
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" |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
15 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
16 "google.golang.org/api/calendar/v3" |
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 |
54
3a12a3ac9164
refactor. run 30s forever. doesn't work on 2+ cals
drewp@bigasterisk.com
parents:
53
diff
changeset
|
19 const urlBase = "http://bigasterisk.com/calendar/" |
3a12a3ac9164
refactor. run 30s forever. doesn't work on 2+ cals
drewp@bigasterisk.com
parents:
53
diff
changeset
|
20 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
21 type GCalClient struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
22 ctx context.Context |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
23 srv *calendar.Service |
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 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
26 // 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
|
27 type CalendarEvent struct { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
28 *calendar.Event |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
29 CalendarUrl string |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
30 EventUrl string |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
31 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
33 func MakeCalUrl(calId string) string { |
54
3a12a3ac9164
refactor. run 30s forever. doesn't work on 2+ cals
drewp@bigasterisk.com
parents:
53
diff
changeset
|
34 return urlBase + url.QueryEscape(calId) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
35 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
36 |
51
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
37 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
|
38 return calUrl + "/" + url.QueryEscape(evId) |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
39 } |
a9b720445bcf
now roughly syncs cals and events to mongodb, one time
drewp@bigasterisk.com
parents:
49
diff
changeset
|
40 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
41 func New(ctx context.Context) (*GCalClient, error) { |
58 | 42 srv, err := newService(ctx) |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
43 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
44 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
|
45 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
46 return &GCalClient{ctx, srv}, nil |
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 |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
49 func (gc *GCalClient) Close() { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
50 // todo: disconnect watches if possible |
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 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
53 func (gc *GCalClient) AllCalendars() ([]*calendar.CalendarListEntry, error) { |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
54 // todo: pagination |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
55 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
|
56 if err != nil { |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
57 return nil, err |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
58 } |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
59 |
58 | 60 // todo: do not submit |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
61 debugFilterCals(list) |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
62 |
49
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
63 return list.Items, nil |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
64 } |
2991c1166852
start calsync in go. Calendar list seems to sync
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
55
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 for _, cal := range list.Items { |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
70 if strings.Contains(cal.Id, "drewp") || strings.Contains(cal.Id, "east") { |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
71 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
|
72 } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
73 } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
74 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
|
75 } |
627c815f83bb
wip- discovring that i need to rewrite the concurrency between watchers (1 per cal)
drewp@bigasterisk.com
parents:
54
diff
changeset
|
76 |
56
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
77 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
|
78 if pageToken == "" { |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
79 return "(empty)" |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
80 } |
635ff76f867c
WIP: rewrite: process load+sync in parallel between cals; simplify a lot
drewp@bigasterisk.com
parents:
55
diff
changeset
|
81 return fmt.Sprintf("%x", md5.Sum([]byte(pageToken))) |
52 | 82 } |