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