blob: 183dfbd5a725abace89094ecb46556ba7ea6575b (
plain) (
blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
|
;;; ~/.doom.d/org-gcal.el -*- lexical-binding: t; -*-
(require 'aio)
(require 'parse-time)
(setq-local lexical-binding t)
(setq plstore-cache-passphrase-for-symmetric-encryption t)
(defvar gcal-client-id)
(defvar gcal-client-secret)
(defvar google-calendar-readonly-scope
"https://www.googleapis.com/auth/calendar.readonly")
(defvar events-file "/home/grfn/notes/events.org")
(defun google--get-token (scope client-id client-secret)
(oauth2-auth-and-store
"https://accounts.google.com/o/oauth2/v2/auth"
"https://oauth2.googleapis.com/token"
scope
client-id
client-secret))
(cl-defun google--request (url &key method params scope)
(let ((p (aio-promise))
(auth-token (google--get-token scope gcal-client-id gcal-client-secret)))
(oauth2-url-retrieve
auth-token
url
(lambda (&rest _)
(goto-char (point-min))
(re-search-forward "^$")
(let ((resp (json-parse-buffer :object-type 'alist)))
(aio-resolve p (lambda () resp))))
nil
(or method "GET")
params)
p))
(cl-defun list-events (&key min-time max-time)
(google--request
(concat
"https://www.googleapis.com/calendar/v3/calendars/griffin@urbint.com/events"
"?timeMin=" (format-time-string "%Y-%m-%dT%T%z" min-time)
"&timeMax=" (format-time-string "%Y-%m-%dT%T%z" max-time))
:scope google-calendar-readonly-scope))
(defun last-week-events ()
(list-events :min-time (time-subtract
(current-time)
(seconds-to-time
(* 60 60 24 7)))
:max-time (current-time)))
(defun next-week-events ()
(list-events :min-time (current-time)
:max-time (time-add
(current-time)
(seconds-to-time
(* 60 60 24 7)))))
(defun attending-event? (event)
(let* ((attendees (append (alist-get 'attendees event) nil))
(self (--find (alist-get 'self it) attendees)))
(equal "accepted" (alist-get 'responseStatus self))))
(defun event->org-headline (event level)
(cl-flet ((make-time
(key)
(when-let ((raw-time (->> event (alist-get key) (alist-get 'dateTime))))
(format-time-string
(org-time-stamp-format t)
(parse-iso8601-time-string raw-time)))))
(if-let ((start-time (make-time 'start))
(end-time (make-time 'end)))
(s-format
"${headline} [[${htmlLink}][${summary}]] :event:
${startTime}--${endTime}
:PROPERTIES:
${location-prop}
:EVENT: ${htmlLink}
:END:
${description}"
(function
(lambda (k m)
(or (alist-get (intern k) m)
(format "key not found: %s" k))))
(append
event
`((headline . ,(make-string level ?*))
(startTime . ,start-time)
(endTime . ,end-time)
(location-prop
. ,(if-let ((location (alist-get 'location event)))
(s-lex-format ":LOCATION: ${location}")
"")))))
"")))
(comment
(alist-get 'foo nil)
)
(defun write-events (events)
(with-current-buffer (find-file-noselect events-file)
(save-mark-and-excursion
(save-restriction
(widen)
(erase-buffer)
(goto-char (point-min))
(insert "#+TITLE: Events")
(newline) (newline)
(prog1
(loop for event in (append events nil)
when (attending-event? event)
do
(insert (event->org-headline event 1))
(newline)
sum 1)
(org-align-tags t))))))
(defun +grfn/sync-events ()
(interactive)
(let* ((events (alist-get 'items (aio-wait-for (next-week-events))))
(num-written (write-events events)))
(message "Successfully wrote %d events" num-written)))
(comment
((kind . "calendar#event")
(etag . "\"3174776941020000\"")
(id . "SNIP")
(status . "confirmed")
(htmlLink . "https://www.google.com/calendar/event?eid=SNIP")
(created . "2020-04-01T13:30:09.000Z")
(updated . "2020-04-20T13:14:30.510Z")
(summary . "SNIP")
(description . "SNIP")
(location . "SNIP")
(creator
(email . "griffin@urbint.com")
(self . t))
(organizer
(email . "griffin@urbint.com")
(self . t))
(start
(dateTime . "2020-04-01T12:00:00-04:00")
(timeZone . "America/New_York"))
(end
(dateTime . "2020-04-01T12:30:00-04:00")
(timeZone . "America/New_York"))
(recurrence .
["RRULE:FREQ=WEEKLY;UNTIL=20200408T035959Z;BYDAY=WE"])
(iCalUID . "SNIP")
(sequence . 0)
(attendees .
[((email . "griffin@urbint.com")
(organizer . t)
(self . t)
(responseStatus . "accepted"))
((email . "SNIP")
(displayName . "SNIP")
(responseStatus . "needsAction"))])
(extendedProperties
(private
(origRecurringId . "309q48kc1dihsvbi13pnlimb5a"))
(shared
(origRecurringId . "309q48kc1dihsvbi13pnlimb5a")))
(reminders
(useDefault . t)))
(require 'icalendar)
(icalendar--convert-recurring-to-diary
nil
"RRULE:FREQ=WEEKLY;UNTIL=20200408T035959Z;BYDAY=WE"
)
)
|