1 from urllib2 import urlopen,URLError
2 from json import load,dumps
4 """ Library Book Type Description:
5 The book is a dictionary of the form { string : a, ... }
9 title - Book/Article title
10 publisher - string containing semi-colon separated list eg. "UW Press; CSC, inc."
11 authors - as above. each name is of the form "First Initials. Last" eg. "Calum T. Dalek; Conan T.B. Ladan"
15 isbn - integer (it's preferred to use the isbn-13 rather than isbn-10)
16 lccn - integer: library of congress catalogue number
17 publish date - string of date (to make things easier to code/catalogue (won't be stored)
18 publish year - int (this kind of thing will have to be confirmed by cataloguer)
20 publish location - like publisher
22 pages - integer - just the number of pages
23 pagination - string eg. "xviii, 1327-1850"
24 weight - string (purely for interest's sake eg. "3lb." or "3 pounds"
25 categories - list of strings?
29 # look up data from openlibrary.org using isbn
30 def openLibrary(ISBN):
33 jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
36 openBook = load(jsondata)
37 if "ISBN:"+isbn not in openBook:
38 return {'isbn':isbn,'title':'Book not found'}
39 openBook = openBook["ISBN:"+isbn]
40 # create my custom dict for books with the info we want.
41 book = {"isbn" : isbn}
42 book["title"]=openBook["title"]
44 if "authors" in openBook:
45 for v in openBook["authors"]:
46 book['authors'] += "; " + v['name']
47 book['authors'] = book['authors'][2:]
49 if "publishers" in openBook:
50 for v in openBook["publishers"]:
51 book["publisher"] += "; " + v['name']
52 book['publisher'] = book['publisher'][2:]
53 if "publish_places" in openBook:
54 book["publish location"]=""
55 for v in openBook["publish_places"]:
56 book["publish location"] += "; " + v['name']
57 book['publish location'] = book['publish location'][2:]
59 # for lccn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
60 if "lccn" in openBook['identifiers']:
61 book["lccn"]=int(openBook['identifiers']['lccn'][0])
62 if "publish_date" in openBook:
63 book['publish date']=openBook['publish_date']
64 #code to pull out year and month (hopefully)
65 if "number_of_pages" in openBook:
66 book["pages"]=openBook["number_of_pages"]
67 if "pagination" in openBook:
68 book["pagination"]=openBook["pagination"]
69 if "weight" in openBook:
70 book["weight"]=openBook["weight"]
71 if "subtitle" in openBook:
72 book["subtitle"]=openBook["subtitle"]