Added permission checking. Added unit tests for the db_layer and permissions.
[library/.git] / book_data.py
1 try:
2         # For Python 3.0 and later
3             from urllib.request import urlopen,URLError
4 except ImportError:
5         # Fall back to Python 2's urllib2
6             from urllib2 import urlopen,URLError
7 from json import loads,dumps
8 import sys
9
10 """ Library Book Type Description:
11 The book is a dictionary of the form { string : a, ... }
12
13 Keys:
14   required: (ideally)
15     title - Book/Article title
16     publisher - string containing semi-colon separated list eg. "UW Press; CSC, inc."
17     authors - as above. each name is of the form "First Initials. Last" eg. "Calum T. Dalek; Conan T.B. Ladan"
18   optional:
19     subtitle - string
20     edition - integer
21     isbn - integer (it's preferred to use the isbn-13 rather than isbn-10)
22     lccn - integer: library of congress catalogue number
23     publish date - string of date (to make things easier to code/catalogue (won't be stored)
24     publish year - int (this kind of thing will have to be confirmed by cataloguer)
25     publish month - int
26     publish location - like publisher
27     
28     pages - integer - just the number of pages
29     pagination - string eg. "xviii, 1327-1850"
30     weight - string (purely for interest's sake eg. "3lb." or "3 pounds"
31     categories - list of strings?
32 """
33
34
35 # look up data from openlibrary.org using isbn
36 def openLibrary_isbn(ISBN):
37     isbn = str(ISBN)
38     try:
39         jsondata = urlopen("http://openlibrary.org/api/books"
40                            "?format=json&jscmd=data&bibkeys=ISBN:"+isbn,
41                            timeout=3)
42     except URLError:
43         return {}
44     openBook = loads(jsondata.read().decode('utf-8'))
45     if "ISBN:"+isbn not in openBook:
46         return {'isbn':isbn,'title':'Book not found'}
47     openBook = openBook["ISBN:"+isbn]
48     # create my custom dict for books with the info we want.
49     book = dict({"isbn" : isbn})
50     book["title"]=openBook["title"]
51     book["authors"]=""
52     if "authors" in openBook:
53         for v in openBook["authors"]:
54             book['authors'] += "; " + v['name']
55         book['authors'] = book['authors'][2:]
56     book["publisher"]=""
57     if "publishers" in openBook:
58         for v in openBook["publishers"]:
59             book["publisher"] += "; " + v['name']
60         book['publisher'] = book['publisher'][2:]
61     if "publish_places" in openBook:
62         book["publish location"]=""
63         for v in openBook["publish_places"]:
64             book["publish location"] += "; " + v['name']
65         book['publish location'] = book['publish location'][2:]
66
67     # for lccn, there maybe be multiple values in the query. I'm just taking
68     # the first, but the full list may be useful
69     if "lccn" in openBook['identifiers']:
70         book["lccn"]=int(openBook['identifiers']['lccn'][0])
71     if "publish_date" in openBook:
72         book['publish date']=openBook['publish_date']
73         #code to pull out year and month (hopefully)
74     if "number_of_pages" in openBook:
75         book["pages"]=openBook["number_of_pages"]
76     if "pagination" in openBook:
77         book["pagination"]=openBook["pagination"]
78     if "weight" in openBook:
79         book["weight"]=openBook["weight"]
80     if "subtitle" in openBook:
81         book["subtitle"]=openBook["subtitle"]
82     return book
83
84 # look up data from openlibrary.org using lccn
85 def openLibrary_lccn(LCCN):
86     lccn = str(LCCN)
87     try:
88         jsondata = urlopen("http://openlibrary.org/api/books"
89                            "?format=json&jscmd=data&bibkeys=lccn:"+lccn,
90                            timeout=3)
91     except URLError:
92         return {}
93     openBook = loads(jsondata.read().decode('utf-8'))
94     if "lccn:"+lccn not in openBook:
95         return {'lccn':lccn,'title':'Book not found'}
96     openBook = openBook["lccn:"+lccn]
97     # create my custom dict for books with the info we want.
98     book = {"lccn" : lccn}
99     book["title"]=openBook["title"]
100     book["authors"]=""
101     if "authors" in openBook:
102         for v in openBook["authors"]:
103             book['authors'] += "; " + v['name']
104         book['authors'] = book['authors'][2:]
105     book["publisher"]=""
106     if "publishers" in openBook:
107         for v in openBook["publishers"]:
108             book["publisher"] += "; " + v['name']
109         book['publisher'] = book['publisher'][2:]
110     if "publish_places" in openBook:
111         book["publish location"]=""
112         for v in openBook["publish_places"]:
113             book["publish location"] += "; " + v['name']
114         book['publish location'] = book['publish location'][2:]
115
116     # for isbn, there maybe be multiple values in the query. I'm just taking
117     # the first, but the full list may be useful
118     # There are also ISBN's that have non-number values :(
119     if "isbn_10" in openBook['identifiers']:
120         book["isbn"]=openBook['identifiers']['isbn_10'][0]
121     if "isbn_13" in openBook['identifiers']:
122         book["isbn"]=openBook['identifiers']['isbn_13'][0]
123     if "publish_date" in openBook:
124         book['publish date']=openBook['publish_date']
125         #code to pull out year and month (hopefully)
126     if "number_of_pages" in openBook:
127         book["pages"]=openBook["number_of_pages"]
128     if "pagination" in openBook:
129         book["pagination"]=openBook["pagination"]
130     if "weight" in openBook:
131         book["weight"]=openBook["weight"]
132     if "subtitle" in openBook:
133         book["subtitle"]=openBook["subtitle"]
134     return book
135