Added help bar
[public/library.git] / bookData.py
1 from urllib2 import urlopen
2 from json import load,dumps
3
4 """ Library Book Type Description:
5 The book is a dictionary of the form { string : a, ... }
6
7 Keys:
8   required: (ideally)
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"
12   optional:
13     subtitle - string
14     edition - integer
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)
19     publish month - int
20     publish location - like publisher
21     
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?
26 """
27
28
29 # look up data from openlibrary.org using isbn
30 def openLibrary(ISBN):
31     isbn = str(ISBN)
32     jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn)
33     openBook = load(jsondata)
34     if "ISBN:"+isbn not in openBook:
35         return {'isbn':isbn,'title':'Book not found'}
36     openBook = openBook["ISBN:"+isbn]
37     # create my custom dict for books with the info we want.
38     book = {"isbn" : isbn}
39     book["title"]=openBook["title"]
40     book["authors"]=""
41     if "authors" in openBook:
42         for v in openBook["authors"]:
43             book['authors'] += "; " + v['name']
44         book['authors'] = book['authors'][2:]
45     book["publisher"]=""
46     if "publishers" in openBook:
47         for v in openBook["publishers"]:
48             book["publisher"] += "; " + v['name']
49         book['publisher'] = book['publisher'][2:]
50     if "publish_places" in openBook:
51         book["publish location"]=""
52         for v in openBook["publish_places"]:
53             book["publish location"] += "; " + v['name']
54         book['publish location'] = book['publish location'][2:]
55
56     # for lccn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
57     if "lccn" in openBook['identifiers']:
58         book["lccn"]=int(openBook['identifiers']['lccn'][0])
59     if "publish_date" in openBook:
60         book['publish date']=openBook['publish_date']
61         #code to pull out year and month (hopefully)
62     if "number_of_pages" in openBook:
63         book["pages"]=openBook["number_of_pages"]
64     if "pagination" in openBook:
65         book["pagination"]=openBook["pagination"]
66     if "weight" in openBook:
67         book["weight"]=openBook["weight"]
68     if "subtitle" in openBook:
69         book["subtitle"]=openBook["subtitle"]
70     return book
71