b9231edbb62c19a992156599994e3a25b2e6aaba
[public/library.git] / bookData.py
1 from urllib2 import urlopen,URLError
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(ISBN):
31     isbn = str(ISBN)
32     try:
33         jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
34     except URLError:
35         return {}
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"]
43     book["authors"]=""
44     if "authors" in openBook:
45         for v in openBook["authors"]:
46             book['authors'] += "; " + v['name']
47         book['authors'] = book['authors'][2:]
48     book["publisher"]=""
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:]
58
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"]
73     return book
74
75 # look up data from openlibrary.org using lccn
76 def openLibrary_lccn(LCCN):
77     lccn = str(LCCN)
78     try:
79         jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=lccn:"+lccn, timeout=3)
80     except URLError:
81         return {}
82     openBook = load(jsondata)
83     if "lccn:"+lccn not in openBook:
84         return {'lccn':lccn,'title':'Book not found'}
85     openBook = openBook["lccn:"+lccn]
86     # create my custom dict for books with the info we want.
87     book = {"lccn" : lccn}
88     book["title"]=openBook["title"]
89     book["authors"]=""
90     if "authors" in openBook:
91         for v in openBook["authors"]:
92             book['authors'] += "; " + v['name']
93         book['authors'] = book['authors'][2:]
94     book["publisher"]=""
95     if "publishers" in openBook:
96         for v in openBook["publishers"]:
97             book["publisher"] += "; " + v['name']
98         book['publisher'] = book['publisher'][2:]
99     if "publish_places" in openBook:
100         book["publish location"]=""
101         for v in openBook["publish_places"]:
102             book["publish location"] += "; " + v['name']
103         book['publish location'] = book['publish location'][2:]
104
105     # for isbn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
106     if "isbn_10" in openBook['identifiers']:
107         book["isbn"]=int(openBook['identifiers']['isbn_10'][0])
108     if "isbn_13" in openBook['identifiers']:
109         book["isbn"]=int(openBook['identifiers']['isbn_13'][0])
110     if "publish_date" in openBook:
111         book['publish date']=openBook['publish_date']
112         #code to pull out year and month (hopefully)
113     if "number_of_pages" in openBook:
114         book["pages"]=openBook["number_of_pages"]
115     if "pagination" in openBook:
116         book["pagination"]=openBook["pagination"]
117     if "weight" in openBook:
118         book["weight"]=openBook["weight"]
119     if "subtitle" in openBook:
120         book["subtitle"]=openBook["subtitle"]
121     return book
122