finishing fixing bugs from last commit
[public/library.git] / bookData.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?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
40     except URLError:
41         return {}
42     openBook = loads(jsondata.read().decode('utf-8'))
43     if "ISBN:"+isbn not in openBook:
44         return {'isbn':isbn,'title':'Book not found'}
45     openBook = openBook["ISBN:"+isbn]
46     # create my custom dict for books with the info we want.
47     book = dict({"isbn" : isbn})
48     book["title"]=openBook["title"]
49     book["authors"]=""
50     if "authors" in openBook:
51         for v in openBook["authors"]:
52             book['authors'] += "; " + v['name']
53         book['authors'] = book['authors'][2:]
54     book["publisher"]=""
55     if "publishers" in openBook:
56         for v in openBook["publishers"]:
57             book["publisher"] += "; " + v['name']
58         book['publisher'] = book['publisher'][2:]
59     if "publish_places" in openBook:
60         book["publish location"]=""
61         for v in openBook["publish_places"]:
62             book["publish location"] += "; " + v['name']
63         book['publish location'] = book['publish location'][2:]
64
65     # for lccn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
66     if "lccn" in openBook['identifiers']:
67         book["lccn"]=int(openBook['identifiers']['lccn'][0])
68     if "publish_date" in openBook:
69         book['publish date']=openBook['publish_date']
70         #code to pull out year and month (hopefully)
71     if "number_of_pages" in openBook:
72         book["pages"]=openBook["number_of_pages"]
73     if "pagination" in openBook:
74         book["pagination"]=openBook["pagination"]
75     if "weight" in openBook:
76         book["weight"]=openBook["weight"]
77     if "subtitle" in openBook:
78         book["subtitle"]=openBook["subtitle"]
79     return book
80
81 # look up data from openlibrary.org using lccn
82 def openLibrary_lccn(LCCN):
83     lccn = str(LCCN)
84     try:
85         jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=lccn:"+lccn, timeout=3)
86     except URLError:
87         return {}
88     openBook = loads(jsondata.read().decode('utf-8'))
89     if "lccn:"+lccn not in openBook:
90         return {'lccn':lccn,'title':'Book not found'}
91     openBook = openBook["lccn:"+lccn]
92     # create my custom dict for books with the info we want.
93     book = {"lccn" : lccn}
94     book["title"]=openBook["title"]
95     book["authors"]=""
96     if "authors" in openBook:
97         for v in openBook["authors"]:
98             book['authors'] += "; " + v['name']
99         book['authors'] = book['authors'][2:]
100     book["publisher"]=""
101     if "publishers" in openBook:
102         for v in openBook["publishers"]:
103             book["publisher"] += "; " + v['name']
104         book['publisher'] = book['publisher'][2:]
105     if "publish_places" in openBook:
106         book["publish location"]=""
107         for v in openBook["publish_places"]:
108             book["publish location"] += "; " + v['name']
109         book['publish location'] = book['publish location'][2:]
110
111     # for isbn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
112     # There are also ISBN's that have non-number values :(
113     if "isbn_10" in openBook['identifiers']:
114         book["isbn"]=openBook['identifiers']['isbn_10'][0]
115     if "isbn_13" in openBook['identifiers']:
116         book["isbn"]=openBook['identifiers']['isbn_13'][0]
117     if "publish_date" in openBook:
118         book['publish date']=openBook['publish_date']
119         #code to pull out year and month (hopefully)
120     if "number_of_pages" in openBook:
121         book["pages"]=openBook["number_of_pages"]
122     if "pagination" in openBook:
123         book["pagination"]=openBook["pagination"]
124     if "weight" in openBook:
125         book["weight"]=openBook["weight"]
126     if "subtitle" in openBook:
127         book["subtitle"]=openBook["subtitle"]
128     return book
129