Difference between revisions of "SPIRES script"

From String Theory Wiki
Jump to: navigation, search
 
m (revert after spam)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
SPIRES script is a command line program written in [http://www.python.org/ Python] that accesses information on papers in the [http://www.slac.stanford.edu/spires/ SPIRES] online high energy physics literature database.  It can access title, author and [[References with BiBTeX|BiBTeX]] records and update your local [[References with BiBTeX|BiBTeX]] database and/or a local HTML record of the papers you have.
+
SPIRES script is a program written in the [http://www.python.org/ python] programming language that accesses information on papers in the [http://www.slac.stanford.edu/spires/ SPIRES] online high energy physics literature database.  It can access title, author, bibitem and [[References with BiBTeX|BiBTeX]] records, update your local BiBTeX library and download papers from the [http://www.arxiv.org/ arXiv].  A separate program [[#listcitations.py|listcitations.py]] reads through a TeX file and then looks up all the \cite{...} references and outputs the bibitems.  Although originally written for the GNU/Linux command line, the code should be portable enough to run on any operating system with a python interpreter.
 +
 
 +
===download===
 +
Download [[Spires.py|spires.py]] and [[Listcitations.py|listcitations.py]].
  
 
===installation on a GNU/Linux system===
 
===installation on a GNU/Linux system===
The script requires the python interpreter and wget to be installed on your system.  These are automatically installed on most GNU/Linux distributions, but check they are there by typing
+
The script requires the python interpreter to be installed on your system.  This is automatically installed on most GNU/Linux distributions, but check it is there by typing
 
<pre>
 
<pre>
 
python
 
python
</pre>
 
and
 
<pre>
 
wget
 
 
</pre>
 
</pre>
 
at the command line and see what happens.
 
at the command line and see what happens.
  
If python and wget are present copy the [[#script|script]] into a text file with a name such as spires.py.
+
If python is present download [[Spires.py|spires.py]] and run it by typing
 
 
To run it type
 
 
<pre>
 
<pre>
 
python spires.py hep-th/9711200
 
python spires.py hep-th/9711200
 
</pre>
 
</pre>
  
===basic usage===
+
===usage===
  
The typical accepted inputs are the usual arXiv references or a SPIRES-style journal reference
+
The typical accepted inputs are the usual arXiv references, the SPIRES TeX key or a SPIRES-style journal reference
 
<pre>
 
<pre>
 
python spires.py hep-th/9711200
 
python spires.py hep-th/9711200
 
python spires.py 0705.0303
 
python spires.py 0705.0303
 +
python spires.py Maldacena:1997re
 
python spires.py CMPHA,43,199
 
python spires.py CMPHA,43,199
 +
python spires.py Phys.Rev.,D52,5783
 
</pre>
 
</pre>
  
Line 42: Line 41:
 
     SLACcitation  = "%%CITATION = HEP-TH/9711200;%%"
 
     SLACcitation  = "%%CITATION = HEP-TH/9711200;%%"
 
}
 
}
\cite{Maldacena:1997re}
 
 
Maldacena, Juan M.
 
Maldacena, Juan M.
 
The large N limit of superconformal field theories and supergravity
 
The large N limit of superconformal field theories and supergravity
<p><a href="http://arxiv.org/abs/hep-th/9711200">The large N limit of superconformal field theories and supergravity</a> by Maldacena, Juan M. (hep-th/9711200)</p>
+
\cite{Maldacena:1997re}
 
</pre>
 
</pre>
  
Line 53: Line 51:
 
SPIRES script
 
SPIRES script
 
Usage:
 
Usage:
python spires.py reference [ -hatbcevd ] [ --library library.bib ] [ --database database.html ]
+
python spires.py reference [ -hbiatcev ] [ --help ] [ --library library.bib ] [ --download download_path/ ]
"reference" must be a standard arXiv reference, e.g. hep-th/9711200 or 0705.0303, or a SPIRES journal reference, e.g. CMPHA,43,199
+
"reference" must be a standard arXiv reference, e.g. hep-th/9711200, 0705.0303, Maldacena:1997re or a SPIRES journal reference, e.g. CMPHA,43,199
 
Options:
 
Options:
 
-h, --help
 
-h, --help
 
displays this help message
 
displays this help message
 +
-b
 +
displays the BiBTeX entry
 +
-i
 +
displays the bibitem entry
 
-a
 
-a
 
displays the author(s)
 
displays the author(s)
 
-t
 
-t
 
displays the title
 
displays the title
-b
 
displays the BiBTeX entry
 
 
-c
 
-c
displays the LaTeX citation
+
displays the TeX citation key
 
-e
 
-e
 
displays everything
 
displays everything
 
-v
 
-v
 
verbose mode
 
verbose mode
-d
+
 
displays an HTML database entry
+
--download download_path/
 +
for arXiv references downloads a pdf of the paper from the arXiv to the directory download_path/
 
--library library.bib
 
--library library.bib
if it is not already in library.bib, appends the BiBTeX entry to library.bib
+
if it is not already in library.bib, appends the BiBTeX entry to library.bib; use at your own risk
--database database.html
 
if it is not already in database.html, appends the HTML database entry to database.html
 
 
</pre>
 
</pre>
  
Each item can be specified individual with switches, e.g. -t for title, -a for authors, -b for BiBTeX entry, -c for LaTeX citation, -d for HTML database entry.  For example
+
Each item can be specified individually with switches, e.g. -t for title, -a for authors, -b for BiBTeX entry, -c for SPIRES TeX key.  For example
 
<pre>
 
<pre>
 
tom@fyodor:~$ python spires.py hep-th/9711200 -at
 
tom@fyodor:~$ python spires.py hep-th/9711200 -at
Line 90: Line 89:
 
</pre>
 
</pre>
  
If you keep an HTML list of papers in database.html then the script can update the file
+
To download a PDF of an eprint from the [http://www.arxiv.org/ arXiv] to the directory <code>some_folder/</code>
 
<pre>
 
<pre>
python spires.py hep-th/9711200 --database database.html
+
python spires.py hep-th/9711200 --download some_folder/
 
</pre>
 
</pre>
  
===script===
+
===listcitations.py===
Copy the following into a text file with a name such as spires.py
+
To use this program you must download [[Spires.py|spires.py]] and [[Listcitations.py|listcitations.py]] into the same directory.
 +
 
 +
This program reads a TeX file and then looks up all the \cite{...} references and outputs the bibitems. The typical accepted inputs are the usual arXiv references, the SPIRES TeX key or a SPIRES-style journal reference. With no options it will output bibitems
 
<pre>
 
<pre>
#! /usr/bin/python
+
python listcitations.py tex_file.tex
 +
</pre>
 +
It can also output BiBTeX entries instead
 +
<pre>
 +
python listcitations.py tex_file.tex -b
 +
</pre>
 +
To see all the options access the help page
 +
<pre>
 +
tom@tarkovsky:~$ python listcitations.py -h
 +
list citations script
 +
Usage:
 +
python listcitations.py TeX_file_name.tex [ -hbiv ] [ --help ]
  
## SPIRES script version 0.1
+
TeX_file_name must contain citations as standard arXiv references,
 +
e.g. hep-th/9711200, 0705.0303, Maldacena:1997re or SPIRES journal
 +
references, e.g. CMPHA,43,199
  
## Copyright 2007 Tom Brown
 
 
## 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 2 of the
 
## License, or 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.
 
 
## The GNU General Public License is available at
 
## http://www.gnu.org/copyleft/gpl.html
 
 
 
import sys, os, getopt, re
 
 
libraryOpt = 0
 
databaseOpt = 0
 
authorOpt = 0
 
titleOpt = 0
 
bibtexOpt = 0
 
citeOpt = 0
 
verboseOpt = 0
 
htmlOpt = 0
 
 
try:
 
    options, arguments = getopt.gnu_getopt(sys.argv[1:],
 
    'hatbcevd', ['help','library=', 'database='])
 
except getopt.error:
 
    print 'error: you tried to use an unknown option or the argument for an option that requires it was missing; try \'spires.py -h\' for more information'
 
    sys.exit(0)
 
 
for o,a in options:
 
    if o in  ('-h','--help'):
 
        print '''SPIRES script
 
Usage:
 
python spires.py reference [ -hatbcevd ] [ --library library.bib ] [ --database database.html ]
 
"reference" must be a standard arXiv reference, e.g. hep-th/9711200 or 0705.0303, or a SPIRES journal reference, e.g. CMPHA,43,199
 
 
Options:
 
Options:
 
-h, --help
 
-h, --help
 
displays this help message
 
displays this help message
-a
 
displays the author(s)
 
-t
 
displays the title
 
 
-b
 
-b
 
displays the BiBTeX entry
 
displays the BiBTeX entry
-c
+
-i
displays the LaTeX citation
+
displays the bibitem entry
-e
 
displays everything
 
 
-v
 
-v
 
verbose mode
 
verbose mode
-d
+
</pre>
displays an HTML database entry
 
--library library.bib
 
if it is not already in library.bib, appends the BiBTeX entry to library.bib
 
--database database.html
 
if it is not already in database.html, appends the HTML database entry to database.html
 
'''
 
        sys.exit(0)
 
 
 
    elif o == '--library':
 
        if a == '':
 
            print '--library expects an argument'
 
            sys.exit(0)
 
        libraryOpt = 1
 
        BiBTeXlibraryFileName = os.path.expanduser(a)
 
        print 'library file name is ' + BiBTeXlibraryFileName
 
       
 
    elif o == '--database':
 
        if a == '':
 
            print '--database expects an argument'
 
            sys.exit(0)
 
        databaseOpt = 1
 
        databaseFileName = os.path.expanduser(a)
 
        print 'database file name is ' + databaseFileName
 
 
 
    elif o == '-a':
 
        authorOpt = 1
 
       
 
    elif o == '-t':
 
        titleOpt = 1
 
       
 
    elif o == '-b':
 
        bibtexOpt = 1
 
       
 
    elif o == '-c':
 
        citeOpt = 1
 
 
 
    elif o == '-d':
 
        htmlOpt = 1
 
 
 
    elif o == '-e':
 
        authorOpt = 1
 
        titleOpt = 1
 
        bibtexOpt = 1
 
        citeOpt = 1
 
        htmlOpt = 1
 
 
 
    elif o == '-v':
 
        verboseOpt = 1
 
 
 
if len(arguments) != 1:
 
    print 'you didn\'t specify a SPIRES reference'
 
    sys.exit(0)
 
else:
 
    ref=arguments[0]
 
 
 
if len(options) == 0:
 
    authorOpt = 1
 
    titleOpt = 1
 
    bibtexOpt = 1
 
    citeOpt = 1
 
    htmlOpt = 1 
 
 
 
if verboseOpt:
 
    print 'reference is ' + ref
 
 
 
oldeprint = 0
 
neweprint = 0
 
journal = 0
 
numbers = ['0','1','2','3','4','5','6','7','8','9']
 
  
 +
===future features===
  
# These tests are not infallible: for example 054/234 will be counted
+
* allow alias citations such as \cite{mypaper} for listcitations.py
# as an old-style eprint
 
  
if ref.count('/') > 0:
+
===technical details===
    oldeprint = 1
 
    type = 'this is an old-style eprint reference'
 
elif ref[0] in numbers:
 
    neweprint = 1
 
    type = 'this is a new-style eprint reference'
 
else:
 
    journal = 1
 
    type = 'this is a journal reference'
 
  
if verboseOpt:
+
The code is very modular so it is easy to write your own programs using the functions defined in spires.py.  Just import the functions in spires.py with <code>import spires</code>; you can use spires.py as a library of SPIRES-related functions (see [[Listcitations.py|listcitations.py]] as an example).
    print type
 
  
if oldeprint:
+
===comments and bugs===
    os.system('wget http://www.slac.stanford.edu/spires/find/hep/wwwbriefbibtex?eprint=' + ref + ' -q -O spires.html')
+
If you have any comments or find bugs, please contact [[User:Tom|Tom]].
elif neweprint:
 
    os.system('wget http://www.slac.stanford.edu/spires/find/hep/wwwbriefbibtex?eprint=arXiv:' + ref + ' -q -O spires.html')
 
elif journal:
 
    os.system('wget http://www.slac.stanford.edu/spires/find/hep/wwwbriefbibtex?j=' + ref + ' -q -O spires.html')
 
  
 +
===thanks===
 +
Thanks to Travis C. Brooks of [http://www.slac.stanford.edu/spires/ SPIRES] for his help and [http://theory.tifr.res.in/~kevin/ Kevin Goldstein] for pointing out the python <code>urllib</code> library.
  
spiresHTMLfile = open('spires.html', 'r')
+
===licence===
 
+
This script is Copyright 2007 Tom Brown and made available under the [http://www.gnu.org/copyleft/gpl.html GNU General Public Licence].
BiBTeX = spiresHTMLfile.read()
 
 
 
spiresHTMLfile.close()
 
 
 
os.remove('spires.html')
 
 
 
if 'No records' in BiBTeX:
 
  print "no records were found in SPIRES to match your search, please try again"
 
  sys.exit(0)
 
 
 
BiBTeX = BiBTeX[BiBTeX.find('@'):]
 
 
 
BiBTeX = BiBTeX[:BiBTeX.find('}')+1]
 
 
 
if bibtexOpt:
 
    print BiBTeX
 
 
 
# remove excess white space and replace with a single space
 
data = re.sub(r'\s+',r' ',BiBTeX)
 
 
 
cite = data[data.find('{')+1:data.find(',')]
 
 
 
if citeOpt:
 
    print '\cite{' + cite + '}'
 
 
 
author = data[data.find('author'):]
 
author = author[author.find('\"')+1:]
 
author = author[:author.find('\"')]
 
 
 
if authorOpt:
 
    print author
 
 
 
title = data[data.find('title'):]
 
title = title[title.find('\"')+1:]
 
title = title[:title.find('\"')]
 
 
 
if titleOpt:
 
    print title
 
 
 
if libraryOpt:
 
 
 
    libraryfile = open(BiBTeXlibraryFileName, 'r+')
 
 
 
    library = libraryfile.read()
 
 
 
    if library.count(cite) == 0:
 
        print 'adding BiBTeX entry to bib_string.bib'
 
        #find the end of the file (the second argument means count from
 
        #the end of the file
 
        libraryfile.seek(0,2)
 
        libraryfile.write('\n' + BiBTeX + '\n')
 
    else:
 
        print 'BiBTeX entry already in library'
 
 
 
    libraryfile.close()
 
 
 
if oldeprint:
 
    databaseEntry = '<p><a href=\"http://arxiv.org/abs/' + ref + '\">' + title + '</a> by ' + author + ' (' + ref + ')</p>'
 
elif neweprint:
 
    databaseEntry = '<p><a href=\"http://arxiv.org/abs/' + ref + '\">' + title + '</a> by ' + author + ' (' + ref + ')</p>'
 
elif journal:
 
    databaseEntry = '<p><a href=\"http://www.slac.stanford.edu/spires/find/hep/www?j=' + ref + '\">' + title + '</a> by ' + author + ' (' + ref + ')</p>'
 
 
 
if htmlOpt:
 
    print databaseEntry
 
 
 
if databaseOpt:
 
   
 
    databasefile = open(databaseFileName, 'r+')
 
 
 
    database = databasefile.read()
 
 
 
    if database.count(ref) == 0:
 
        print 'adding to database'
 
        databasefile.seek(0,2)
 
        databasefile.write('\n' + databaseEntry + '\n')
 
    else:
 
        print 'HTML entry already in database'
 
 
 
    databasefile.close()
 
</pre>
 

Latest revision as of 14:48, 15 July 2010

SPIRES script is a program written in the python programming language that accesses information on papers in the SPIRES online high energy physics literature database. It can access title, author, bibitem and BiBTeX records, update your local BiBTeX library and download papers from the arXiv. A separate program listcitations.py reads through a TeX file and then looks up all the \cite{...} references and outputs the bibitems. Although originally written for the GNU/Linux command line, the code should be portable enough to run on any operating system with a python interpreter.

download

Download spires.py and listcitations.py.

installation on a GNU/Linux system

The script requires the python interpreter to be installed on your system. This is automatically installed on most GNU/Linux distributions, but check it is there by typing

python

at the command line and see what happens.

If python is present download spires.py and run it by typing

python spires.py hep-th/9711200

usage

The typical accepted inputs are the usual arXiv references, the SPIRES TeX key or a SPIRES-style journal reference

python spires.py hep-th/9711200
python spires.py 0705.0303
python spires.py Maldacena:1997re
python spires.py CMPHA,43,199
python spires.py Phys.Rev.,D52,5783

If no options are specified all possible outputs will be displayed

tom@fyodor:~$ python spires.py hep-th/9711200
@Article{Maldacena:1997re,
     author    = "Maldacena, Juan M.",
     title     = "The large N limit of superconformal field theories and
                  supergravity",
     journal   = "Adv. Theor. Math. Phys.",
     volume    = "2",
     year      = "1998",
     pages     = "231-252",
     eprint    = "hep-th/9711200",
     SLACcitation  = "%%CITATION = HEP-TH/9711200;%%"
}
Maldacena, Juan M.
The large N limit of superconformal field theories and supergravity
\cite{Maldacena:1997re}

To see all the options access the help page

tom@fyodor:~$ python spires.py -h
SPIRES script
Usage:
python spires.py reference [ -hbiatcev ] [ --help ] [ --library library.bib ] [ --download download_path/ ]
"reference" must be a standard arXiv reference, e.g. hep-th/9711200, 0705.0303, Maldacena:1997re or a SPIRES journal reference, e.g. CMPHA,43,199
Options:
-h, --help
displays this help message
-b
displays the BiBTeX entry
-i
displays the bibitem entry
-a
displays the author(s)
-t
displays the title
-c
displays the TeX citation key
-e
displays everything
-v
verbose mode

--download download_path/
for arXiv references downloads a pdf of the paper from the arXiv to the directory download_path/
--library library.bib
if it is not already in library.bib, appends the BiBTeX entry to library.bib; use at your own risk

Each item can be specified individually with switches, e.g. -t for title, -a for authors, -b for BiBTeX entry, -c for SPIRES TeX key. For example

tom@fyodor:~$ python spires.py hep-th/9711200 -at
Maldacena, Juan M.
The large N limit of superconformal field theories and supergravity

If you have a BiBTeX database file such as library.bib then the script can update the file with the BiBTeX entry if it is not already present

python spires.py hep-th/9711200 --library library.bib

To download a PDF of an eprint from the arXiv to the directory some_folder/

python spires.py hep-th/9711200 --download some_folder/

listcitations.py

To use this program you must download spires.py and listcitations.py into the same directory.

This program reads a TeX file and then looks up all the \cite{...} references and outputs the bibitems. The typical accepted inputs are the usual arXiv references, the SPIRES TeX key or a SPIRES-style journal reference. With no options it will output bibitems

python listcitations.py tex_file.tex

It can also output BiBTeX entries instead

python listcitations.py tex_file.tex -b

To see all the options access the help page

tom@tarkovsky:~$ python listcitations.py -h
list citations script
Usage:
python listcitations.py TeX_file_name.tex [ -hbiv ] [ --help ]

TeX_file_name must contain citations as standard arXiv references,
e.g. hep-th/9711200, 0705.0303, Maldacena:1997re or SPIRES journal
references, e.g. CMPHA,43,199

Options:
-h, --help
displays this help message
-b
displays the BiBTeX entry
-i
displays the bibitem entry
-v
verbose mode

future features

  • allow alias citations such as \cite{mypaper} for listcitations.py

technical details

The code is very modular so it is easy to write your own programs using the functions defined in spires.py. Just import the functions in spires.py with import spires; you can use spires.py as a library of SPIRES-related functions (see listcitations.py as an example).

comments and bugs

If you have any comments or find bugs, please contact Tom.

thanks

Thanks to Travis C. Brooks of SPIRES for his help and Kevin Goldstein for pointing out the python urllib library.

licence

This script is Copyright 2007 Tom Brown and made available under the GNU General Public Licence.