1 ;;; doi-utils.el --- DOI utilities for making bibtex entries and downloading PDFs -*- 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/>.
23 ;; This package provides functionality to download PDFs and bibtex entries from a DOI, as well as to update a bibtex entry from a DOI. It depends slightly on org-ref, to determine where to save pdf files too, and where to insert bibtex entries in the default bibliography.
25 ;; The principle commands you will use from here are:
27 ;; - doi-utils-get-bibtex-entry-pdf with the cursor in a bibtex entry.
28 ;; - doi-utils-insert-bibtex-entry-from-doi to insert a bibtex entry at your cursor, clean it and try to get a pdf.
29 ;; - doi-utils-add-bibtex-entry-from-doi to add an entry to your default bibliography (cleaned with pdf if possible).
30 ;; - doi-utils-add-bibtex-entry-from-region to add an entry from a highlighed doi to your default bibliography.
31 ;; - doi-utils-update-bibtex-entry-from-doi with cursor in an entry to update its fields.
33 ;; Package-Requires: ((org-ref))
38 ;; * Getting pdf files from a DOI
39 ;; The idea here is simple. When you visit http://dx.doi.org/doi, you get redirected to the journal site. Once you have the url for the article, you can usually compute the url to the pdf, or find it in the page. Then you simply download it.
41 ;; There are some subtleties in doing this that are described here. To get the redirect, we have to use url-retrieve, and a callback function. The callback does not return anything, so we communicate through global variables. url-retrieve is asynchronous, so we have to make sure to wait for it to finish.
43 (defvar *doi-utils-waiting* t
44 "stores waiting state for url retrieval.")
46 (defvar *doi-utils-redirect* nil
47 "stores redirect url from a callback function")
49 (defun doi-utils-redirect-callback (&optional status)
50 "callback for url-retrieve to set the redirect"
51 (when (plist-get status :error)
52 (signal (car (plist-get status :error)) (cdr(plist-get status :error))))
53 (when (plist-get status :redirect) ; is nil if there none
54 (message "redirects = %s" (plist-get status :redirect))
55 (message "*doi-utils-redirect* set to %s"
56 (setq *doi-utils-redirect* (plist-get status :redirect))))
57 ;; we have done our job, so we are not waiting any more.
58 (setq *doi-utils-waiting* nil))
60 ;; To actually get the redirect we use url-retrieve like this.
62 (defun doi-utils-get-redirect (doi)
63 "get redirect url from dx.doi.org/doi"
64 ;; we are going to wait until the url-retrieve is done
65 (setq *doi-utils-waiting* t)
66 ;; start with no redirect. it will be set in the callback.
67 (setq *doi-utils-redirect* nil)
69 (format "http://dx.doi.org/%s" doi)
70 'doi-utils-redirect-callback)
71 ; I suspect we need to wait here for the asynchronous process to
72 ; finish. we loop and sleep until the callback says it is done via
73 ; `*doi-utils-waiting*'. this works as far as i can tell. Before I
74 ; had to run this a few times to get it to work, which i suspect
75 ; just gave the first one enough time to finish.
76 (while *doi-utils-waiting* (sleep-for 0.1)))
78 ;; Once we have a redirect for a particular doi, we need to compute the url to the pdf. We do this with a series of functions. Each function takes a single argument, the redirect url. If it knows how to compute the pdf url it does, and returns it. We store the functions in a variable:
80 (defvar doi-utils-pdf-url-functions nil
81 "list of functions that return a url to a pdf from a redirect url. Each function takes one argument, the redirect url. The function must return a pdf-url, or nil.")
86 (defun aps-pdf-url (*doi-utils-redirect*)
87 (when (string-match "^http://journals.aps.org" *doi-utils-redirect*)
88 (replace-regexp-in-string "/abstract/" "/pdf/" *doi-utils-redirect*)))
93 (defun science-pdf-url (*doi-utils-redirect*)
94 (when (string-match "^http://www.sciencemag.org" *doi-utils-redirect*)
95 (concat *doi-utils-redirect* ".full.pdf")))
100 (defun nature-pdf-url (*doi-utils-redirect*)
101 (when (string-match "^http://www.nature.com" *doi-utils-redirect*)
102 (let ((result *doi-utils-redirect*))
103 (setq result (replace-regexp-in-string "/full/" "/pdf/" result))
104 (replace-regexp-in-string "\.html$" "\.pdf" result))))
108 ;; http://onlinelibrary.wiley.com/doi/10.1002/anie.201402680/abstract
109 ;; http://onlinelibrary.wiley.com/doi/10.1002/anie.201402680/pdf
111 ;; It appears that it is not enough to use the pdf url above. That takes you to an html page. The actual link to teh pdf is embedded in that page. This is how ScienceDirect does things too.
113 ;; This is where the link is hidden:
115 ;; <iframe id="pdfDocument" src="http://onlinelibrary.wiley.com/store/10.1002/anie.201402680/asset/6397_ftp.pdf?v=1&t=hwut2142&s=d4bb3cd4ad20eb733836717f42346ffb34017831" width="100%" height="675px"></iframe>
118 (defun doi-utils-get-wiley-pdf-url (redirect-url)
119 "wileyscience direct hides the pdf url in html. we get it out here"
120 (setq *doi-utils-waiting* t)
121 (url-retrieve redirect-url
123 (beginning-of-buffer)
124 (re-search-forward "<iframe id=\"pdfDocument\" src=\"\\([^\"]*\\)\"" nil)
125 (setq *doi-utils-pdf-url* (match-string 1)
126 ,*doi-utils-waiting* nil)))
127 (while *doi-utils-waiting* (sleep-for 0.1))
130 (defun wiley-pdf-url (*doi-utils-redirect*)
131 (when (string-match "^http://onlinelibrary.wiley.com" *doi-utils-redirect*)
132 (doi-utils-get-wiley-pdf-url
133 (replace-regexp-in-string "/abstract" "/pdf" *doi-utils-redirect*))
134 *doi-utils-pdf-url*))
139 (defun springer-pdf-url (*doi-utils-redirect*)
140 (when (string-match "^http://link.springer.com" *doi-utils-redirect*)
141 (replace-regexp-in-string "/article/" "/content/pdf/" (concat *doi-utils-redirect* ".pdf"))))
145 ;; here is a typical url http://pubs.acs.org/doi/abs/10.1021/nl500037x
146 ;; the pdf is found at http://pubs.acs.org/doi/pdf/10.1021/nl500037x
148 ;; we just change /abs/ to /pdf/.
150 (defun acs-pdf-url (*doi-utils-redirect*)
151 (when (string-match "^http://pubs.acs.org" *doi-utils-redirect*)
152 (replace-regexp-in-string "/abs/" "/pdf/" *doi-utils-redirect*)))
157 (defun iop-pdf-url (*doi-utils-redirect*)
158 (when (string-match "^http://iopscience.iop.org" *doi-utils-redirect*)
159 (let ((tail (replace-regexp-in-string
160 "^http://iopscience.iop.org" "" *doi-utils-redirect*)))
161 (concat "http://iopscience.iop.org" tail
162 "/pdf" (replace-regexp-in-string "/" "_" tail) ".pdf"))))
167 (defun jstor-pdf-url (*doi-utils-redirect*)
168 (when (string-match "^http://www.jstor.org" *doi-utils-redirect*)
169 (concat (replace-regexp-in-string "/stable/" "/stable/pdfplus/" *doi-utils-redirect*) ".pdf")))
174 (defun aip-pdf-url (*doi-utils-redirect*)
175 (when (string-match "^http://scitation.aip.org" *doi-utils-redirect*)
176 ;; get stuff after content
178 (setq p2 (replace-regexp-in-string "^http://scitation.aip.org/" "" *doi-utils-redirect*))
179 (setq s (split-string p2 "/"))
180 (setq p1 (mapconcat 'identity (-remove-at-indices '(0 6) s) "/"))
181 (setq p3 (concat "/" (nth 0 s) (nth 1 s) "/" (nth 2 s) "/" (nth 3 s)))
182 (format "http://scitation.aip.org/deliver/fulltext/%s.pdf?itemId=/%s&mimeType=pdf&containerItemId=%s"
185 ;; ** Taylor and Francis
187 (defun tandfonline-pdf-url (*doi-utils-redirect*)
188 (when (string-match "^http://www.tandfonline.com" *doi-utils-redirect*)
189 (replace-regexp-in-string "/abs/\\|/full/" "/pdf/" *doi-utils-redirect*)))
193 (defun ecs-pdf-url (*doi-utils-redirect*)
194 (when (string-match "^http://jes.ecsdl.org" *doi-utils-redirect*)
195 (replace-regexp-in-string "\.abstract$" ".full.pdf" *doi-utils-redirect*)))
197 ;; http://ecst.ecsdl.org/content/25/2/2769
198 ;; http://ecst.ecsdl.org/content/25/2/2769.full.pdf
201 (defun ecst-pdf-url (*doi-utils-redirect*)
202 (when (string-match "^http://ecst.ecsdl.org" *doi-utils-redirect*)
203 (concat *doi-utils-redirect* ".full.pdf")))
209 (defun rsc-pdf-url (*doi-utils-redirect*)
210 (when (string-match "^http://pubs.rsc.org" *doi-utils-redirect*)
211 (let ((url (downcase *doi-utils-redirect*)))
212 (setq url (replace-regexp-in-string "articlelanding" "articlepdf" url))
215 ;; ** Elsevier/ScienceDirect
216 ;; You cannot compute these pdf links; they are embedded in the redirected pages.
218 (defvar *doi-utils-pdf-url* nil
219 "stores url to pdf download from a callback function")
221 (defun doi-utils-get-science-direct-pdf-url (redirect-url)
222 "science direct hides the pdf url in html. we get it out here"
223 (setq *doi-utils-waiting* t)
224 (url-retrieve redirect-url
226 (beginning-of-buffer)
227 (re-search-forward "pdfurl=\"\\([^\"]*\\)\"" nil t)
228 (setq *doi-utils-pdf-url* (match-string 1)
229 ,*doi-utils-waiting* nil)))
230 (while *doi-utils-waiting* (sleep-for 0.1))
234 (defun science-direct-pdf-url (*doi-utils-redirect*)
235 (when (string-match "^http://www.sciencedirect.com" *doi-utils-redirect*)
236 (doi-utils-get-science-direct-pdf-url *doi-utils-redirect*)
237 *doi-utils-pdf-url*))
240 ;; http://linkinghub.elsevier.com/retrieve/pii/S0927025609004558
241 ;; which actually redirect to
242 ;; http://www.sciencedirect.com/science/article/pii/S0927025609004558
243 (defun linkinghub-elsevier-pdf-url (*doi-utils-redirect*)
244 (when (string-match "^http://linkinghub.elsevier.com/retrieve" *doi-utils-redirect*)
245 (let ((second-redirect (replace-regexp-in-string
246 "http://linkinghub.elsevier.com/retrieve"
247 "http://www.sciencedirect.com/science/article"
248 ,*doi-utils-redirect*)))
249 (message "getting pdf url from %s" second-redirect)
250 *doi-utils-pdf-url*)))
253 ;; http://www.pnas.org/content/early/2014/05/08/1319030111
254 ;; http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf
256 ;; with supporting info
257 ;; http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf+html?with-ds=yes
259 (defun pnas-pdf-url (*doi-utils-redirect*)
260 (when (string-match "^http://www.pnas.org" *doi-utils-redirect*)
261 (concat *doi-utils-redirect* ".full.pdf?with-ds=yes")))
264 ;; ** Add all functions
266 (setq doi-utils-pdf-url-functions
277 'science-direct-pdf-url
278 'linkinghub-elsevier-pdf-url
285 ;; ** Get the pdf url for a doi
287 (defun doi-utils-get-pdf-url (doi)
288 "returns a url to a pdf for the doi if one can be
289 calculated. Loops through the functions in `doi-utils-pdf-url-functions'
291 (doi-utils-get-redirect doi)
293 (unless *doi-utils-redirect*
294 (error "No redirect found for %s" doi))
295 (message "applying functions")
297 (dolist (func doi-utils-pdf-url-functions)
298 (message "calling %s" func)
299 (let ((this-pdf-url (funcall func *doi-utils-redirect*)))
300 (message "t: %s" this-pdf-url)
302 (message "found pdf url: %s" this-pdf-url)
303 (throw 'pdf-url this-pdf-url))))))
305 ;; ** Finally, download the pdf
307 (defun doi-utils-get-bibtex-entry-pdf ()
308 "download pdf for entry at point if the pdf does not already
309 exist locally. The entry must have a doi. The pdf will be saved
310 to `org-ref-pdf-directory', by the name %s.pdf where %s is the
311 bibtex label. Files will not be overwritten. The pdf will be
312 checked to make sure it is a pdf, and not some html failure
313 page. you must have permission to access the pdf. We open the pdf
317 (bibtex-beginning-of-entry)
318 (let (;; get doi, removing http://dx.doi.org/ if it is there.
319 (doi (replace-regexp-in-string
320 "http://dx.doi.org/" ""
321 (bibtex-autokey-get-field "doi")))
326 ;; get the key and build pdf filename.
327 (re-search-forward bibtex-entry-maybe-empty-head)
328 (setq key (match-string bibtex-key-in-head))
329 (setq pdf-file (concat org-ref-pdf-directory key ".pdf"))
331 ;; now get file if needed.
332 (when (and doi (not (file-exists-p pdf-file)))
333 (setq pdf-url (doi-utils-get-pdf-url doi))
336 (url-copy-file pdf-url pdf-file)
337 ;; now check if we got a pdf
339 (insert-file-contents pdf-file)
340 ;; PDFS start with %PDF-1.x as the first few characters.
341 (if (not (string= (buffer-substring 1 6) "%PDF-"))
343 (message "%s" (buffer-string))
344 (delete-file pdf-file))
345 (message "%s saved" pdf-file)))
347 (when (file-exists-p pdf-file)
348 (org-open-file pdf-file)))
349 (message "No pdf-url found for %s at %s" doi *doi-utils-redirect* ))
352 ;; * Getting bibtex entries from a DOI
354 ;; I [[http://homepages.see.leeds.ac.uk/~eeaol/notes/2013/02/doi-metadata/][found]] you can download metadata about a DOI from http://dx.doi.org. You just have to construct the right http request to get it. Here is a function that gets the metadata as a plist in emacs.
356 (defun doi-utils-get-json-metadata (doi)
357 "Try to get json metadata for DOI. Open the DOI in a browser if we do not get it."
358 (let ((url-request-method "GET")
359 (url-mime-accept-string "application/citeproc+json")
360 (json-object-type 'plist)
363 (url-retrieve-synchronously
364 (concat "http://dx.doi.org/" doi))
365 (setq json-data (buffer-substring url-http-end-of-headers (point-max)))
366 (if (string-match "Resource not found" json-data)
368 (browse-url (concat "http://dx.doi.org/" doi))
369 (error "Resource not found. Opening website."))
370 (json-read-from-string json-data)))))
372 ;; We can use that data to construct a bibtex entry. We do that by defining a template, and filling it in. I wrote this template expansion code which makes it easy to substitute values like %{} in emacs lisp.
375 (defun doi-utils-expand-template (s)
376 "expand a template containing %{} with the eval of its contents"
377 (replace-regexp-in-string "%{\\([^}]+\\)}"
379 (let ((sexp (substring arg 2 -1)))
380 (format "%s" (eval (read sexp))))) s))
383 ;; Now we define a function that fills in that template from the metadata.
385 ;; As different bibtex types share common keys, it is advantageous to separate data extraction from json, and the formatting of the bibtex entry.
388 (setq doi-utils-json-metadata-extract
389 '((type (plist-get results :type))
390 (author (mapconcat (lambda (x) (concat (plist-get x :given) " " (plist-get x :family)))
391 (plist-get results :author) " and "))
392 (title (plist-get results :title))
393 (subtitle (plist-get results :subtitle))
394 (journal (plist-get results :container-title))
395 (series (plist-get results :container-title))
396 (publisher (plist-get results :publisher))
397 (volume (plist-get results :volume))
398 (issue (plist-get results :issue))
399 (number (plist-get results :issue))
400 (year (elt (elt (plist-get (plist-get results :issued) :date-parts) 0) 0))
401 (month (elt (elt (plist-get (plist-get results :issued) :date-parts) 0) 1))
402 (pages (plist-get results :page))
403 (doi (plist-get results :DOI))
404 (url (plist-get results :URL))
405 (booktitle (plist-get results :container-title))))
407 ;; Next, we need to define the different bibtex types. Each type has a bibtex type (for output) and the type as provided in the doi record. Finally, we have to declare the fields we want to output.
409 (setq doi-utils-bibtex-type-generators nil)
411 (defun doi-utils-concat-prepare (lst &optional acc)
412 "Given a list `lst' of strings and other expressions, which are
413 intented to passed to `concat', concat any subsequent strings,
414 minimising the number of arguments being passed to `concat'
415 without changing the results."
416 (cond ((null lst) (nreverse acc))
417 ((and (stringp (car lst))
419 (doi-utils-concat-prepare (cdr lst) (cons (concat (car acc) (car lst))
421 (t (doi-utils-concat-prepare (cdr lst) (cons (car lst) acc)))))
423 (defmacro doi-utils-def-bibtex-type (name matching-types &rest fields)
424 "Define a BibTeX type identified by (symbol) `name' with
425 `fields' (given as symbols), matching to retrieval expressions in
426 `doi-utils-json-metadata-extract'. This type will only be used
427 when the `:type' parameter in the JSON metadata is contained in
428 `matching-types' - a list of strings."
429 `(push (lambda (type results)
430 (when (or ,@(mapcar (lambda (match-type) `(string= type ,match-type)) matching-types))
431 (let ,(mapcar (lambda (field)
432 (let ((field-expr (assoc field doi-utils-json-metadata-extract)))
434 ;; need to convert to string first
435 `(,(car field-expr) (format "%s" ,(cadr field-expr)))
436 (error "unknown bibtex field type %s" field))))
439 ,@(doi-utils-concat-prepare
441 (list (concat "@" (symbol-name name) "{,\n")
442 ;; there seems to be some bug with mapcan,
443 ;; so we fall back to flatten
444 (mapcar (lambda (field)
445 `(" " ,(symbol-name field) " = {" ,field "},\n"))
448 doi-utils-bibtex-type-generators))
450 (doi-utils-def-bibtex-type article ("journal-article" "article-journal")
451 author title journal year volume number pages doi url)
453 (doi-utils-def-bibtex-type inproceedings ("proceedings-article")
454 author title booktitle year month pages doi url)
456 (doi-utils-def-bibtex-type book ("book")
457 author title series publisher year pages doi url)
459 (doi-utils-def-bibtex-type inbook ("book-chapter")
460 author title booktitle series publisher year pages doi url)
464 ;; With the code generating the bibtex entry in place, we can glue it to the json retrieval code.
466 (defun doi-utils-doi-to-bibtex-string (doi)
467 "return a bibtex entry as a string for the doi. Not all types are supported yet."
468 (let* ((results (doi-utils-get-json-metadata doi))
469 (type (plist-get results :type)))
470 ;(format "%s" results) ; json-data
471 (or (some (lambda (g) (funcall g type results)) doi-utils-bibtex-type-generators)
472 (message "%s not supported yet\n%S." type results))))
474 ;; That is just the string for the entry. To be useful, we need a function that inserts the string into a buffer. This function will insert the string at the cursor, clean the entry, try to get the pdf, and create a notes entry for you.
477 (defun doi-utils-insert-bibtex-entry-from-doi (doi)
478 "insert bibtex entry from a doi. Also cleans entry using
479 org-ref, and tries to download the corresponding pdf."
480 (interactive "sDOI :")
481 (insert (doi-utils-doi-to-bibtex-string doi))
483 ;; set date added for the record
484 (bibtex-set-field "DATE_ADDED" (current-time-string))
485 (if (bibtex-key-in-head nil)
486 (org-ref-clean-bibtex-entry t)
487 (org-ref-clean-bibtex-entry))
489 (doi-utils-get-bibtex-entry-pdf)
490 (save-selected-window
491 (org-ref-open-bibtex-notes)))
494 ;; It may be you are in some other place when you want to add a bibtex entry. This next function will open the first entry in org-ref-default-bibliography go to the end, and add the entry. You can sort it later.
497 (defun doi-utils-add-bibtex-entry-from-doi (doi bibfile)
498 "Add entry to end of a file in in the current directory ending
499 with .bib or in `org-ref-default-bibliography'. If you have an
500 active region that starts like a DOI, that will be the initial
501 prompt. If no region is selected and the first entry of the
502 kill-ring starts like a DOI, then that is the intial
503 prompt. Otherwise, you have to type or pste in a DOI."
505 (list (read-input "DOI: "
506 ;; now set initial input
508 ;; If region is active and it starts like a doi we want it.
509 ((and (region-active-p)
510 (s-match "^10" (buffer-substring
513 (buffer-substring (region-beginning) (region-end)))
514 ;; if the first entry in the kill-ring looks
515 ;; like a DOI, let's use it.
516 ((if (s-match "^10" (car kill-ring))
518 ;; otherwise, we have no initial input. You
519 ;; will have to type it in.
522 ;; now get the bibfile to add it to
525 (append (f-entries "." (lambda (f) (f-ext? f "bib")))
526 org-ref-default-bibliography))))
527 ;; Wrap in save-window-excursion to restore your window arrangement after this
529 (save-window-excursion
531 ;; Check if the doi already exists
532 (goto-char (point-min))
533 (if (search-forward doi nil t)
534 (message "%s is already in this file" doi)
537 (doi-utils-insert-bibtex-entry-from-doi doi)
541 ;; * Updating bibtex entries
542 ;; I wrote this code because it is pretty common for me to copy bibtex entries from ASAP articles that are incomplete, e.g. no page numbers because it is not in print yet. I wanted a convenient way to update an entry from its DOI. Basically, we get the metadata, and update the fields in the entry.
544 ;; There is not bibtex set field function, so I wrote this one.
547 (defun bibtex-set-field (field value &optional nodelim)
548 "set field to value in bibtex file. create field if it does not exist"
549 (interactive "sfield: \nsvalue: ")
550 (bibtex-beginning-of-entry)
552 (if (setq found (bibtex-search-forward-field field t))
555 (goto-char (car (cdr found)))
558 (bibtex-make-field field nil nil nodelim)
562 (message "new field being made")
563 (bibtex-beginning-of-entry)
564 (forward-line) (beginning-of-line)
565 (bibtex-next-field nil)
567 (bibtex-make-field field nil nil nodelim)
572 ;; The updating function for a whole entry looks like this. We get all the keys from the json plist metadata, and update the fields if they exist.
575 (defun plist-get-keys (plist)
576 "return keys in a plist"
578 for key in results by #'cddr collect key))
580 (defun doi-utils-update-bibtex-entry-from-doi (doi)
581 "update fields in a bibtex entry from the doi. Every field will be updated, so previous changes will be lost."
583 (or (replace-regexp-in-string "http://dx.doi.org/" "" (bibtex-autokey-get-field "doi"))
584 (read-string "DOI: "))))
585 (let* ((results (doi-utils-get-json-metadata doi))
586 (type (plist-get results :type))
588 (lambda (x) (concat (plist-get x :given)
589 " " (plist-get x :family)))
590 (plist-get results :author) " and "))
591 (title (plist-get results :title))
592 (journal (plist-get results :container-title))
597 (plist-get results :issued) :date-parts) 0) 0)))
598 (volume (plist-get results :volume))
599 (number (or (plist-get results :issue) ""))
600 (pages (or (plist-get results :page) ""))
601 (url (or (plist-get results :URL) ""))
602 (doi (plist-get results :DOI)))
604 ;; map the json fields to bibtex fields. The code each field is mapped to is evaluated.
605 (setq mapping '((:author . (bibtex-set-field "author" author))
606 (:title . (bibtex-set-field "title" title))
607 (:container-title . (bibtex-set-field "journal" journal))
608 (:issued . (bibtex-set-field "year" year))
609 (:volume . (bibtex-set-field "volume" volume))
610 (:issue . (bibtex-set-field "number" number))
611 (:page . (bibtex-set-field "pages" pages))
612 (:DOI . (bibtex-set-field "doi" doi))
613 (:URL . (bibtex-set-field "url" url))))
615 ;; now we have code to run for each entry. we map over them and evaluate the code
618 (eval (cdr (assoc key mapping))))
619 (plist-get-keys results)))
621 ; reclean entry, but keep key if it exists.
622 (if (bibtex-key-in-head)
623 (org-ref-clean-bibtex-entry t)
624 (org-ref-clean-bibtex-entry)))
627 ;; A downside to updating an entry is it overwrites what you have already fixed. So, we next develop a function to update the field at point.
630 (defun doi-utils-update-field ()
632 (let* ((doi (bibtex-autokey-get-field "doi"))
633 (results (doi-utils-get-json-metadata doi))
634 (field (car (bibtex-find-text-internal nil nil ","))))
636 ((string= field "volume")
637 (bibtex-set-field field (plist-get results :volume)))
638 ((string= field "number")
639 (bibtex-set-field field (plist-get results :issue)))
640 ((string= field "pages")
641 (bibtex-set-field field (plist-get results :page)))
642 ((string= field "year")
643 (bibtex-set-field field (plist-get results :year)))
645 (message "%s not supported yet." field)))))
649 ;; * DOI functions for WOS
650 ;; I came across this API http://wokinfo.com/media/pdf/OpenURL-guide.pdf to make links to the things I am interested in here. Based on that document, here are three links based on a doi:10.1021/jp047349j that take you to different Web Of Science (WOS) pages.
653 ;; 1. go to article in WOS: http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info:doi/10.1021/jp047349j
654 ;; 2. citing articles: http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F10.1021/jp047349j&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.citing=yes
655 ;; 3. related articles: http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F10.1021/jp047349j&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.related=yes
657 ;; These are pretty easy to construct, so we can write functions that will create them and open the url in our browser. There are some other options that could be considered, but since we usually have a doi, it seems like the best way to go for creating the links. Here are the functions.
659 (defun doi-utils-wos (doi)
660 "Open Web of Science entry for DOI"
661 (interactive "sDOI: ")
664 "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info:doi/%s" doi)))
666 (defun doi-utils-wos-citing (doi)
667 "Open Web of Science citing articles entry. May be empty if none are found"
668 (interactive "sDOI: ")
671 "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F"
673 "&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.citing=yes")))
675 (defun doi-utils-wos-related (doi)
676 "Open Web of Science related articles page."
677 (interactive "sDOI: ")
679 (concat "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F"
681 "&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.related=yes")))
686 ;; * A new doi link for org-mode
687 ;; The idea is to add a menu to the doi link, so rather than just clicking to open the article, you can do other things.
690 ;; 3. open citing articles
691 ;; 4. open related articles
692 ;; 5. open bibtex entry
693 ;; 6. get bibtex entry
696 (defun doi-utils-open (doi)
697 (interactive "sDOI: ")
698 (browse-url (concat "http://dx.doi.org/" doi)))
701 (defun doi-utils-open-bibtex (doi)
702 "Search through `reftex-default-bibliography' for DOI."
703 (interactive "sDOI: ")
705 (dolist (f reftex-default-bibliography)
707 (when (search-forward doi (point-max) t)
708 (bibtex-beginning-of-entry)
712 (defun doi-utils-crossref (doi)
713 "Search DOI in CrossRef."
714 (interactive "sDOI: ")
717 "http://search.crossref.org/?q=%s" doi)))
720 (defun doi-utils-google-scholar (doi)
721 "Google scholar the word at point or selection."
722 (interactive "sDOI: ")
725 "http://scholar.google.com/scholar?q=%s" doi)))
728 (defun doi-utils-pubmed (doi)
729 "Pubmed the word at point or selection."
730 (interactive "sDOI: ")
733 "http://www.ncbi.nlm.nih.gov/pubmed/?term=%s"
734 (url-hexify-string doi))))
737 (defvar doi-link-menu-funcs '()
738 "Functions to run in doi menu. Each entry is a list of (key menu-name function).
739 The function must take one argument, the doi.")
741 (setq doi-link-menu-funcs
742 '(("o" "pen" doi-utils-open)
743 ("w" "os" doi-utils-wos)
744 ("c" "iting articles" doi-utils-wos-citing)
745 ("r" "elated articles" doi-utils-wos-related)
746 ("s" "Google Scholar" doi-utils-google-scholar)
747 ("f" "CrossRef" doi-utils-crossref)
748 ("p" "ubmed" doi-utils-pubmed)
749 ("b" "open in bibtex" doi-utils-open-bibtex)
750 ("g" "et bibtex entry" doi-utils-add-bibtex-entry-from-doi)))
753 (defun doi-link-menu (link-string)
754 "Generate the link menu message, get choice and execute it.
755 Options are stored in `doi-link-menu-funcs'."
761 (concat "[" (elt tup 0) "]"
763 doi-link-menu-funcs "") ": "))
764 (let* ((input (read-char-exclusive))
766 (char-to-string input) doi-link-menu-funcs)))
779 ;; * Getting a doi for a bibtex entry missing one
780 ;; Some bibtex entries do not have a DOI, maybe because they were entered by hand, or copied from a source that did not have it available. Here we develop some functions to help you find the DOI using Crossref.
782 ;; Here is our example bibtex entry.
783 ;; #+BEGIN_SRC bibtex
784 ;; @article{deml-2014-oxide,
785 ;; author = {Ann M. Deml and Vladan Stevanovi{\'c} and
786 ;; Christopher L. Muhich and Charles B. Musgrave and
788 ;; title = {Oxide Enthalpy of Formation and Band Gap Energy As
789 ;; Accurate Descriptors of Oxygen Vacancy Formation
791 ;; journal = {Energy Environ. Sci.},
796 ;; doi = {10.1039/c3ee43874k,
797 ;; url = {http://dx.doi.org/10.1039/c3ee43874k}},
802 ;; The idea is to query Crossref in a way that is likely to give us a hit relevant to the entry.
804 ;; According to http://search.crossref.org/help/api we can send a query with a free form citation that may give us something back. We do this to get a list of candidates, and run a helm command to get the doi.
808 (defun doi-utils-crossref-citation-query ()
809 "Query Crossref with the title of the bibtex entry at point to
810 get a list of possible matches. This opens a helm buffer to
811 select an entry. The default action inserts a doi and url field
812 in the bibtex entry at point. The second action opens the doi
813 url. If there is already a doi field, the function raises an
816 (bibtex-beginning-of-entry)
817 (let* ((entry (bibtex-parse-entry))
821 (unless (string= ""(reftex-get-bib-field "doi" entry))
822 (error "Entry already has a doi field"))
825 (url-retrieve-synchronously
827 "http://search.crossref.org/dois?q="
828 (url-hexify-string (org-ref-bib-citation))))
829 (setq json-string (buffer-substring url-http-end-of-headers (point-max)))
830 (setq json-data (json-read-from-string json-string)))
832 (let* ((name (format "Crossref hits for %s" (org-ref-bib-citation)))
833 (helm-candidates (mapcar (lambda (x)
836 (cdr (assoc 'fullCitation x))
838 (cdr (assoc 'doi x)))
839 (cdr (assoc 'doi x))))
841 (source `((name . ,name)
842 (candidates . ,helm-candidates)
843 ;; just return the candidate
844 (action . (("Insert doi and url field" . (lambda (doi)
845 (bibtex-make-field "doi")
847 ;; crossref returns doi url, but I prefer only a doi for the doi field
848 (insert (replace-regexp-in-string "^http://dx.doi.org/" "" doi))
849 (when (string= ""(reftex-get-bib-field "url" entry))
850 (bibtex-make-field "url")
853 ("Open url" . (lambda (doi)
854 (browse-url doi))))))))
855 (helm :sources '(source)))))
860 ;; I wrote this function to help debug a DOI. This function generates an org-buffer with the doi, gets the json metadata, shows the bibtex entry, and the pdf link for it.
862 (defun doi-utils-debug (doi)
863 "Generate an org-buffer showing data about DOI."
864 (interactive "sDOI: ")
865 (switch-to-buffer "*debug-doi*")
868 (insert (concat "doi:" doi) "\n\n")
870 " (format "%s" (doi-utils-get-json-metadata doi)) "
874 " (doi-utils-doi-to-bibtex-string doi) "
877 " (doi-utils-get-pdf-url doi)))
879 ;; * Adding a bibtex entry from a crossref query
880 ;; The idea here is to perform a query on Crossref, get a helm buffer of candidates, and select the entry(ies) you want to add to your bibtex file. You can select a region, e.g. a free form citation, or set of words, or you can type the query in by hand.
882 (defun doi-utils-add-entry-from-crossref-query (query bibtex-file)
886 ;; now set initial input
888 ;; If region is active assume we want it
890 (replace-regexp-in-string
892 (buffer-substring (region-beginning) (region-end))))
893 ;; type or paste it in
898 (append (f-entries "." (lambda (f) (f-ext? f "bib")))
899 org-ref-default-bibliography))))
905 (url-retrieve-synchronously
907 "http://search.crossref.org/dois?q="
908 (url-hexify-string query)))
909 (setq json-string (buffer-substring url-http-end-of-headers (point-max)))
910 (setq json-data (json-read-from-string json-string)))
912 (let* ((name (format "Crossref hits for %s"
913 ;; remove carriage returns. they cause problems in helm.
914 (replace-regexp-in-string "\n" " " query)))
915 (helm-candidates (mapcar (lambda (x)
918 (cdr (assoc 'fullCitation x))
920 (cdr (assoc 'doi x)))
921 (cdr (assoc 'doi x))))
923 (source `((name . ,name)
924 (candidates . ,helm-candidates)
925 ;; just return the candidate
926 (action . (("Insert bibtex entry" . (lambda (doi)
927 (doi-utils-add-bibtex-entry-from-doi
928 (replace-regexp-in-string "^http://dx.doi.org/" "" doi) ,bibtex-file)))
929 ("Open url" . (lambda (doi)
930 (browse-url doi))))))))
931 (helm :sources '(source)))))
935 ;;; doi-utils.el ends here