browser window started
[public/library.git] / bookData.py
1 from urllib2 import urlopen
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     publishers - 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 locations - like publishers
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):
31     jsondata = urlopen("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:"+isbn)
32     openBook = load(jsondata)
33     if "ISBN:"+isbn not in openBook:
34         return openBook
35     openBook = openBook["ISBN:"+isbn]
36     # create my custom dict for books with the info we want.
37     book = {"isbn" : isbn}
38     book["title"]=openBook["title"]
39     book["authors"]=""
40     if "authors" in openBook:
41         for v in openBook["authors"]:
42             book['authors'] += "; " + v['name']
43         book['authors'] = book['authors'][2:]
44     book["publishers"]=""
45     if "publishers" in openBook:
46         for v in openBook["publishers"]:
47             book["publishers"] += "; " + v['name']
48         book['publishers'] = book['publishers'][2:]
49     if "publish_places" in openBook:
50         book["publish locations"]=""
51         for v in openBook["publish_places"]:
52             book["publish locations"] += "; " + v['name']
53         book['publish locations'] = book['publish locations'][2:]
54
55     # for lccn, there maybe be multiple values in the query. I'm just taking the first, but the full list may be useful
56     if "lccn" in openBook['identifiers']:
57         book["lccn"]=int(openBook['identifiers']['lccn'][0])
58     if "publish_date" in openBook:
59         book['publish date']=openBook['publish_date']
60         #code to pull out year and month (hopefully)
61     if "number_of_pages" in openBook:
62         book["pages"]=openBook["number_of_pages"]
63     if "pagination" in openBook:
64         book["pagination"]=openBook["pagination"]
65     if "weight" in openBook:
66         book["weight"]=openBook["weight"]
67     if "subtitle" in openBook:
68         book["subtitle"]=openBook["subtitle"]
69     return book
70
71 book = openLibrary("9780521714723")
72 print dumps(book, indent=2)
73 book = openLibrary("9780521565431")
74 print dumps(book, indent=2)
75 book = openLibrary("689145728392")
76 print dumps(book, indent=2)
77 book = openLibrary("9780321468932")
78 print dumps(book, indent=2)
79 book = openLibrary("9781555580414")
80 print dumps(book, indent=2)