Added an exception for timeout
[public/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?format=json&jscmd=data&bibkeys=ISBN:"+isbn, timeout=3)
41     except URLError as e:
42         return {'title':e}
43     except timeout:
44         return {'title':'Timeout while connecting to OpenLibrary.org'}
45     openBook = loads(jsondata.read().decode('utf-8'))
46     if "ISBN:"+isbn not in openBook:
47         return {'isbn':isbn,'title':'Book not found'}
48     openBook = openBook["ISBN:"+isbn]
49     # create my custom dict for books with the info we want.
50     book = dict({"isbn" : isbn})
51     book["title"]=openBook["title"]
52     book["authors"]=""
53     if "authors" in openBook:
54         for v in openBook["authors"]:
55             book['authors'] += "; " + v['name']
56         book['authors'] = book['authors'][2:]
57     book["publisher"]=""
58     if "publishers" in openBook:
59         for v in openBook["publishers"]:
60             book["publisher"] += "; " + v['name']
61         book['publisher'] = book['publisher'][2:]
62     if "publish_places" in openBook:
63         book["publish location"]=""
64         for v in openBook["publish_places"]:
65             book["publish location"] += "; " + v['name']
66         book['publish location'] = book['publish location'][2:]
67
68     # for lccn, there maybe be multiple values in the query. I'm just taking 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?format=json&jscmd=data&bibkeys=lccn:"+lccn, timeout=3)
89     except URLError:
90         return {}
91     openBook = loads(jsondata.read().decode('utf-8'))
92     if "lccn:"+lccn not in openBook:
93         return {'lccn':lccn,'title':'Book not found'}
94     openBook = openBook["lccn:"+lccn]
95     # create my custom dict for books with the info we want.
96     book = {"lccn" : lccn}
97     book["title"]=openBook["title"]
98     book["authors"]=""
99     if "authors" in openBook:
100         for v in openBook["authors"]:
101             book['authors'] += "; " + v['name']
102         book['authors'] = book['authors'][2:]
103     book["publisher"]=""
104     if "publishers" in openBook:
105         for v in openBook["publishers"]:
106             book["publisher"] += "; " + v['name']
107         book['publisher'] = book['publisher'][2:]
108     if "publish_places" in openBook:
109         book["publish location"]=""
110         for v in openBook["publish_places"]:
111             book["publish location"] += "; " + v['name']
112         book['publish location'] = book['publish location'][2:]
113
114     # for isbn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
115     # There are also ISBN's that have non-number values :(
116     if "isbn_10" in openBook['identifiers']:
117         book["isbn"]=openBook['identifiers']['isbn_10'][0]
118     if "isbn_13" in openBook['identifiers']:
119         book["isbn"]=openBook['identifiers']['isbn_13'][0]
120     if "publish_date" in openBook:
121         book['publish date']=openBook['publish_date']
122         #code to pull out year and month (hopefully)
123     if "number_of_pages" in openBook:
124         book["pages"]=openBook["number_of_pages"]
125     if "pagination" in openBook:
126         book["pagination"]=openBook["pagination"]
127     if "weight" in openBook:
128         book["weight"]=openBook["weight"]
129     if "subtitle" in openBook:
130         book["subtitle"]=openBook["subtitle"]
131     return book
132