-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticktick.el
311 lines (286 loc) · 12.4 KB
/
ticktick.el
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
(require 'oauth2)
(require 'json)
(require 'url)
(require 'seq)
(require 'tabulated-list)
(require 'org)
(defgroup ticktick nil
"Interface with TickTick API."
:prefix "ticktick-"
:group 'applications)
(defcustom ticktick-client-id ""
"TickTick client ID."
:type 'string
:group 'ticktick)
(defcustom ticktick-client-secret ""
"TickTick client secret."
:type 'string
:group 'ticktick)
(defcustom ticktick-auth-scope "tasks:write tasks:read"
"Space-separated scopes for TickTick API access."
:type 'string
:group 'ticktick)
(defcustom ticktick-token-file (concat user-emacs-directory "org-gcal/")
"Location of file storing token"
:type 'file
:group 'ticktick)
(defcustom ticktick-auto-fetch-new-tasks-from-server nil
"Whether ticktick.el should automatically fetch new
objects from the server."
:type 'boolean
:group 'ticktick)
(defcustom ticktick-sync-mode "down"
:type 'string
:group 'ticktick)
(defcustom ticktick-sync-files ("~/org/ticktick.org")
"Association list of file path(s) where tasks under a TickTick project should be
imported '(project-id file)."
:type 'file
:group 'ticktick)
(defcustom ticktick-)
(defcustom ticktick-files-alist nil
"Association list of '(project-id file). For each project-id, ‘ticktick-fetch’
and ‘ticktick-sync’ will retrieve new tasks and events and insert them into the
file."
:group 'ticktick
:type '(alist :key-type (string :tag "project-id") :value-type (file :tag "Org file")))
(defvar ticktick-token nil
"Access token for accessing the TickTick API."
:type 'string
:group 'ticktick)
(defvar ticktick-redirect-uri "http://localhost"
"The redirect URI registered with TickTick."
:type 'string
:group 'ticktick)
(defvar ticktick-sync-timer nil
"Timer object for automatic syncing.")
;;; Authorization functions (unchanged)
;;;###autoload
(defun ticktick-authorize ()
"Authorize tick.el with TickTick and obtain an access token."
(interactive)
(let* ((state (format "%06x" (random (expt 16 6))))
(auth-url (concat "https://ticktick.com/oauth/authorize?"
(url-build-query-string
`(("client_id" ,ticktick-client-id)
("response_type" "code")
("redirect_uri" ,ticktick-redirect-uri)
("scope" ,ticktick-auth-scopes)
("state" ,state)))))
(authorization (concat "Basic "
(base64-encode-string
(concat ticktick-client-id ":" ticktick-client-secret)
t)))
code token-response)
(browse-url auth-url)
(message "Please authorize the application in your browser and enter the authorization code (after '?code=').")
(setq code (read-string "Enter authorization code: "))
(setq token-response
(ticktick--exchange-code-for-token code authorization))
(if token-response
(progn
(setq ticktick-token token-response)
(message "Authorization successful!"))
(message "Failed to obtain access token."))))
(defun ticktick--exchange-code-for-token (code authorization)
"Exchange the authorization CODE for an access token using AUTHORIZATION header."
(let* ((token-url "https://ticktick.com/oauth/token")
(url-request-method "POST")
(url-request-extra-headers `(("Authorization" . ,authorization)
("Content-Type" . "application/x-www-form-urlencoded")))
(url-request-data (mapconcat
(lambda (kv)
(concat (url-hexify-string (car kv)) "=" (url-hexify-string (cdr kv))))
`(("grant_type" . "authorization_code")
("code" . ,code)
("redirect_uri" . ,ticktick-redirect-uri)
("scope" . ,ticktick-auth-scopes))
"&"))
response token-data)
(with-current-buffer (url-retrieve-synchronously token-url t t)
(goto-char url-http-end-of-headers)
(setq response (buffer-substring-no-properties (point) (point-max)))
(kill-buffer (current-buffer)))
(let ((json-object-type 'plist))
(setq token-data (json-read-from-string response)))
(if (plist-get token-data :access_token)
(progn
(plist-put token-data :created_at (float-time))
token-data)
nil)))
;;;###autoload
(defun ticktick-refresh-token ()
"Refresh the OAuth2 token."
(interactive)
(when ticktick-token
(let* ((token-url "https://ticktick.com/oauth/token")
(authorization (concat "Basic "
(base64-encode-string
(concat ticktick-client-id ":" ticktick-client-secret)
t)))
(url-request-method "POST")
(url-request-extra-headers `(("Authorization" . ,authorization)
("Content-Type" . "application/x-www-form-urlencoded")))
(refresh-token (plist-get ticktick-token :refresh_token))
(url-request-data (mapconcat
(lambda (kv)
(concat (url-hexify-string (car kv)) "=" (url-hexify-string (cdr kv))))
`(("grant_type" . "refresh_token")
("refresh_token" . ,refresh-token)
("redirect_uri" . ,ticktick-redirect-uri)
("scope" . ,ticktick-auth-scopes))
"&"))
response token-data)
(with-current-buffer (url-retrieve-synchronously token-url t t)
(goto-char url-http-end-of-headers)
(setq response (buffer-substring-no-properties (point) (point-max)))
(kill-buffer (current-buffer)))
(let ((json-object-type 'plist))
(setq token-data (json-read-from-string response)))
(if (plist-get token-data :access_token)
(progn
(plist-put token-data :created_at (float-time))
(setq ticktick-token token-data)
(message "Token refreshed!"))
(message "Failed to refresh token.")))))
(defun ticktick--ensure-token ()
"Ensure we have a valid access token."
(unless (and ticktick-token
(plist-get ticktick-token :access_token)
(not (ticktick--token-expired-p ticktick-token)))
(ticktick-refresh-token)))
(defun ticktick--token-expired-p (token)
"Check if TOKEN has expired."
(let ((expires-in (plist-get token :expires_in))
(created-at (plist-get token :created_at)))
(if (and expires-in created-at)
(> (float-time) (+ created-at expires-in -30))
nil)))
(defun ticktick-request (method endpoint &optional data)
"Send a request to the TickTick API.
METHOD is the HTTP method as a string.
ENDPOINT is the API endpoint.
DATA is an alist of data to send with the request."
(ticktick--ensure-token)
(condition-case err
(let* ((url-request-method method)
(url (concat "https://api.ticktick.com" endpoint))
(access-token (plist-get ticktick-token :access_token))
(url-request-extra-headers `(("Authorization" . ,(concat "Bearer " access-token))
("Content-Type" . "application/json")))
(url-request-data (and data (encode-coding-string (json-encode data) 'utf-8)))
response status)
(with-current-buffer (url-retrieve-synchronously url t t)
(goto-char url-http-end-of-headers)
(setq response (buffer-substring-no-properties (point) (point-max)))
(setq status url-http-response-status)
(kill-buffer (current-buffer)))
(cond
((and (>= status 200) (< status 300))
(if (string-blank-p response)
nil
(let ((json-object-type 'plist)
(json-array-type 'list))
(json-read-from-string response))))
((= status 401)
(ticktick-refresh-token)
(ticktick-request method endpoint data))
(t
(error "HTTP Error %s: %s" status response))))
(error
(message "Request failed: %s" (error-message-string err))
nil)))
(defun ticktick--task-to-heading (task)
"Convert a TickTick TASK to an org heading."
(let* ((id (plist-get task :id))
(title (plist-get task :title))
(content (plist-get task :content))
(status (plist-get task :status))
(due-date (plist-get task :dueDate))
(priority (plist-get task :priority)))
(concat "** " (if (= status 2) "DONE" "TODO")
(pcase priority
(5 " [#A]")
(3 " [#B]")
(1 " [#C]")
(_ ""))
" " title
(when due-date
(concat "\nDEADLINE: "
(format-time-string "<%Y-%m-%d %a>"
(date-to-time due-date))))
"\n:PROPERTIES:\n:TICKTICK_ID: " id "\n:END:\n"
(when content
(concat (replace-regexp-in-string "\\*" "\\\\*" content) "\n")))))
(defun ticktick--heading-to-task ()
"Convert org heading at point to a TickTick task."
(let* ((element (org-element-at-point))
(title (org-element-property :title element))
(todo-type (org-element-property :todo-type element))
(priority (org-element-property :priority element))
(deadline (org-element-property :deadline element))
(id (org-entry-get nil "TICKTICK_ID"))
(content (org-element-property :contents-begin element)))
`(("id" . ,id)
("title" . ,title)
("status" . ,(if (eq todo-type 'done) 2 0))
("priority" . ,(pcase priority
(?A 5)
(?B 3)
(?C 1)
(_ 0)))
("dueDate" . ,(when deadline
(format-time-string "%Y-%m-%dT%H:%M:%S+0000"
(org-timestamp-to-time deadline))))
("content" . ,(when content
(string-trim (buffer-substring-no-properties
content
(org-element-property :contents-end element))))))))
;;;###autoload
(defun ticktick-fetch ()
"Fetch all tasks from TickTick and update the sync file."
(interactive)
(let ((projects (ticktick-request "GET" "/open/v1/project")))
(with-current-buffer (find-file-noselect ticktick-sync-file)
(org-with-wide-buffer
(erase-buffer) ; Start fresh
(dolist (project projects)
(let* ((project-id (plist-get project :id))
(project-name (plist-get project :name))
(project-data (ticktick-request "GET" (format "/open/v1/project/%s/data" project-id)))
(tasks (plist-get project-data :tasks)))
;; Insert project heading
(insert (format "* %s\n:PROPERTIES:\n:TICKTICK_PROJECT_ID: %s\n:END:\n\n"
project-name project-id))
;; Insert all tasks under this project
(dolist (task tasks)
(insert (ticktick--task-to-heading task)))))
(save-buffer)))))
;;;###autoload
(defun ticktick-sync ()
"Sync tasks between local file and TickTick server."
(interactive)
(ticktick-fetch)
(with-current-buffer (find-file-noselect ticktick-sync-file)
(org-with-wide-buffer
(goto-char (point-min))
(while (outline-next-heading)
(unless (org-entry-get nil "TICKTICK_PROJECT_ID") ; Skip project headings
(when-let ((id (org-entry-get nil "TICKTICK_ID")))
(let* ((task (ticktick--heading-to-task))
(project-id (org-entry-get nil "TICKTICK_PROJECT_ID" t))) ; Get inherited property
(ticktick-request "POST" (format "/open/v1/task/%s" id)
(append task `(("projectId" . ,project-id)))))))))))
;;;###autoload
(defun ticktick-create-task ()
"Create a new TickTick task from the org heading at point."
(interactive)
(let* ((project-id (org-entry-get nil "TICKTICK_PROJECT_ID" t)) ; Get inherited property
(task (ticktick--heading-to-task))
(response (ticktick-request "POST" "/open/v1/task"
(append task `(("projectId" . ,project-id))))))
(when response
(org-set-property "TICKTICK_ID" (plist-get response :id))
(message "Task created successfully!"))))
(provide 'ticktick)
;;; ticktick.el ends here