1 ;;; isbn.el --- utilities for generating bibtex entries from an ISBN -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2015 John Kitchin
5 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
6 ;; Keywords: convenience
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
29 ;; I found this on the web. It can be handy, but the bibtex entry has a lot of stuff in it.
31 (defun isbn-to-bibtex-lead (isbn)
32 "Search lead.to for ISBN bibtex entry.
33 You have to copy the entry if it is on the page to your bibtex
35 (interactive "sISBN: ")
38 "http://lead.to/amazon/en/?key=%s+&si=all&op=bt&bn=&so=sa&ht=us"
41 ;; Here we get isbn metadata and build a bibtex entry.
42 ;; http://xisbn.worldcat.org/xisbnadmin/doc/api.htm#getmetadata
44 (defun isbn-to-bibtex (isbn bibfile)
45 "Get bibtex entry for ISBN and insert it into BIBFILE unless an
46 entry with the generated key already exists in the file."
51 ;; now set initial input
53 ;; If region is active and it starts with a number, we use it
54 ((and (region-active-p)
55 (s-match "^[0-9]" (buffer-substring (region-beginning) (region-end))))
56 (buffer-substring (region-beginning) (region-end)))
57 ;; if first entry in kill ring starts with a number assume it is an isbn
58 ;; and use it as the guess
59 ((if (s-match "^[0-9]" (car kill-ring))
61 ;; type or paste it in
66 (append (f-entries "." (lambda (f) (f-ext? f "bib")))
67 org-ref-default-bibliography))))
69 (let* ((results (with-current-buffer
70 (url-retrieve-synchronously
72 "http://xisbn.worldcat.org/webservices/xid/isbn/%s?method=getMetadata&format=json&fl=*"
74 (json-read-from-string
75 (buffer-substring url-http-end-of-headers (point-max)))))
76 (status (cdr (nth 1 results)))
77 (metadata (aref (cdar results) 0))
81 ;; check if we got something
82 (unless (string= "ok" status)
83 (error "Status is %s" status))
85 ;; construct an alphabetically sorted bibtex entry. I assume ISBN numbers go
91 (cl-loop for field in (-sort 'string-lessp (mapcar 'car metadata))
93 (format " %s={%s}," field (cdr (assoc field metadata))))
97 ;; build entry in temp buffer to get the key so we can check for duplicates
98 (setq new-entry (with-temp-buffer
100 (org-ref-clean-bibtex-entry)
101 (setq new-key (bibtex-key-in-head))
104 (goto-char (point-min))
105 (when (search-forward new-key nil t)
107 (setq new-key (read-string
108 (format "%s already exists. Enter new key (C-g to cancel): " new-key)
110 (goto-char (point-max))
112 ;; set key. It is simplest to just replace it, even if it is the same.
113 (bibtex-beginning-of-entry)
114 (re-search-forward bibtex-entry-maybe-empty-head)
115 (if (match-beginning bibtex-key-in-head)
116 (delete-region (match-beginning bibtex-key-in-head)
117 (match-end bibtex-key-in-head)))
123 ;;; isbn.el ends here