Simple, an ISBN/Author-Title to BibTeX entry generator.
Have you been doing research, and forgot to write down a citation, or forgot to enter it into your citation management system? Exactly! Well, that is what ths is for, just write down the author and name of the book, and it outputs BibTeX to import into about any reference manager.
Simple, you need an install of python, I just use the standard Library. You also need a ISBNDB.com API Key, which you can get here: ISBNDB.Com Key manager.
You can resolve the noweb labels yourself and do copy/paste, but that's the hard way! The easy way is to open it in emacs, by typing C-x C-f /path/to/file RET, then type C-c C-v t. This will tangle the file to the current directory as isbn2bibtex.py.
It's simple to use, just type "isbn2bibtex.py 'author title'" in a terminal session. So if we ran "isbn2bibtex.py 'siert'" we get:
@Book{kara75, author = "{Kara Grace ManJian Siert}", title = "{Tales of Cunburra and Other Stories}", publisher = "{Swirl}" }
Copyright FlintFam Systems Management, 2013. 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/>.
Well, we need a few libraries for this to work.
import sys import urllib import urllib2 import json import random import re
We use each of these libraries for a purpose:
to access the argument array.
to encode the arguments for the query.
to perform the query.
to parse the results of the query to a python data structure.
to create a random number for the key generation.
to obtain the author's last name for the key generation.
We have a few variables to create:
accesskey = "XXXXXXXX" jsonreq = "http://isbndb.com/api/v2/json/" + accesskey + "/books/?i=combined&q="
Okay, so the first variable,`accesskey`, is supposed to contain your ISBNDB access key, you need to set this before you use it! The next one, `jsonreq`, creates the query string, sans query. The `i=combined` means that we may query in any way, ISBN, author, title, or a combination of such.
Well, here's the thing, this body needs a few things, but we have a single entry for simplicity.
<<ConstructURL>> <<QueryAndParse>> for book in data['data']: if book['author_data']: <<KeyCreation>> <<BibTeXOutput>>
The first thing that we need to do is construct the query URL. We then perform the query and parse the results. from there we enter a for loop, grabbing items from the `data` key of the query as book, and creating a key, and outputting an entry if there is an `author_data` key in `book`.
First, we need to construct the query URL.
qstring = "" for arg in sys.argv: qstring = qstring + " " + arg query = urllib.quote_plus(qstring) queryURL = jsonreq + query
So the first thing we do is to construct a single string from all of the arguments. We than url encode it as `query`, tacking that on to `jsonreq` as `queryURL`.
We also have to actually query the server, this is simple, especially since we want our data.
jsondata = urllib2.urlopen(queryURL).read() data = json.loads(jsondata)
Okay, so we have our query url, we now retrive it, and use `json.load` to turn it into a data structure.
Okay, every BibTeX entry must have a key, right? well, we have to create a key, and this is how we do it.
authorid = book['author_data'][0]['id'] authorlast = re.split('_', authorid) rand = random.randint(0, 3000) bibtexkey = authorlast[0] + str(rand)
We're in the loop! We need to use some of data to create the BibTeX key, the lastname of the first author, and a random integer between 0 and 3000. I chose 0-3000 for the reason that you are unlikely to come across any bokks from past then at this current time.
We now have to create the entry, and output it.
authoritem = "" first = 1 for author in book['author_data']: authorname = author['name'] if first == 0: authoritem = authoritem + " and " + authorname else: authoritem = authorname first = 0 titleitem = book['title_latin'] publisher = book['publisher_name'] entry = "@Book{" + bibtexkey + ",\nauthor = \"{" + authoritem + "}\",\ntitle = \"{" + titleitem + "}\",\npublisher = \"{" + publisher + "}\"\n}\n\n" print(entry)
So the first thing done here is create an author string, by going through the author data and concatinating the names of each of the authors with the previous and the word `and`. We then retrive the book's title and publisher. From their, we output a fully formed BibTeX entry.
# <<CopyrightStatement>> <<Imports>> <<MiscVars>> <<Body>>