aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-calibre.el
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-06-17 17:20:29 +1000
committerYuchen Pei <id@ypei.org>2023-06-17 17:20:29 +1000
commit093ffa5fbf7143f4668bb0a3dc9659a5cc836e12 (patch)
tree1ed4e14b2a43b8e338f4ad6a04d969b99b9239be /emacs/.emacs.d/lisp/my/my-calibre.el
parentabc686827ae38ee715d9eed1c5c29161c74127e6 (diff)
Moving things one level deeper
To ease gnu stow usage. Now we can do stow -t ~ emacs
Diffstat (limited to 'emacs/.emacs.d/lisp/my/my-calibre.el')
-rw-r--r--emacs/.emacs.d/lisp/my/my-calibre.el83
1 files changed, 83 insertions, 0 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-calibre.el b/emacs/.emacs.d/lisp/my/my-calibre.el
new file mode 100644
index 0000000..e12028b
--- /dev/null
+++ b/emacs/.emacs.d/lisp/my/my-calibre.el
@@ -0,0 +1,83 @@
+;;; my-calibre.el -- Calibre client -*- lexical-binding: t -*-
+
+;; Copyright (C) 2023 Free Software Foundation.
+
+;; Author: Yuchen Pei <id@ypei.org>
+;; Package-Requires: ((emacs "28.2"))
+
+;; This file is part of dotfiles.
+
+;; dotfiles is free software: you can redistribute it and/or modify it
+;; under the terms of the GNU Affero General Public License as
+;; published by the Free Software Foundation, either version 3 of the
+;; License, or (at your option) any later version.
+
+;; dotfiles 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
+;; Affero General Public License for more details.
+
+;; You should have received a copy of the GNU Affero General Public
+;; License along with dotfiles. If not, see
+;; <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Calibre client.
+
+;;; Code:
+
+
+(defun org-attach-calibre-book (id)
+ "Attach a calibre book with ID to the current org entry."
+ (interactive "sCalibre book id: ")
+ (let ((export-dir (org-attach-dir t)
+ ))
+ (call-process-shell-command
+ (format "mkdir -p %s && calibredb export --dont-asciiize \\
+ --replace-whitespace --single-dir --to-dir %s %s" export-dir export-dir id)
+ nil "*calibredb*")
+ (org-attach-sync)))
+
+;; the following should be adapted to a capture so that one can fix anything
+;; erroneous before refiling and attaching
+;; fixme: the following should be decoupled from org maybe
+(defun create-calibre-book-node (id)
+ (interactive "sCalibre book ID: ")
+ ;; 1. get book metadata from calibredb
+ ;; 1.1. run calibredb to get metadata
+ (ignore-errors (kill-buffer "*calibredb*"))
+ (if (= 0
+ (call-process-shell-command
+ (concat "calibredb show_metadata " id) nil "*calibredb*"))
+ ;; 1.2. parse the metadata to get author, title and year
+ (let ((book-info (my-parse-colon-separated-output "*calibredb*")))
+ ;; 2. create the node and attach it under books and papers
+ ;; 2.1 create a new node
+ (kill-buffer "*calibredb*")
+ (org-capture nil "book")
+ (insert (format
+ "%s - %s - [%s]"
+ (let ((full-author (alist-get "Authors" book-info nil nil 'string=)))
+ ;; (pp book-info)
+ (substring full-author
+ (1+ (string-match "\\[.*\\]" full-author))
+ (1- (match-end 0))))
+ (alist-get "Title" book-info nil nil 'string=)
+ (substring (alist-get "Published" book-info nil nil 'string=) 0 10)))
+ ;; 2.2 use org-entry-put to add all the properties
+ (dolist (pair book-info)
+ (org-entry-put (point-min) (car pair) (cdr pair)))
+ (attach-calibre-book id))
+ (error (format "Cannot find book %s!" id))))
+
+(defun my-calibredb-search (query)
+ (interactive "scalibredb search query: ")
+ (ignore-errors (kill-buffer "*calibredb*"))
+ (call-process-shell-command
+ (format "calibredb search %s | xargs -d, -i sh -c 'echo ID: {} && calibredb show_metadata {}'" query)
+ nil "*calibredb*")
+ (switch-to-buffer "*calibredb*"))
+
+(provide 'my-calibre)
+;;; my-calibre.el ends here