-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathorg-readwise.el
More file actions
475 lines (422 loc) · 23.1 KB
/
org-readwise.el
File metadata and controls
475 lines (422 loc) · 23.1 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
;;; org-readwise.el --- Sync Readwise highlights with Org-mode -*- lexical-binding: t; -*-
;; Author: CountGreven
;; URL: https://github.com/CountGreven/org-readwise
;; Version: 0.2
;; Package-Requires: ((emacs "24.3") (request "0.3.2") (org "9.1"))
;; Keywords: tools, convenience, outlines, hypermedia
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package integrates Readwise highlight syncing with Org-mode.
;; It provides commands to fetch highlights from Readwise and insert
;; them into an Org buffer or a specified file.
;;; Code:
(require 'org)
(require 'auth-source)
(require 'url)
(require 'request)
(require 'json)
(require 'readwise-lib)
(defvar org-readwise--last-cursor nil
"Tracks the last cursor received from the Readwise API during pagination.")
(defgroup org-readwise ()
"Integrate the Readwise.io highlight syncing service with `org-mode`."
:group 'files
:prefix "org-readwise-"
:link '(info-link "(org-readwise) Top"))
(defcustom org-readwise-last-sync-time-file "~/.emacs.d/org-readwise-last-sync"
"File to store the last sync time for org-readwise."
:group 'org-readwise
:type 'string)
(defcustom org-readwise-output-location 'buffer
"Specify where to output the Readwise highlights: 'buffer or a file path."
:group 'org-readwise
:type '(choice (const :tag "Buffer" buffer)
(file :tag "File")))
(defcustom org-readwise-sync-highlights t
"Toggle whether to sync Readwise highlights."
:group 'org-readwise
:type 'boolean)
(defcustom org-readwise-sync-reader t
"Toggle whether to sync Readwise reader documents."
:group 'org-readwise
:type 'boolean)
(defcustom org-readwise-debug-level 0
"Debug level for the org-readwise package.
0 - No debug output.
1 - Basic debug output.
2 - Detailed debug output."
:group 'org-readwise
:type 'integer)
(defvar org-readwise-last-sync-time nil
"The timestamp of the last successful sync.")
(defun org-readwise-debug (level message &rest args)
"Log a debug message at LEVEL.
MESSAGE is the format string, and ARGS are the arguments for the format string."
(when (>= org-readwise-debug-level level)
(apply #'message (concat "[org-readwise] " message) args)))
(defun org-readwise--load-last-sync-time ()
"Load the last sync time from `org-readwise-last-sync-time-file`."
(when (file-exists-p org-readwise-last-sync-time-file)
(with-temp-buffer
(insert-file-contents org-readwise-last-sync-time-file)
(setq org-readwise-last-sync-time (buffer-string)))))
(defun org-readwise--save-last-sync-time (timestamp)
"Save the last sync time TIMESTAMP to `org-readwise-last-sync-time-file`."
(with-temp-file org-readwise-last-sync-time-file
(insert timestamp))
(setq org-readwise-last-sync-time timestamp))
(defun org-readwise--get-access-token ()
"Get the access token for Readwise.
This function expects the token to be present in one of the files defined in
`auth-sources`. If it is not present, it will prompt the user for their access
token. It returns a list containing the token and a save function."
(let ((found (nth 0 (auth-source-search :host "readwise.io"))))
(if found
(list (let ((secret (plist-get found :secret)))
(if (functionp secret)
(funcall secret)
secret))
(plist-get found :save-function))
nil)))
(defun org-readwise--get-highlights (&optional cursor updated-after)
"Get highlights from the Readwise API, handling pagination with CURSOR.
Include the UPDATED-AFTER parameter only in the initial request."
(let* ((token-header (list (cons "Authorization" (concat "Token " (nth 0 (org-readwise--get-access-token))))))
(url (concat readwise-api-base-url "v2/export"
(when cursor (concat "?pageCursor=" (format "%s" cursor)))
(when updated-after (concat (if cursor "&" "?") "updatedAfter=" (url-hexify-string updated-after))))))
(org-readwise-debug 1 "Making request to: %s" url)
(request url
:headers token-header
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(org-readwise-debug 2 "Response Data: %S" data)
(let ((results (assoc-default 'results data))
(next-cursor (let ((nc (assoc-default 'nextPageCursor data)))
(if (numberp nc) (number-to-string nc) nc))))
(when results
(org-readwise--process-highlights results))
(if (and next-cursor (not (string-empty-p next-cursor))
(not (string= next-cursor org-readwise--last-cursor)))
(progn
(setq org-readwise--last-cursor next-cursor)
(org-readwise--get-highlights next-cursor updated-after))
(org-readwise-debug 1 "No more pages to fetch or cursor repeated, stopping pagination.")))))
:error (cl-function
(lambda (&key response data &allow-other-keys)
(let* ((status-code (request-response-status-code response))
(retry-after (assoc-default 'Retry-After (request-response-headers response)))
(detail (assoc-default 'detail data))
(retry-seconds (if retry-after
(string-to-number retry-after)
(if (and detail (string-match "in \\([0-9]+\\) seconds" detail))
(string-to-number (match-string 1 detail))
30)))) ;; Default to 30 seconds if no information is available
(if (eq status-code 429)
(progn
(org-readwise-debug 1 "Rate limit hit, retrying after %s seconds" retry-seconds)
(run-at-time retry-seconds nil #'org-readwise--get-highlights cursor updated-after))
(org-readwise-debug 1 "Error Response: %S" response)
(message "Error fetching highlights: %S" status-code))))))))
(defun org-readwise--insert-org-heading (level title id &optional author url body tags buffer)
"Insert an org-mode heading.
LEVEL is the heading level.
TITLE is the heading title.
ID is the ID property.
AUTHOR is the optional author property.
URL is the optional URL property.
BODY is the optional body content.
TAGS are optional tags for the heading.
BUFFER is the buffer or file to insert the heading into."
(with-current-buffer (or buffer (current-buffer))
(insert (format "%s %s" (make-string level ?*) title))
(when (and tags (not (string-empty-p tags)))
(insert (format " :%s:" tags)))
(insert "\n :PROPERTIES:\n :ID: " id "\n")
(when author
(setq author (replace-regexp-in-string "\n" " " author)) ; Replace newlines with spaces
(insert (format " :AUTHOR: %s\n" author)))
(when url
(insert (format " :URL: %s\n" url)))
(insert " :END:\n")
(when body
(insert (format " %s\n\n" body)))))
(defun org-readwise--process-highlight (highlight buffer)
"Process a single HIGHLIGHT and insert it into BUFFER."
(let ((highlight-id (number-to-string (assoc-default 'id highlight)))
(text (assoc-default 'text highlight))
(note (assoc-default 'note highlight)))
(org-readwise--insert-org-heading 2 "Highlight" highlight-id nil nil text nil buffer)
(when (and note (not (string-empty-p note)))
(org-readwise--insert-org-heading 3 "Note" (concat highlight-id "-note") nil nil note nil buffer))))
(defun org-readwise--process-book (book buffer)
"Process a single BOOK and insert its details into BUFFER."
(let* ((book-id (number-to-string (assoc-default 'user_book_id book))) ;; Ensure book ID is a string
(title (or (assoc-default 'title book) "No Title"))
(author (assoc-default 'author book))
(tags (mapconcat (lambda (tag) (assoc-default 'name tag))
(assoc-default 'book_tags book) ":"))
(summary (assoc-default 'summary book))
(source-url (assoc-default 'source_url book)))
(org-readwise--insert-org-heading 1 title book-id author source-url summary tags buffer)
(let ((highlights (assoc-default 'highlights book)))
(when (vectorp highlights)
(setq highlights (append highlights nil)))
(dolist (highlight highlights)
(org-readwise--process-highlight highlight buffer)))))
(defun org-readwise--process-highlights (results)
"Process highlights data and print it in a structured way."
(org-readwise-debug 1 "Processing %d books" (length results))
(org-readwise-debug 2 "Results type: %s" (type-of results))
(when (or (listp results) (vectorp results))
(setq results (append results nil)) ; Convert vector to list if necessary
(let* ((output-buffer (when (eq org-readwise-output-location 'buffer)
(get-buffer-create "*Readwise Highlights*")))
(output-file (when (and (stringp org-readwise-output-location)
(not (eq org-readwise-output-location 'buffer)))
org-readwise-output-location)))
(org-readwise-debug 2 "Output buffer: %s" output-buffer)
(org-readwise-debug 2 "Output file: %s" output-file)
(with-current-buffer (or output-buffer (find-file-noselect output-file))
(goto-char (point-max))
(dolist (book results)
(org-readwise-debug 1 "Processing book: %s (ID: %s)" (or (assoc-default 'title book) "No Title") (assoc-default 'user_book_id book))
(org-readwise--process-book book (current-buffer))
(org-readwise-debug 2 "Finished processing book: %s (ID: %s)" (or (assoc-default 'title book) "No Title") (assoc-default 'user_book_id book))))
(when output-buffer
(with-current-buffer output-buffer
(org-mode)
(goto-char (point-min))
(display-buffer (current-buffer)))))))
(defun org-readwise--get-documents (&optional cursor updated-after)
"Get documents from the Readwise API v3, handling pagination with CURSOR.
Include the UPDATED-AFTER parameter only in the initial request."
(let* ((token-header (list (cons "Authorization" (concat "Token " (nth 0 (org-readwise--get-access-token))))))
(url (concat "https://readwise.io/api/v3/list/"
(when cursor (concat "?pageCursor=" cursor))
(when updated-after (concat (if cursor "&" "?") "updatedAfter=" (url-hexify-string updated-after))))))
(org-readwise-debug 1 "Making request to: %s" url)
(request url
:headers token-header
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(org-readwise-debug 2 "Response Data: %S" data)
(let ((results (assoc-default 'results data))
(next-cursor (assoc-default 'nextPageCursor data)))
(when results
(org-readwise--process-documents results (get-buffer-create "*Readwise Highlights*")))
(if (and next-cursor (not (string-empty-p next-cursor))
(not (string= next-cursor org-readwise--last-cursor)))
(progn
(setq org-readwise--last-cursor next-cursor)
(org-readwise--get-documents next-cursor updated-after))
(org-readwise-debug 1 "No more pages to fetch or cursor repeated, stopping pagination.")))))
:error (cl-function
(lambda (&key response data &allow-other-keys)
(let* ((status-code (request-response-status-code response))
(retry-after (assoc-default 'Retry-After (request-response-headers response)))
(detail (assoc-default 'detail data))
(retry-seconds (if retry-after
(string-to-number retry-after)
(if (string-match "in \\([0-9]+\\) seconds" detail)
(string-to-number (match-string 1 detail))
30)))) ;; Default to 30 seconds if no information is available
(if (eq status-code 429)
(progn
(org-readwise-debug 1 "Rate limit hit, retrying after %s seconds" retry-seconds)
(run-at-time retry-seconds nil #'org-readwise--get-documents cursor updated-after))
(org-readwise-debug 1 "Error Response: %S" response)
(message "Error fetching documents: %S" status-code))))))))
(defun org-readwise--process-document (document buffer &optional parent-id)
"Process a single DOCUMENT and insert its details into BUFFER.
If PARENT-ID is provided, insert the document as a subheading under the parent."
(let* ((doc-id (assoc-default 'id document))
(title (or (assoc-default 'title document) "No Title"))
(author (assoc-default 'author document))
(summary (assoc-default 'summary document))
(notes (assoc-default 'notes document))
(source-url (assoc-default 'source_url document))
(tags (assoc-default 'tags document)))
(org-readwise-debug 2 "Inserting document with ID: %s, Title: %s" doc-id title)
;; Convert tags to a string if necessary and ensure it's a list of strings
(when (and tags (listp tags))
(org-readwise-debug 1 "Processing tags for document ID: %s" doc-id)
(setq tags (mapconcat (lambda (tag)
(if (stringp tag)
tag
(assoc-default 'name tag))) ;; Ensure we only get the string 'name'
tags ":")))
(org-readwise--insert-org-heading (if parent-id 2 1) title doc-id author source-url summary tags buffer)
;; Only insert notes if they are not null and not empty
(when (and notes (not (string-empty-p notes)))
(org-readwise--insert-org-heading (if parent-id 3 2) "Notes" (concat doc-id "-notes") nil nil notes nil buffer))))
(defun org-readwise--process-documents (documents buffer)
"Process documents data and insert them into Org mode, handling parent-child relationships."
;; Convert documents to a list if it's a vector
(when (vectorp documents)
(org-readwise-debug 2 "Converting documents vector to list")
(setq documents (append documents nil))) ;; Convert vector to list
;; Ensure it's a list before proceeding
(when (listp documents)
(org-readwise-debug 1 "Processing %d documents" (length documents))
(let ((document-hash (make-hash-table :test 'equal))
(processed-ids (make-hash-table :test 'equal))) ;; Keep track of processed documents
;; First, populate the hash table with all documents by their IDs
(dolist (document documents)
(let ((doc-id (assoc-default 'id document)))
(org-readwise-debug 2 "Adding document to hash with ID: %s" doc-id)
(puthash doc-id document document-hash)))
;; Now process each document
(dolist (document documents)
(let ((doc-id (assoc-default 'id document))
(parent-id (assoc-default 'parent_id document)))
(unless (gethash doc-id processed-ids) ;; Skip if document is already processed
(org-readwise-debug 1 "Processing document with ID: %s" doc-id)
(if parent-id
(let ((parent-document (gethash parent-id document-hash)))
(if (and parent-document (not (gethash parent-id processed-ids)))
(progn
(org-readwise-debug 2 "Found parent document with ID: %s for document: %s" parent-id doc-id)
(org-readwise--process-document document buffer parent-id))
(org-readwise-debug 1 "Parent document with ID: %s not found for document: %s" parent-id doc-id)))
(org-readwise--process-document document buffer))
;; Mark as processed and log the action
(org-readwise-debug 1 "Document with ID: %s has been processed" doc-id)
(puthash doc-id t processed-ids)))))))
(defun string-empty-p (str)
"Check whether STR, coerced to a string, is empty."
(string= (format "%s" str) ""))
(defun org-readwise--get-next-page-cursors (&optional cursor updated-after)
"Get nextPageCursor from the Readwise API v3, handling pagination with CURSOR."
(let* ((token-header (list (cons "Authorization" (concat "Token " (nth 0 (org-readwise--get-access-token))))))
(url (concat "https://readwise.io/api/v3/list/"
(when cursor (concat "?pageCursor=" cursor))
(when updated-after (concat (if cursor "&" "?") "updatedAfter=" (url-hexify-string updated-after))))))
(request url
:headers token-header
:parser 'json-read
:success
(cl-function
(lambda (&key data &allow-other-keys)
(let ((next-cursor (assoc-default 'nextPageCursor data)))
;; Print nextPageCursor at debug level 1
(when next-cursor
(org-readwise-debug 1 "Next Page Cursor: %s" next-cursor))
;; The rest of the processing at debug level 2
(org-readwise-debug 2 "Response Data: %S" data)
(when (and next-cursor (not (string= next-cursor cursor))) ;; Avoid looping on the same cursor
(org-readwise--get-next-page-cursors next-cursor updated-after)))))
:error (cl-function
(lambda (&key response &allow-other-keys)
(message "Error fetching documents: %S" (request-response-status-code response)))))
:status-code '((401 . (lambda (&rest _) (message "Unauthorized"))))))
(defun org-readwise-sync-cursors (&optional all)
"Print only nextPageCursor from Readwise during pagination."
(interactive "P")
(setq org-readwise--last-cursor nil) ;; Reset the cursor before sync
(org-readwise--load-last-sync-time)
(let ((updated-after (unless all org-readwise-last-sync-time)))
(org-readwise--get-next-page-cursors nil updated-after)))
(defun org-readwise-sync (&optional all)
"Synchronize highlights and documents from Readwise and insert them into an Org buffer or file.
If ALL is non-nil (when called with a universal argument), pull all highlights and documents."
(interactive "P")
;; Explicitly initialize output-buffer and output-file to nil
(let* ((output-buffer nil)
(output-file nil))
;; Log initialization
(org-readwise-debug 1 "Initializing output-buffer and output-file as nil.")
;; Set output-buffer or output-file based on user preferences
(setq output-buffer (if (eq org-readwise-output-location 'buffer)
(get-buffer-create "*Readwise Highlights*")
nil))
(setq output-file (if (and (stringp org-readwise-output-location)
(not (eq org-readwise-output-location 'buffer)))
org-readwise-output-location
nil))
;; Log post-setup values
(org-readwise-debug 1 "After initialization: output-buffer: %s, output-file: %s"
(if output-buffer "set" "nil")
(if output-file "set" "nil"))
;; Check if neither output-buffer nor output-file is set
(unless (or output-buffer output-file)
(org-readwise-debug 1 "Neither output-buffer nor output-file is set. Exiting sync.")
(message "No output buffer or file specified. Exiting sync.")
(return-from org-readwise-sync))
;; Continue with sync logic
(when output-buffer
(with-current-buffer output-buffer
(erase-buffer)
(org-readwise-debug 1 "Erased output-buffer.")))
(setq org-readwise--last-cursor nil) ;; Reset the cursor before sync
(org-readwise--load-last-sync-time)
(let ((updated-after (unless all org-readwise-last-sync-time)))
;; Sync highlights from v2 if enabled
(when org-readwise-sync-highlights
(org-readwise-debug 1 "Syncing highlights...")
(org-readwise--get-highlights nil updated-after))
;; Sync documents from v3 if enabled
(when org-readwise-sync-reader
(org-readwise-debug 1 "Syncing documents...")
(org-readwise--get-documents nil updated-after))
;; Save the last sync time
(org-readwise--save-last-sync-time (format-time-string "%Y-%m-%dT%H:%M:%S%z"))))
;; Write to the file if output-file is provided
(when output-file
(org-readwise-debug 1 "Writing output to file: %s" output-file)
(with-temp-file output-file
(when output-buffer ;; Ensure output-buffer exists
(insert-buffer-substring output-buffer))))
;; Check for output-buffer being displayed
(when output-buffer
(with-current-buffer output-buffer
(org-mode)
(goto-char (point-min))
(org-readwise-debug 1 "Displaying output-buffer.")
(display-buffer (current-buffer)))))
(defun org-readwise-sync (&optional all)
"Synchronize highlights and documents from Readwise and insert them into an Org buffer or file.
If ALL is non-nil (when called with a universal argument), pull all highlights and documents."
(interactive "P")
(let* ((output-buffer (when (eq org-readwise-output-location 'buffer)
(get-buffer-create "*Readwise Highlights*")))
(output-file (when (and (stringp org-readwise-output-location)
(not (eq org-readwise-output-location 'buffer)))
org-readwise-output-location)))
(when output-buffer
(with-current-buffer output-buffer
(erase-buffer)))
(setq org-readwise--last-cursor nil) ;; Reset the cursor before sync
(org-readwise--load-last-sync-time)
(let ((updated-after (unless all org-readwise-last-sync-time)))
;; Sync highlights from v2 if enabled
(when org-readwise-sync-highlights
(org-readwise--get-highlights nil updated-after))
;; Sync documents from v3 if enabled
(when org-readwise-sync-reader
(org-readwise--get-documents nil updated-after))
;; Save the last sync time
(org-readwise--save-last-sync-time (format-time-string "%Y-%m-%dT%H:%M:%S%z"))))
;; Writing to file if output-file is non-nil
(when (and (boundp 'output-file) output-file)
(org-readwise-debug 1 "Writing output to file: %s" output-file)
(with-temp-file output-file
(when output-buffer
(insert-buffer-substring output-buffer))))
(when (and (boundp 'output-buffer) output-buffer)
(with-current-buffer output-buffer
(org-mode)
(goto-char (point-min))
(display-buffer (current-buffer)))))
(provide 'org-readwise)