openmedialibrary_platform/Shared/lib/python2.7/site-packages/ox/web/openlibrary.py

32 lines
946 B
Python

from ox.cache import read_url
from ox.utils import json
def find(query):
url = 'http://openlibrary.org/search.json?q=%s' % query
print url
data = json.loads(read_url(url))
return data
def authors_ol(authors):
r = []
for a in authors:
url = 'http://openlibrary.org%s.json' % a
data = json.loads(read_url(url))
r.append(data['name'])
return r
def get_data(isbn):
data = {}
ol = find(isbn)
if ol['docs']:
d = ol['docs'][0]
data['title'] = d['title']
data['author'] = authors_ol(d['authors'])
data['work'] = d['key']
data['edition'] = d['edition_key'][0]
url = 'https://openlibrary.org/books/%s.json' % data['edition']
info = json.load(read_url(url))
data['pages'] = info['number_of_pages']
if 'dewey_decimal_class' in info:
data['classification'] = info['dewey_decimal_class'][0]
return data