added lookup by lccn
parent
c39c4ef3db
commit
37fd6d7d57
50
bookData.py
50
bookData.py
|
@ -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
|
||||
|
||||
|
|
14
form.py
14
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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue