1 #+TITLE: DOI utilities for making bibtex entries and downloading pdfs
3 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.
5 The principle commands you will use from here are:
7 - doi-utils-get-bibtex-entry-pdf with the cursor in a bibtex entry.
8 - doi-utils-insert-bibtex-entry-from-doi to insert a bibtex entry at your cursor, clean it and try to get a pdf.
9 - doi-utils-add-bibtex-entry-from-doi to add an entry to your default bibliography (cleaned with pdf if possible).
10 - doi-utils-add-bibtex-entry-from-region to add an entry from a highlighed doi to your default bibliography.
11 - doi-utils-update-bibtex-entry-from-doi with cursor in an entry to update its fields.
14 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
15 ;;; doi-utils.el --- get bibtex entries and pdfs from a DOI
17 ;; Copyright(C) 2014 John Kitchin
19 ;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
20 ;; This file is not currently part of GNU Emacs.
22 ;; This program is free software; you can redistribute it and/or
23 ;; modify it under the terms of the GNU General Public License as
24 ;; published by the Free Software Foundation; either version 2, or (at
25 ;; your option) any later version.
27 ;; This program is distributed in the hope that it will be useful, but
28 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
29 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 ;; General Public License for more details.
32 ;; You should have received a copy of the GNU General Public License
33 ;; along with this program ; see the file COPYING. If not, write to
34 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
35 ;; Boston, MA 02111-1307, USA.
39 ;; Lisp code to generate and update bibtex entries from a DOI, and to
40 ;; download pdfs from publisher websites from a DOI.
42 ;; Package-Requires: ((org-ref))
47 * Getting pdf files from a DOI
48 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.
50 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.
52 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
53 (defvar *doi-utils-waiting* t
54 "stores waiting state for url retrieval.")
56 (defvar *doi-utils-redirect* nil
57 "stores redirect url from a callback function")
59 (defun doi-utils-redirect-callback (&optional status)
60 "callback for url-retrieve to set the redirect"
61 (when (plist-get status :error)
62 (signal (car (plist-get status :error)) (cdr(plist-get status :error))))
63 (when (plist-get status :redirect) ; is nil if there none
64 (message "redirects = %s" (plist-get status :redirect))
65 (message "*doi-utils-redirect* set to %s"
66 (setq *doi-utils-redirect* (plist-get status :redirect))))
67 ;; we have done our job, so we are not waiting any more.
68 (setq *doi-utils-waiting* nil))
71 To actually get the redirect we use url-retrieve like this.
73 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
74 (defun doi-utils-get-redirect (doi)
75 "get redirect url from dx.doi.org/doi"
76 ;; we are going to wait until the url-retrieve is done
77 (setq *doi-utils-waiting* t)
78 ;; start with no redirect. it will be set in the callback.
79 (setq *doi-utils-redirect* nil)
81 (format "http://dx.doi.org/%s" doi)
82 'doi-utils-redirect-callback)
83 ; I suspect we need to wait here for the asynchronous process to
84 ; finish. we loop and sleep until the callback says it is done via
85 ; `*doi-utils-waiting*'. this works as far as i can tell. Before I
86 ; had to run this a few times to get it to work, which i suspect
87 ; just gave the first one enough time to finish.
88 (while *doi-utils-waiting* (sleep-for 0.1)))
91 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:
93 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
94 (defvar doi-utils-pdf-url-functions nil
95 "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.")
99 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
100 (defun aps-pdf-url (*doi-utils-redirect*)
101 (when (string-match "^http://journals.aps.org" *doi-utils-redirect*)
102 (replace-regexp-in-string "/abstract/" "/pdf/" *doi-utils-redirect*)))
106 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
107 (defun science-pdf-url (*doi-utils-redirect*)
108 (when (string-match "^http://www.sciencemag.org" *doi-utils-redirect*)
109 (concat *doi-utils-redirect* ".full.pdf")))
113 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
114 (defun nature-pdf-url (*doi-utils-redirect*)
115 (when (string-match "^http://www.nature.com" *doi-utils-redirect*)
116 (let ((result *doi-utils-redirect*))
117 (setq result (replace-regexp-in-string "/full/" "/pdf/" result))
118 (replace-regexp-in-string "\.html$" "\.pdf" result))))
122 http://onlinelibrary.wiley.com/doi/10.1002/anie.201402680/abstract
123 http://onlinelibrary.wiley.com/doi/10.1002/anie.201402680/pdf
125 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.
127 This is where the link is hidden:
129 <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>
133 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
134 (defun doi-utils-get-wiley-pdf-url (redirect-url)
135 "wileyscience direct hides the pdf url in html. we get it out here"
136 (setq *doi-utils-waiting* t)
137 (url-retrieve redirect-url
139 (beginning-of-buffer)
140 (re-search-forward "<iframe id=\"pdfDocument\" src=\"\\([^\"]*\\)\"" nil)
141 (setq *doi-utils-pdf-url* (match-string 1)
142 ,*doi-utils-waiting* nil)))
143 (while *doi-utils-waiting* (sleep-for 0.1))
144 ,*doi-utils-pdf-url*)
146 (defun wiley-pdf-url (*doi-utils-redirect*)
147 (when (string-match "^http://onlinelibrary.wiley.com" *doi-utils-redirect*)
148 (doi-utils-get-wiley-pdf-url (replace-regexp-in-string "/abstract" "/pdf" *doi-utils-redirect*))
149 ,*doi-utils-pdf-url*))
153 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
154 (defun springer-pdf-url (*doi-utils-redirect*)
155 (when (string-match "^http://link.springer.com" *doi-utils-redirect*)
156 (replace-regexp-in-string "/article/" "/content/pdf/" (concat *doi-utils-redirect* ".pdf"))))
160 here is a typical url http://pubs.acs.org/doi/abs/10.1021/nl500037x
161 the pdf is found at http://pubs.acs.org/doi/pdf/10.1021/nl500037x
163 we just change /abs/ to /pdf/.
165 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
166 (defun acs-pdf-url (*doi-utils-redirect*)
167 (when (string-match "^http://pubs.acs.org" *doi-utils-redirect*)
168 (replace-regexp-in-string "/abs/" "/pdf/" *doi-utils-redirect*)))
171 #+BEGIN_SRC emacs-lisp :tangle no
172 (acs-pdf-url "http://pubs.acs.org/doi/abs/10.1021/nl500037x")
176 : http://pubs.acs.org/doi/pdf/10.1021/nl500037x
179 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
180 (defun iop-pdf-url (*doi-utils-redirect*)
181 (when (string-match "^http://iopscience.iop.org" *doi-utils-redirect*)
182 (let ((tail (replace-regexp-in-string "^http://iopscience.iop.org" "" *doi-utils-redirect*)))
183 (concat "http://iopscience.iop.org" tail "/pdf" (replace-regexp-in-string "/" "_" tail) ".pdf"))))
187 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
188 (defun jstor-pdf-url (*doi-utils-redirect*)
189 (when (string-match "^http://www.jstor.org" *doi-utils-redirect*)
190 (concat (replace-regexp-in-string "/stable/" "/stable/pdfplus/" *doi-utils-redirect*) ".pdf")))
194 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
195 (defun aip-pdf-url (*doi-utils-redirect*)
196 (when (string-match "^http://scitation.aip.org" *doi-utils-redirect*)
197 ;; get stuff after content
199 (setq p2 (replace-regexp-in-string "^http://scitation.aip.org/" "" *doi-utils-redirect*))
200 (setq s (split-string p2 "/"))
201 (setq p1 (mapconcat 'identity (-remove-at-indices '(0 6) s) "/"))
202 (setq p3 (concat "/" (nth 0 s) (nth 1 s) "/" (nth 2 s) "/" (nth 3 s)))
203 (format "http://scitation.aip.org/deliver/fulltext/%s.pdf?itemId=/%s&mimeType=pdf&containerItemId=%s"
207 ** Taylor and Francis
208 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
209 (defun tandfonline-pdf-url (*doi-utils-redirect*)
210 (when (string-match "^http://www.tandfonline.com" *doi-utils-redirect*)
211 (replace-regexp-in-string "/abs/\\|/full/" "/pdf/" *doi-utils-redirect*)))
214 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
215 (defun ecs-pdf-url (*doi-utils-redirect*)
216 (when (string-match "^http://jes.ecsdl.org" *doi-utils-redirect*)
217 (replace-regexp-in-string "\.abstract$" ".full.pdf" *doi-utils-redirect*)))
220 http://ecst.ecsdl.org/content/25/2/2769
221 http://ecst.ecsdl.org/content/25/2/2769.full.pdf
223 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
224 (defun ecst-pdf-url (*doi-utils-redirect*)
225 (when (string-match "^http://ecst.ecsdl.org" *doi-utils-redirect*)
226 (concat *doi-utils-redirect* ".full.pdf")))
231 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
232 (defun rsc-pdf-url (*doi-utils-redirect*)
233 (when (string-match "^http://pubs.rsc.org" *doi-utils-redirect*)
234 (let ((url (downcase *doi-utils-redirect*)))
235 (setq url (replace-regexp-in-string "articlelanding" "articlepdf" url))
239 ** Elsevier/ScienceDirect
240 You cannot compute these pdf links; they are embedded in the redirected pages.
242 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
243 (defvar *doi-utils-pdf-url* nil
244 "stores url to pdf download from a callback function")
246 (defun doi-utils-get-science-direct-pdf-url (redirect-url)
247 "science direct hides the pdf url in html. we get it out here"
248 (setq *doi-utils-waiting* t)
249 (url-retrieve redirect-url
251 (beginning-of-buffer)
252 (re-search-forward "pdfurl=\"\\([^\"]*\\)\"" nil t)
253 (setq *doi-utils-pdf-url* (match-string 1)
254 ,*doi-utils-waiting* nil)))
255 (while *doi-utils-waiting* (sleep-for 0.1))
256 ,*doi-utils-pdf-url*)
259 (defun science-direct-pdf-url (*doi-utils-redirect*)
260 (when (string-match "^http://www.sciencedirect.com" *doi-utils-redirect*)
261 (doi-utils-get-science-direct-pdf-url *doi-utils-redirect*)
262 ,*doi-utils-pdf-url*))
265 ;; http://linkinghub.elsevier.com/retrieve/pii/S0927025609004558
266 ;; which actually redirect to
267 ;; http://www.sciencedirect.com/science/article/pii/S0927025609004558
268 (defun linkinghub-elsevier-pdf-url (*doi-utils-redirect*)
269 (when (string-match "^http://linkinghub.elsevier.com/retrieve" *doi-utils-redirect*)
270 (let ((second-redirect (replace-regexp-in-string
271 "http://linkinghub.elsevier.com/retrieve"
272 "http://www.sciencedirect.com/science/article"
273 ,*doi-utils-redirect*)))
274 (message "getting pdf url from %s" second-redirect)
275 ;(doi-utils-get-science-direct-pdf-url second-redirect)
276 ,*doi-utils-pdf-url*)))
280 http://www.pnas.org/content/early/2014/05/08/1319030111
281 http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf
284 http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf+html?with-ds=yes
285 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
286 (defun pnas-pdf-url (*doi-utils-redirect*)
287 (when (string-match "^http://www.pnas.org" *doi-utils-redirect*)
288 (concat *doi-utils-redirect* ".full.pdf?with-ds=yes")))
292 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
293 (setq doi-utils-pdf-url-functions
304 'science-direct-pdf-url
305 'linkinghub-elsevier-pdf-url
313 ** Get the pdf url for a doi
314 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
315 (defun doi-utils-get-pdf-url (doi)
316 "returns a url to a pdf for the doi if one can be
317 calculated. Loops through the functions in `doi-utils-pdf-url-functions'
319 (doi-utils-get-redirect doi)
321 (unless *doi-utils-redirect*
322 (error "No redirect found for %s" doi))
323 (message "applying functions")
325 (dolist (func doi-utils-pdf-url-functions)
326 (message "calling %s" func)
327 (let ((this-pdf-url (funcall func *doi-utils-redirect*)))
328 (message "t: %s" this-pdf-url)
330 (message "found pdf url: %s" this-pdf-url)
331 (throw 'pdf-url this-pdf-url))))))
335 : doi-utils-get-pdf-url
338 #+BEGIN_SRC emacs-lisp :tangle no
339 (doi-utils-get-pdf-url "10.1126/science.1158722")
343 : http://www.sciencemag.org/content/321/5890/792.full.pdf
345 #+BEGIN_SRC emacs-lisp :tangle no
346 (doi-utils-get-pdf-url "10.1021/nl500037x")
350 : http://pubs.acs.org/doi/pdf/10.1021/nl500037x
353 #+BEGIN_SRC emacs-lisp :tangle no
354 (doi-utils-get-pdf-url "10.1002/anie.201402680")
358 : http://onlinelibrary.wiley.com/doi/10.1002/anie.201402680/pdf
360 ** Finally, download the pdf
361 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
362 (defun doi-utils-get-bibtex-entry-pdf ()
363 "download pdf for entry at point if the pdf does not already
364 exist locally. The entry must have a doi. The pdf will be saved
365 to `org-ref-pdf-directory', by the name %s.pdf where %s is the
366 bibtex label. Files will not be overwritten. The pdf will be
367 checked to make sure it is a pdf, and not some html failure
368 page. you must have permission to access the pdf. We open the pdf
372 (bibtex-beginning-of-entry)
373 (let (;; get doi, removing http://dx.doi.org/ if it is there.
374 (doi (replace-regexp-in-string
375 "http://dx.doi.org/" ""
376 (bibtex-autokey-get-field "doi")))
381 ;; get the key and build pdf filename.
382 (re-search-forward bibtex-entry-maybe-empty-head)
383 (setq key (match-string bibtex-key-in-head))
384 (setq pdf-file (concat org-ref-pdf-directory key ".pdf"))
386 ;; now get file if needed.
387 (when (and doi (not (file-exists-p pdf-file)))
388 (setq pdf-url (doi-utils-get-pdf-url doi))
391 (url-copy-file pdf-url pdf-file)
392 ;; now check if we got a pdf
394 (insert-file-contents pdf-file)
395 ;; PDFS start with %PDF-1.x as the first few characters.
396 (if (not (string= (buffer-substring 1 6) "%PDF-"))
398 (message "%s" (buffer-string))
399 (delete-file pdf-file))
400 (message "%s saved" pdf-file)))
402 (when (file-exists-p pdf-file)
403 (org-open-file pdf-file)))
404 (message "No pdf-url found for %s at %s" doi *doi-utils-redirect* ))
408 * Getting bibtex entries from a DOI
410 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.
412 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
413 (defun doi-utils-get-json-metadata (doi)
415 (let ((url-request-method "GET")
416 (url-mime-accept-string "application/citeproc+json")
417 (json-object-type 'plist)
420 (url-retrieve-synchronously
421 (concat "http://dx.doi.org/" doi))
422 (setq json-data (buffer-substring url-http-end-of-headers (point-max)))
423 (message "%s" json-data)
424 (json-read-from-string json-data))))
428 : doi-utils-get-json-metadata
431 #+BEGIN_SRC emacs-lisp :tangle no
432 (doi-utils-get-json-metadata "10.1103/PhysRevLett.99.016105")
436 | :volume | 99 | :indexed | (:timestamp 1399964115538.0 :date-parts [[2014 5 13]]) | :publisher | American Physical Society (APS) | :source | CrossRef | :URL | http://dx.doi.org/10.1103/PhysRevLett.99.016105 | :ISSN | [0031-9007 1079-7114] | :DOI | 10.1103/physrevlett.99.016105 | :type | journal-article | :title | Scaling Properties of Adsorption Energies for Hydrogen-Containing Molecules on Transition-Metal Surfaces | :issue | 1 | :deposited | (:timestamp 1313712000000.0 :date-parts [[2011 8 19]]) | :reference-count | 26 | :container-title | Phys. Rev. Lett. | :author | [(:given F. :family Abild-Pedersen) (:given J. :family Greeley) (:given F. :family Studt) (:given J. :family Rossmeisl) (:given T. :family Munter) (:given P. :family Moses) (:given E. :family Skúlason) (:given T. :family Bligaard) (:given J. :family Nørskov)] | :prefix | http://id.crossref.org/prefix/10.1103 | :score | 1.0 | :issued | (:date-parts [[2007 7]]) | :subject | [Physics and Astronomy(all)] | :subtitle | [] |
438 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.
440 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
441 (defun doi-utils-expand-template (s)
442 "expand a template containing %{} with the eval of its contents"
443 (replace-regexp-in-string "%{\\([^}]+\\)}"
445 (let ((sexp (substring arg 2 -1)))
446 (format "%s" (eval (read sexp))))) s))
449 Now we define a function that fills in that template from the metadata.
451 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
452 (defun doi-utils-doi-to-bibtex-string (doi)
453 "return a bibtex entry as a string for the doi. Only articles are currently supported"
467 (setq results (doi-utils-get-json-metadata doi)
468 json-data (format "%s" results)
469 type (plist-get results :type)
470 author (mapconcat (lambda (x) (concat (plist-get x :given) " " (plist-get x :family)))
471 (plist-get results :author) " and ")
472 title (plist-get results :title)
473 journal (plist-get results :container-title)
474 volume (plist-get results :volume)
475 issue (plist-get results :issue)
476 year (elt (elt (plist-get (plist-get results :issued) :date-parts) 0) 0)
477 pages (plist-get results :page)
478 doi (plist-get results :DOI)
479 url (plist-get results :URL))
481 ((or (string= type "journal-article") (string= type "article-journal"))
482 (doi-utils-expand-template "@article{,
483 author = {%{author}},
485 journal = {%{journal}},
487 volume = {%{volume}},
494 ((string= type "proceedings-article")
495 (setq booktitle (plist-get results :container-title))
496 (doi-utils-expand-template "@inproceedings{,
497 author = {%{author}},
499 booktitle = {%{booktitle}},
507 (t (message-box "%s not supported yet." type)))))
511 : doi-utils-doi-to-bibtex-string
513 To see that in action:
514 #+BEGIN_SRC emacs-lisp :tangle no
515 (doi-utils-doi-to-bibtex-string "10.1103/PhysRevLett.99.016105")
521 author = {F. Abild-Pedersen and J. Greeley and F. Studt and J. Rossmeisl and T. Munter and P. Moses and E. Skúlason and T. Bligaard and J. Nørskov},
522 title = {Scaling Properties of Adsorption Energies for Hydrogen-Containing Molecules on Transition-Metal Surfaces},
523 journal = {Phys. Rev. Lett.},
528 doi = {10.1103/physrevlett.99.016105},
529 url = {http://dx.doi.org/10.1103/PhysRevLett.99.016105},
533 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.
535 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
536 (defun doi-utils-insert-bibtex-entry-from-doi (doi)
537 "insert bibtex entry from a doi. Also cleans entry using
538 org-ref, and tries to download the corresponding pdf."
539 (interactive "sDOI: ")
540 (insert (doi-utils-doi-to-bibtex-string doi))
542 (if (bibtex-key-in-head nil)
543 (org-ref-clean-bibtex-entry t)
544 (org-ref-clean-bibtex-entry))
546 (doi-utils-get-bibtex-entry-pdf)
547 (save-selected-window
548 (org-ref-open-bibtex-notes)))
551 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.
553 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
554 (defun doi-utils-add-bibtex-entry-from-doi (doi bibfile)
555 "add entry to end of a file in `org-ref-default-bibliography' or in the current directory ending with .bib."
558 (read-string "DOI: ")
561 (append org-ref-default-bibliography
562 (f-entries "." (lambda (f) (f-ext? f "bib")))))))
564 (goto-char (point-min))
565 (if (search-forward doi nil t)
566 (message "%s is already in this file" doi)
569 (doi-utils-insert-bibtex-entry-from-doi doi)
573 It may be you want to just highlight a doi, and then add it. Here is that function.
575 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
576 (defun doi-utils-add-bibtex-entry-from-region (start end)
577 "add entry assuming region is a doi to end of first entry in `org-ref-default-bibliography'."
579 (let ((doi (buffer-substring start end)))
580 (find-file (car org-ref-default-bibliography))
581 (goto-char (point-min))
582 (if (search-forward doi nil t)
583 (message "%s is already in this file" doi)
586 (doi-utils-insert-bibtex-entry-from-doi doi)
591 : doi-utils-add-bibtex-entry-from-region
593 * Updating bibtex entries
594 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.
596 There is not bibtex set field function, so I wrote this one.
598 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
599 (defun bibtex-set-field (field value &optional nodelim)
600 "set field to value in bibtex file. create field if it does not exist"
601 (interactive "sfield: \nsvalue: ")
602 (bibtex-beginning-of-entry)
604 (if (setq found (bibtex-search-forward-field field t))
607 (goto-char (car (cdr found)))
610 (bibtex-make-field field nil nil nodelim)
614 (message "new field being made")
615 (bibtex-beginning-of-entry)
616 (forward-line) (beginning-of-line)
617 (bibtex-next-field nil)
619 (bibtex-make-field field nil nil nodelim)
624 The updating function looks like this. We get all the keys from the json plist metadata, and update the fields if they exist.
626 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
627 (defun plist-get-keys (plist)
628 "return keys in a plist"
630 for key in results by #'cddr collect key))
632 (defun doi-utils-update-bibtex-entry-from-doi (doi)
633 "update fields in a bibtex entry from the doi. Every field will be updated, so previous changes will be lost."
635 (or (replace-regexp-in-string "http://dx.doi.org/" "" (bibtex-autokey-get-field "doi"))
636 (read-string "DOI: "))))
637 (let* ((results (doi-utils-get-json-metadata doi))
638 (type (plist-get results :type))
640 (lambda (x) (concat (plist-get x :given)
641 " " (plist-get x :family)))
642 (plist-get results :author) " and "))
643 (title (plist-get results :title))
644 (journal (plist-get results :container-title))
649 (plist-get results :issued) :date-parts) 0) 0)))
650 (volume (plist-get results :volume))
651 (number (or (plist-get results :issue) ""))
652 (pages (or (plist-get results :page) ""))
653 (url (or (plist-get results :URL) ""))
654 (doi (plist-get results :DOI)))
656 ;; map the json fields to bibtex fields. The code each field is mapped to is evaluated.
657 (setq mapping '((:author . (bibtex-set-field "author" author))
658 (:title . (bibtex-set-field "title" title))
659 (:container-title . (bibtex-set-field "journal" journal))
660 (:issued . (bibtex-set-field "year" year))
661 (:volume . (bibtex-set-field "volume" volume))
662 (:issue . (bibtex-set-field "number" number))
663 (:page . (bibtex-set-field "pages" pages))
664 (:DOI . (bibtex-set-field "doi" doi))
665 (:URL . (bibtex-set-field "url" url))))
667 ;; now we have code to run for each entry. we map over them and evaluate the code
670 (eval (cdr (assoc key mapping))))
671 (plist-get-keys results)))
673 ; reclean entry, but keep key if it exists.
674 (if (bibtex-key-in-head)
675 (org-ref-clean-bibtex-entry t)
676 (org-ref-clean-bibtex-entry)))
678 * DOI functions for WOS
679 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.
682 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
683 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
684 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
686 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.
688 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
689 (defun doi-utils-wos (doi)
690 "Open Web of Science entry for DOI"
691 (interactive "sDOI: ")
694 "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info:doi/%s" doi)))
696 (defun doi-utils-wos-citing (doi)
697 "Open Web of Science citing articles entry. May be empty if none are found"
698 (interactive "sDOI: ")
701 "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F"
703 "&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.citing=yes")))
705 (defun doi-utils-wos-related (doi)
706 "Open Web of Science related articles page."
707 (interactive "sDOI: ")
709 (concat "http://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info%3Adoi%2F"
711 "&svc_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Asch_svc&svc.related=yes")))
715 * A new doi link for org-mode
716 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.
719 3. open citing articles
720 4. open related articles
724 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el :results silent
725 (defun doi-utils-open (doi)
726 (interactive "sDOI: ")
727 (browse-url (concat "http://dx.doi.org/" doi)))
730 (defun doi-utils-open-bibtex (doi)
731 "Search through `reftex-default-bibliography' for DOI."
732 (interactive "sDOI: ")
734 (dolist (f reftex-default-bibliography)
736 (when (search-forward doi (point-max) t)
737 (bibtex-beginning-of-entry)
741 (defun doi-utils-crossref (doi)
742 "Search DOI in CrossRef."
743 (interactive "sDOI: ")
746 "http://search.crossref.org/?q=%s" doi)))
749 (defun doi-utils-google-scholar (doi)
750 "Google scholar the word at point or selection."
751 (interactive "sDOI: ")
754 "http://scholar.google.com/scholar?q=%s" doi)))
757 (defun doi-utils-pubmed (doi)
758 "Pubmed the word at point or selection."
759 (interactive "sDOI: ")
762 "http://www.ncbi.nlm.nih.gov/pubmed/?term=%s"
763 (url-hexify-string doi))))
766 (defvar doi-link-menu-funcs '()
767 "Functions to run in doi menu. Each entry is a list of (key menu-name function).
768 The function must take one argument, the doi.")
770 (setq doi-link-menu-funcs
771 '(("o" "pen" doi-utils-open)
772 ("w" "os" doi-utils-wos)
773 ("c" "iting articles" doi-utils-wos-citing)
774 ("r" "elated articles" doi-utils-wos-related)
775 ("s" "Google Scholar" doi-utils-google-scholar)
776 ("f" "CrossRef" doi-utils-crossref)
777 ("p" "ubmed" doi-utils-pubmed)
778 ("b" "open in bibtex" doi-utils-open-bibtex)
779 ("g" "et bibtex entry" doi-utils-add-bibtex-entry-from-doi)))
782 (defun doi-link-menu (link-string)
783 "Generate the link menu message, get choice and execute it.
784 Options are stored in `doi-link-menu-funcs'."
790 (concat "[" (elt tup 0) "]"
792 doi-link-menu-funcs "") ": "))
793 (let* ((input (read-char-exclusive))
795 (char-to-string input) doi-link-menu-funcs)))
808 doi:10.1021/jp047349j
811 #+BEGIN_SRC emacs-lisp :tangle doi-utils.el
815 #+BEGIN_SRC emacs-lisp :tangle no
816 (org-babel-load-file "doi-utils.org")
820 : Loaded doi-utils.el