deletion of categories now supported
[library/.git] / bookData.py
index 3a7c2e4..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"
@@ -29,7 +29,10 @@ Keys:
 # look up data from openlibrary.org using isbn
 def openLibrary(ISBN):
     isbn = str(ISBN)
-    jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+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 {'isbn':isbn,'title':'Book not found'}
@@ -42,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']:
@@ -69,5 +72,3 @@ def openLibrary(ISBN):
         book["subtitle"]=openBook["subtitle"]
     return book
 
-book = openLibrary(9780865479104)
-print dumps(book,indent=2)