aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-calibre.el
blob: e12028bb9a29973ccd0ee7347e05e9d53ea29098 (plain) (blame)
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
;;; 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