deletion of categories now supported
[library/.git] / bookData.py
index fba3e2e..516e967 100644 (file)
@@ -1,4 +1,4 @@
-from urllib2 import urlopen
+from urllib2 import urlopen,URLError
 from json import load,dumps
 
 """ Library Book Type Description:
@@ -7,7 +7,7 @@ The book is a dictionary of the form { string : a, ... }
 Keys:
   required: (ideally)
     title - Book/Article title
-    publishers - string containing semi-colon separated list eg. "UW Press; CSC, inc."
+    publisher - string containing semi-colon separated list eg. "UW Press; CSC, inc."
     authors - as above. each name is of the form "First Initials. Last" eg. "Calum T. Dalek; Conan T.B. Ladan"
   optional:
     subtitle - string
@@ -17,7 +17,7 @@ Keys:
     publish date - string of date (to make things easier to code/catalogue (won't be stored)
     publish year - int (this kind of thing will have to be confirmed by cataloguer)
     publish month - int
-    publish locations - like publishers
+    publish location - like publisher
     
     pages - integer - just the number of pages
     pagination - string eg. "xviii, 1327-1850"
@@ -27,11 +27,15 @@ Keys:
 
 
 # look up data from openlibrary.org using isbn
-def openLibrary(isbn):
-    jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn)
+def openLibrary(ISBN):
+    isbn = str(ISBN)
+    try:
+        jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
+    except URLError:
+        return {}
     openBook = load(jsondata)
     if "ISBN:"+isbn not in openBook:
-        return openBook
+        return {'isbn':isbn,'title':'Book not found'}
     openBook = openBook["ISBN:"+isbn]
     # create my custom dict for books with the info we want.
     book = {"isbn" : isbn}
@@ -41,16 +45,16 @@ def openLibrary(isbn):
         for v in openBook["authors"]:
             book['authors'] += "; " + v['name']
         book['authors'] = book['authors'][2:]
-    book["publishers"]=""
+    book["publisher"]=""
     if "publishers" in openBook:
         for v in openBook["publishers"]:
-            book["publishers"] += "; " + v['name']
-        book['publishers'] = book['publishers'][2:]
+            book["publisher"] += "; " + v['name']
+        book['publisher'] = book['publisher'][2:]
     if "publish_places" in openBook:
-        book["publish locations"]=""
+        book["publish location"]=""
         for v in openBook["publish_places"]:
-            book["publish locations"] += "; " + v['name']
-        book['publish locations'] = book['publish locations'][2:]
+            book["publish location"] += "; " + v['name']
+        book['publish location'] = book['publish location'][2:]
 
     # for lccn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
     if "lccn" in openBook['identifiers']:
@@ -68,13 +72,3 @@ def openLibrary(isbn):
         book["subtitle"]=openBook["subtitle"]
     return book
 
-book = openLibrary("9780521714723")
-print dumps(book, indent=2)
-book = openLibrary("9780521565431")
-print dumps(book, indent=2)
-book = openLibrary("689145728392")
-print dumps(book, indent=2)
-book = openLibrary("9780321468932")
-print dumps(book, indent=2)
-book = openLibrary("9781555580414")
-print dumps(book, indent=2)