-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenlibrary.py
More file actions
211 lines (172 loc) · 5.77 KB
/
openlibrary.py
File metadata and controls
211 lines (172 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''A Python wrapper for the OpenLibrary API.'''
import urllib2
import simplejson
__author__ = "Felipe Borges <felipe10borges@gmail.com>"
class Publisher(object):
'''A class represeting a Book Publisher.
'''
def __init__(self,
name = None):
self.name = name
def get_name(self):
'''Get the Publisher name.
Returns:
The Publisher name.
'''
return self.name
@staticmethod
def get_publisher_from_json_dict(data):
'''Creates a new Publisher instance based on a JSON dict.
Args:
data: a Json dictionary
Returns:
A Publisher instance
'''
return Publisher(data.get('name'))
def __str__(self):
'''String representation of a Publisher.
Returns:
A string representation of a Publisher.
'''
return self.name
class Author(object):
'''A class representing a Book Author.
'''
def __init__(self,
name = None,
url = None):
self.name = name
self.url = url
def get_name(self):
'''Get the author name.
Returns:
The author name.
'''
return self.name
def get_url(self):
'''Get the author url.
Returns:
The author URL.
'''
return self.url
@staticmethod
def get_author_from_json_dict(data):
'''Creates a new Author instance based on a JSON dict.
Args:
data: a Json dictionary
Returns:
An Author instance
'''
return Author(data.get('name', None),
data.get('url', None))
def __str__(self):
return self.name
class Book(object):
'''A class representing an OpenLibrary Book entry.
'''
def __init__(self,
publishers = None,
identifiers = None,
classifications = None,
links = None,
weight = None,
title = None,
subtitle = None,
url = None,
number_of_pages = None,
cover = None,
subjects = None,
publish_date = None,
excerpts = None,
authors = None,
publish_places = None):
self.publishers = publishers
self.identifiers = identifiers
self.classifications = classifications
self.links = links
self.weight = weight
self.title = title
self.subtitle = subtitle
self.url = url
self.number_of_pages = number_of_pages
self.cover = cover
self.subjects = subjects
self.publish_date = publish_date
self.excerpts = excerpts
self.authors = authors
self.publish_places = publish_places
def get_title(self):
'''Get the title of this Book.
Returns:
The Title of this Book.
'''
return self.title
def get_authors(self):
'''Get the list of authors of this book.
Returns:
The list of authors of this book
'''
return self.authors
def get_publishers(self):
'''Get a list of publishers of this book.
Returns:
A list of Publishers of this book.
'''
return self.publishers
def get_number_of_pages(self):
'''Get the number of pages of the Book.
Returns:
The number of pages of the Book.
'''
return self.number_of_pages
def get_book_cover(self):
'''Get the Book cover.
Returns:
The Book cover.
'''
return self.cover
@staticmethod
def get_book_from_json_dict(data):
'''Creates a new Book instance based on a JSON dict.
Args:
data: a Json dictionary
Returns:
A Book instance
'''
publishers = [Publisher.get_publisher_from_json_dict(p) for p in data['publishers']]
authors = [Author.get_author_from_json_dict(a) for a in data['authors']]
return Book(publishers = publishers,
identifiers = data.get('identifiers', None),
classifications = data.get('classifications', None),
links = data.get('links', None),
weight = data.get('weight', None),
title = data.get('title', None),
subtitle = data.get('subtitle', None),
url = data.get('url', None),
number_of_pages = data.get('number_of_pages', None),
cover = data.get('cover', None),
subjects = data.get('subjects', None),
publish_date = data.get('publish_date', None),
excerpts = data.get('excerpts', None),
authors = authors,
publish_places = data.get('publish_places', None))
def __str__(self):
'''A string representation of this Book instance.
Returns:
A string representation of this Book instance.
'''
return self.title
class Api:
def __init__(self):
self.base_url = "http://openlibrary.org/api/books?bibkeys="
def get_book(self, id, bibkey = "ISBN"):
'''Get a Book by its ID: ISBN (default), LCCN, OCLC
or OLID (Open Library ID).
Returns:
A Book instance.
'''
url = urllib2.urlopen(self.base_url+bibkey+":%s&jscmd=data&format=json" % id)
data = simplejson.load(url)['%s:%s' % (bibkey, id)]
return Book.get_book_from_json_dict(data)