-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreadwise-lib.el
More file actions
172 lines (148 loc) · 7.75 KB
/
readwise-lib.el
File metadata and controls
172 lines (148 loc) · 7.75 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
;;; readwise-lib.el --- Library for interacting with Readwise API -*- lexical-binding: t; -*-
;; Author: CountGreven
;; URL: https://github.com/CountGreven/readwise-lib
;; Version: 0.1
;; Package-Requires: ((emacs "24.3") (request "0.3.2"))
;; Keywords: tools, convenience
;; 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 library provides functions to interact with the Readwise API.
;;; Code:
(require 'request)
(require 'json)
(require 'auth-source)
(defvar readwise-api-base-url "https://readwise.io/api/"
"The base URL for the Readwise API.")
(defcustom readwise-debug-level 0
"Debug level for the Readwise library.
0 - No debug output.
1 - Basic debug output.
2 - Detailed debug output."
:group 'readwise
:type 'integer)
(defun readwise-debug (level msg &rest args)
"Print debug message MSG at debug LEVEL with ARGS."
(when (>= readwise-debug-level level)
(apply 'message (concat "[Readwise] " msg) args)))
(defun readwise-get-access-token ()
"Get the access token for Readwise from auth-source."
(let ((found (nth 0 (auth-source-search :host "readwise.io"))))
(if found
(let ((secret (plist-get found :secret)))
(if (functionp secret)
(funcall secret)
secret))
(error "Readwise access token not found in auth-source"))))
(defun readwise-request (endpoint &optional method data success-callback error-callback)
"Make a request to the Readwise API.
ENDPOINT is the API endpoint (e.g., \"/books\").
METHOD is the HTTP method (e.g., \"GET\", \"POST\").
DATA is an optional data payload for POST/PUT requests.
SUCCESS-CALLBACK is a function to call on success.
ERROR-CALLBACK is a function to call on error."
(let ((url (concat readwise-api-base-url endpoint))
(token (readwise-get-access-token))
(method (or method "GET")))
(readwise-debug 1 "Making %s request to %s" method url)
(request url
:type method
:headers `(("Authorization" . ,(concat "Token " token))
("Content-Type" . "application/json"))
:data (when data (json-encode data))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(readwise-debug 2 "Success: %S" data)
(when success-callback
(funcall success-callback data))))
:error (cl-function
(lambda (&key error-thrown &allow-other-keys)
(readwise-debug 1 "Error: %S" error-thrown)
(when error-callback
(funcall error-callback error-thrown)))))))
(defun readwise-create-book (title &optional author success-callback error-callback)
"Create a new book in Readwise with TITLE and optional AUTHOR.
SUCCESS-CALLBACK is called on success.
ERROR-CALLBACK is called on error."
(let ((data `(("title" . ,title)
("author" . ,author))))
(readwise-debug 1 "Creating book with title: %s, author: %s" title author)
(readwise-request "/v2/books" "POST" data success-callback error-callback)))
(defun readwise-update-book (book-id title &optional author success-callback error-callback)
"Update an existing book in Readwise with BOOK-ID, TITLE, and optional AUTHOR.
SUCCESS-CALLBACK is called on success.
ERROR-CALLBACK is called on error."
(let ((data `(("title" . ,title)
("author" . ,author))))
(readwise-debug 1 "Updating book ID: %s with title: %s, author: %s" book-id title author)
(readwise-request (format "/v2/books/%s" book-id) "PUT" data success-callback error-callback)))
(defun readwise-delete-book (book-id success-callback error-callback)
"Delete a book in Readwise with BOOK-ID.
SUCCESS-CALLBACK is called on success.
ERROR-CALLBACK is called on error."
(readwise-debug 1 "Deleting book ID: %s" book-id)
(readwise-request (format "/books/%s" book-id) "DELETE" nil success-callback error-callback))
(defun readwise-create-highlight (book-id text &optional note tags success-callback error-callback)
"Create a new highlight in Readwise with BOOK-ID, TEXT, and optional NOTE and TAGS.
SUCCESS-CALLBACK is called on success.
ERROR-CALLBACK is called on error."
(let ((data `(("book_id" . ,book-id)
("text" . ,text)
("note" . ,note)
("tags" . ,tags))))
(readwise-debug 1 "Creating highlight in book ID: %s with text: %s" book-id text)
(readwise-request "/v2/highlights" "POST" data success-callback error-callback)))
(defun readwise-update-highlight (highlight-id text &optional note tags success-callback error-callback)
"Update an existing highlight in Readwise with HIGHLIGHT-ID, TEXT, and optional NOTE and TAGS.
SUCCESS-CALLBACK is called on success.
ERROR-CALLBACK is called on error."
(let ((data `(("text" . ,text)
("note" . ,note)
("tags" . ,tags))))
(readwise-debug 1 "Updating highlight ID: %s with text: %s" highlight-id text)
(readwise-request (format "/v2/highlights/%s" highlight-id) "PUT" data success-callback error-callback)))
(defun readwise-delete-highlight (highlight-id success-callback error-callback)
"Delete a highlight in Readwise with HIGHLIGHT-ID.
SUCCESS-CALLBACK is called on success.
ERROR-CALLBACK is called on error."
(readwise-debug 1 "Deleting highlight ID: %s" highlight-id)
(readwise-request (format "/v2/highlights/%s" highlight-id) "DELETE" nil success-callback error-callback))
(defun readwise-get-highlights (&optional cursor updated-after success-callback error-callback)
"Get highlights from Readwise.
Optional CURSOR and UPDATED-AFTER parameters for pagination and filtering.
SUCCESS-CALLBACK is called on success.
ERROR-CALLBACK is called on error."
(let ((endpoint "/v2/export"))
(when cursor
(setq endpoint (concat endpoint "?pageCursor=" cursor)))
(when updated-after
(setq endpoint (concat endpoint (if cursor "&" "?") "updatedAfter=" (url-hexify-string updated-after))))
(readwise-debug 1 "Fetching highlights with cursor: %s, updated-after: %s" cursor updated-after)
(readwise-request endpoint "GET" nil success-callback error-callback)))
(defun readwise-v3-create-document (title source-url &optional summary notes tags success-callback error-callback)
"Create a new document in Readwise API v3 with TITLE, SOURCE-URL, and optional SUMMARY, NOTES, and TAGS."
(let ((data `(("title" . ,title)
("source_url" . ,source-url)
("summary" . ,summary)
("notes" . ,notes)
("tags" . ,tags))))
(readwise-request "/v3/documents" "POST" data success-callback error-callback)))
(defun readwise-v3-list-documents (&optional page-cursor updated-after success-callback error-callback)
"Fetch the list of documents from Readwise API v3. Handle pagination with PAGE-CURSOR and filter with UPDATED-AFTER."
(let ((endpoint (concat "/v3/list/"
(when page-cursor (concat "?pageCursor=" page-cursor))
(when updated-after
(concat (if page-cursor "&" "?")
"updated_after=" (url-hexify-string updated-after))))))
(readwise-request endpoint "GET" nil success-callback error-callback)))
(provide 'readwise-lib)
;;; readwise-lib.el ends here