branch: externals/calibre
commit 9b5434664fb5cde3594b2e5cf5885459dd84b558
Author: Kjartan Oli Agustsson <[email protected]>
Commit: Kjartan Oli Agustsson <[email protected]>

    Add function to fetch book summary
    
    * calibre-cli.el (calibre-cli--get-summary): New function.
    * calibre-core.el (calibre-book-summary): New function.
    * calibre-db.el (calibre-db--get-summary): New function.
    * calibre-util.el (calibre-and=>): New function.
---
 calibre-cli.el  | 20 ++++++++++++++++++++
 calibre-core.el |  4 ++++
 calibre-db.el   | 12 ++++++++++--
 calibre-util.el |  6 ++++++
 4 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/calibre-cli.el b/calibre-cli.el
index c074519acc..85e9f3fb9a 100644
--- a/calibre-cli.el
+++ b/calibre-cli.el
@@ -55,6 +55,26 @@ specified the default is \\='all\\='."
   "Return all books in the Calibre library `calibre-library-dir'."
   (mapcar #'calibre-cli--make-book (calibre-cli--list)))
 
+(defun calibre-cli--get-summary (book)
+  "Return the summary of BOOK."
+  (calibre-and=>
+   (with-temp-buffer
+     (apply #'call-process
+            `(,calibre-calibredb-executable
+              nil
+              t
+              nil
+              "--with-library"
+              ,(calibre--library)
+              "list"
+              "--search" ,(format "id:%d" (calibre-book-id book))
+              "--fields" "comments"
+              "--for-machine"))
+     (goto-char (point-min))
+     (json-parse-buffer :object-type 'alist :array-type 'list))
+   (lambda (res)
+     (calibre-and=> (assq 'comments (car res)) #'cdr))))
+
 (defun calibre-cli--make-book (json)
   "Make a `calibre-book' from JSON."
   (let-alist json
diff --git a/calibre-core.el b/calibre-core.el
index 7c4df853e7..903f98bc44 100644
--- a/calibre-core.el
+++ b/calibre-core.el
@@ -45,6 +45,10 @@ If FORCE is non-nil the list is refreshed from the database."
     (setf calibre--books (calibre-core--interface get-books)))
   calibre--books)
 
+(defun calibre-book-summary (book)
+  "Return the summary of BOOK."
+  (calibre-core--interface get-summary book))
+
 (defvar calibre-library--filters nil)
 (defun calibre-library-clear-filters ()
   "Clear all active filters."
diff --git a/calibre-db.el b/calibre-db.el
index be567b010f..bf128f4bb7 100644
--- a/calibre-db.el
+++ b/calibre-db.el
@@ -167,7 +167,7 @@ FROM books
 LEFT JOIN books_series_link sl ON books.id = sl.book
 LEFT JOIN series ON sl.series = series.id;"))))
 
-(defmacro calibre-db--query (query arg fuzzy)
+(defmacro calibre-db--query (query arg &optional fuzzy)
   "Run QUERY on the Calibre database.
 
 ARG is a value to passed to a WHERE clause in QUERY.  FUZZY
@@ -181,10 +181,18 @@ WHERE column %s
 with column being the name of some column."
   `(flatten-list (sqlite-select (calibre--db)
                                 (format ,query (if ,fuzzy "LIKE ?" "= ?"))
-                                (if fuzzy-match
+                                (if ,fuzzy
                                     (vector (format "%%%s%%" ,arg))
                                   (vector ,arg)))))
 
+(defun calibre-db--get-summary (book)
+  "Return the summary of BOOK."
+  (calibre-and=>
+   (calibre-db--query
+    "SELECT text FROM comments WHERE book %s"
+    (calibre-book-id book))
+   #'car))
+
 (defmacro calibre-db--search-function (field docstring query)
   "Create a search function for FIELD with DOCSTRING as docstring.
 
diff --git a/calibre-util.el b/calibre-util.el
index c9aacfa3ea..259ade5cd0 100644
--- a/calibre-util.el
+++ b/calibre-util.el
@@ -26,6 +26,12 @@
 (require 'calibre)
 (require 'calibre-book)
 
+(defun calibre-and=> (value f)
+  "Return (F VALUE) when VALUE is non-nil, nil otherwise."
+  (if value
+      (funcall f value)
+    nil))
+
 (defmacro calibre-util--macro-map (macro list)
   "Apply MACRO to each element in LIST."
   `(progn

Reply via email to