added lookup by lccn
authorJohn Ladan <jladan@uwaterloo.ca>
Fri, 15 Jun 2012 18:11:21 +0000 (14:11 -0400)
committerJohn Ladan <jladan@uwaterloo.ca>
Fri, 15 Jun 2012 18:11:21 +0000 (14:11 -0400)
TODO [new file with mode: 0644]
bookData.py
form.py
librarian.py

diff --git a/TODO b/TODO
new file mode 100644 (file)
index 0000000..6ea38d8
--- /dev/null
+++ b/TODO
@@ -0,0 +1 @@
+Look up by LCCN
index 516e967..b9231ed 100644 (file)
@@ -27,7 +27,7 @@ Keys:
 
 
 # look up data from openlibrary.org using isbn
-def openLibrary(ISBN):
+def openLibrary_isbn(ISBN):
     isbn = str(ISBN)
     try:
         jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
@@ -72,3 +72,51 @@ def openLibrary(ISBN):
         book["subtitle"]=openBook["subtitle"]
     return book
 
+# look up data from openlibrary.org using lccn
+def openLibrary_lccn(LCCN):
+    lccn = str(LCCN)
+    try:
+        jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=lccn:"+lccn, timeout=3)
+    except URLError:
+        return {}
+    openBook = load(jsondata)
+    if "lccn:"+lccn not in openBook:
+        return {'lccn':lccn,'title':'Book not found'}
+    openBook = openBook["lccn:"+lccn]
+    # create my custom dict for books with the info we want.
+    book = {"lccn" : lccn}
+    book["title"]=openBook["title"]
+    book["authors"]=""
+    if "authors" in openBook:
+        for v in openBook["authors"]:
+            book['authors'] += "; " + v['name']
+        book['authors'] = book['authors'][2:]
+    book["publisher"]=""
+    if "publishers" in openBook:
+        for v in openBook["publishers"]:
+            book["publisher"] += "; " + v['name']
+        book['publisher'] = book['publisher'][2:]
+    if "publish_places" in openBook:
+        book["publish location"]=""
+        for v in openBook["publish_places"]:
+            book["publish location"] += "; " + v['name']
+        book['publish location'] = book['publish location'][2:]
+
+    # for isbn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
+    if "isbn_10" in openBook['identifiers']:
+        book["isbn"]=int(openBook['identifiers']['isbn_10'][0])
+    if "isbn_13" in openBook['identifiers']:
+        book["isbn"]=int(openBook['identifiers']['isbn_13'][0])
+    if "publish_date" in openBook:
+        book['publish date']=openBook['publish_date']
+        #code to pull out year and month (hopefully)
+    if "number_of_pages" in openBook:
+        book["pages"]=openBook["number_of_pages"]
+    if "pagination" in openBook:
+        book["pagination"]=openBook["pagination"]
+    if "weight" in openBook:
+        book["weight"]=openBook["weight"]
+    if "subtitle" in openBook:
+        book["subtitle"]=openBook["subtitle"]
+    return book
+
diff --git a/form.py b/form.py
index 2ac3f94..cd53151 100644 (file)
--- a/form.py
+++ b/form.py
@@ -204,16 +204,24 @@ class bookForm(formWindow):
 
     # redefineable functions lookup is called when 'enter' is pressed on ISBN
     # and returns the looked-up book. Default returns nothing
-    def lookup(self,isbn):
+    def lookup_isbn(self,isbn):
         return {'isbn':isbn}
+    
+    def lookup_lccn(self,lccn):
+        return {'lccn':lccn}
 
     def returnBook(self):
         return self.returnValues()
 
     def handleInput(self,ch):
         if ch==10 or ch==curses.KEY_ENTER:
-            if self.hl==0:
-                book = self.lookup(self.entries[0])
+            if self.hl==0:          # lookup by isbn
+                book = self.lookup_isbn(self.entries[0])
+                if book != {}:
+                    self.updateEntries(book)
+                self.refresh()
+            if self.hl==1:          # lookup by lccn
+                book = self.lookup_lccn(self.entries[1])
                 if book != {}:
                     self.updateEntries(book)
                 self.refresh()
index 7365a6a..eee1ec6 100755 (executable)
@@ -85,7 +85,8 @@ def addForm():
     bf = form.bookForm(w,hb)
     (r,c)=w.getmaxyx()
     w.mvwin((my-r)/2,(mx-c)/2)
-    bf.lookup=bookData.openLibrary
+    bf.lookup_isbn=bookData.openLibrary_isbn
+    bf.lookup_lccn=bookData.openLibrary_lccn
     bf.caption='Add a Book'
     bf.blabel = 'Add'
     book = bf.eventLoop()