#+Title: ISBN2BibTeX #+Author: Samuel Flint #+PROPERTY: noweb tangle #+INFOJS_OPT: view:overview toc:nil * What? Simple, an ISBN/Author-Title to BibTeX entry generator. * Why? 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. * What do I need? 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: [[https://isbndb.com/account/keys/manage][ISBNDB.Com Key manager]]. * How do I install it? 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. * How do I use it? It's simple to use, just type "isbn2bibtex.py 'author title'" in a terminal session. So if we ran "isbn2bibtex.py 'siert'" we get: #+Name: SiertExample #+begin_src bibtex @Book{kara75, author = "{Kara Grace ManJian Siert}", title = "{Tales of Cunburra and Other Stories}", publisher = "{Swirl}" } #+end_src #+Caption: Example of output. * Copyright Statement #+Name: CopyrightStatement #+begin_src text 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 . #+end_src #+Caption: Copyright Notice (CopyrightStatement) * Code ** Imports Well, we need a few libraries for this to work. #+Name: Imports #+begin_src python import sys import urllib import urllib2 import json import random import re #+end_src #+Caption: Imports (Imports) We use each of these libraries for a purpose: - sys :: to access the argument array. - urllib :: to encode the arguments for the query. - urllib2 :: to perform the query. - json :: to parse the results of the query to a python data structure. - random :: to create a random number for the key generation. - re :: to obtain the author's last name for the key generation. ** Misc Vars We have a few variables to create: #+Name: MiscVars #+begin_src python accesskey = "XXXXXXXX" jsonreq = "http://isbndb.com/api/v2/json/" + accesskey + "/books/?i=combined&q=" #+end_src #+Caption: Miscellaneous Variables (MiscVars) 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. ** Main Body Well, here's the thing, this body needs a few things, but we have a single entry for simplicity. #+Name: Body #+begin_src python <> <> for book in data['data']: if book['author_data']: <> <> #+end_src #+Caption: The Main Body of Code (Body) 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`. *** Construct query URL First, we need to construct the query URL. #+Name: ConstructURL #+begin_src python qstring = "" for arg in sys.argv: qstring = qstring + " " + arg query = urllib.quote_plus(qstring) queryURL = jsonreq + query #+end_src #+Caption: Construct the query URL (ConstructURL) 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`. *** Query and Parse Response We also have to actually query the server, this is simple, especially since we want our data. #+Name: QueryAndParse #+begin_src python jsondata = urllib2.urlopen(queryURL).read() data = json.loads(jsondata) #+end_src #+Caption: Query the API and parse to a data structure (QueryAndParse) Okay, so we have our query url, we now retrive it, and use `json.load` to turn it into a data structure. *** Create BibTeX Key Okay, every BibTeX entry must have a key, right? well, we have to create a key, and this is how we do it. #+Name: KeyCreation #+begin_src python authorid = book['author_data'][0]['id'] authorlast = re.split('_', authorid) rand = random.randint(0, 3000) bibtexkey = authorlast[0] + str(rand) #+end_src #+Caption: Create a BibTeX key (KeyCreation) 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. *** Format BibTeX Entry We now have to create the entry, and output it. #+Name: BibTeXOutput #+begin_src python 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) #+end_src #+Caption: Construct the entry and output it (BibTeXOutput) 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. ** Final Code #+begin_src python :tangle isbn2bibtex.py :shebang #!/usr/bin/python :exports code # <> <> <> <> #+end_src