]> git.donarmstrong.com Git - org-ref.git/blobdiff - arxiv.el
create arxiv functions for getting a bibtex entry from arxiv.
[org-ref.git] / arxiv.el
index c5548c13c1111474d25951501c9a9d5bd02e27d4..f83e768bdd65796679392c8153bc6c6d125c51cf 100644 (file)
--- a/arxiv.el
+++ b/arxiv.el
@@ -1,3 +1,29 @@
+;;; arxiv.el --- arxiv utilities for org-mode        -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2015  John Kitchin
+
+;; Author: John Kitchin <jkitchin@andrew.cmu.edu>
+;; Keywords:
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This program 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 General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;
+
+;;; Code:
+
 (org-add-link-type
  "arxiv"
  ;; clicking
      (format "\\url{http://arxiv.org/abs/%s}" keyword)))))
 
 ;; arxiv:cond-mat/0410285
+
+;; * Getting a bibtex entry for an arxiv article
+;; For an arxiv article, there is a link to a NASA ADS page like this:
+;; http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:1503.01742
+;; On that page, there is a link to a bibtex entry:
+;; http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=2015arXiv150301742H&data_type=BIBTEX&db_key=PRE&nocookieset=1
+;;
+;; It looks like you need to get a Bibliographic code from the arxiv number to
+;; then get the bibtex entry.
+
+(defun arxiv-get-bibliographic-code (arxiv-number)
+  "Get Bibliographic code for ARXIV-NUMBER."
+  (with-current-buffer
+      (url-retrieve-synchronously
+       (concat
+       "http://adsabs.harvard.edu/cgi-bin/bib_query?arXiv:"
+       arxiv-number))
+    (search-forward-regexp "name=\\\"bibcode\\\" value=\\\"\\(.*\\)\\\"")
+    (match-string 1)))
+
+(defun arxiv-get-bibtex-entry (arxiv-bibliographic-code)
+  "Get bibtex entry for ARXIV-BIBLIOGRAPHIC-CODE"
+  (with-current-buffer
+      (url-retrieve-synchronously
+       (format
+       "http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=%s&data_type=BIBTEX&db_key=PRE&nocookieset=1"
+       arxiv-bibliographic-code))
+    (goto-char  url-http-end-of-headers)
+    (if (search-forward  "Retrieved 1 abstracts" (point-max) t)
+       (progn
+         (forward-line)
+         (buffer-substring (point) (point-max)))
+      (error "Did not get one entry: %s" (buffer-substring (point) (point-max))))))
+
+
+(defun arxiv-add-bibtex-entry (arxiv-number bibfile)
+  "Add bibtex entry for ARXIV-NUMBER to BIBFILE."
+ (interactive
+   (list (read-input "arxiv: ")
+        ;;  now get the bibfile to add it to
+        (ido-completing-read
+         "Bibfile: "
+         (append (f-entries "." (lambda (f) (f-ext? f "bib")))
+                 org-ref-default-bibliography))))
+ (save-window-excursion
+   (find-file bibfile)
+   (goto-char (point-max))
+   (when (not (looking-at "^")) (insert "\n"))
+   (insert (arxiv-get-bibtex-entry (arxiv-get-bibliographic-code arxiv-number)))
+   (save-buffer)))
+
+
+(provide 'arxiv)
+;;; arxiv.el ends here