Преглед на файлове

Added ISBN2BibTeX.org, adding some commentary to it.

Sam Flint преди 10 години
ревизия
8f8822bdfe
променени са 1 файла, в които са добавени 156 реда и са изтрити 0 реда
  1. 156 0
      isbn2BibTeX.org

+ 156 - 0
isbn2BibTeX.org

@@ -0,0 +1,156 @@
+#+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 <http://www.gnu.org/licenses/>.
+#+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
+  <<ConstructURL>>
+  <<QueryAndParse>>
+  
+  for book in data['data']:
+      if book['author_data']:
+          <<KeyCreation>>
+          <<BibTeXOutput>>
+#+end_src
+#+Caption: The Main Body of Code (Body)
+
+*** Construct query URL
+First, we need to construct the query URL.
+#+Name: ConstructURL
+#+begin_src python
+  query = urllib.quote_plus(sys.argv[1])
+  queryURL = jsonreq + query
+#+end_src
+#+Caption: Construct the query URL (ConstructURL)
+
+*** 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)
+
+*** 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)
+
+*** Format BibTeX Entry
+We now have to create the entry, and output it.
+#+Name: BibTeXOutput
+#+begin_src python
+  authoritem = ""
+  first = 1
+  authorstruct = book['author_data']
+  for author in authorstruct:
+      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)
+
+** Final Code
+#+begin_src python :tangle isbn2bibtex.py :shebang #!/usr/bin/python :exports code
+  # <<CopyrightStatement>>
+  
+  <<Imports>>
+  
+  <<MiscVars>>
+  
+  <<Body>>
+#+end_src