2014-05-04 17:26:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
2014-05-14 18:46:31 +00:00
|
|
|
from urllib import urlencode
|
2014-05-04 17:26:43 +00:00
|
|
|
from ox.cache import read_url
|
|
|
|
import json
|
|
|
|
|
|
|
|
from marc_countries import COUNTRIES
|
2014-05-14 09:57:11 +00:00
|
|
|
from utils import normalize_isbn
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-17 14:26:59 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger('meta.openlibrary')
|
|
|
|
|
2014-05-14 18:46:31 +00:00
|
|
|
KEYS = {
|
|
|
|
'authors': 'author',
|
|
|
|
'covers': 'cover',
|
|
|
|
'dewey_decimal_class': 'classification',
|
|
|
|
'isbn_10': 'isbn10',
|
|
|
|
'isbn_13': 'isbn13',
|
|
|
|
'languages': 'language',
|
|
|
|
'lccn': 'lccn',
|
|
|
|
'number_of_pages': 'pages',
|
|
|
|
'oclc_numbers': 'oclc',
|
|
|
|
'publish_country': 'country',
|
|
|
|
'publish_date': 'date',
|
|
|
|
'publishers': 'publisher',
|
|
|
|
'publish_places': 'place',
|
|
|
|
'series': 'series',
|
|
|
|
'title': 'title',
|
|
|
|
}
|
|
|
|
|
|
|
|
def find(*args, **kargs):
|
|
|
|
args = [a.replace(':', ' ') for a in args]
|
|
|
|
for k in ('date', 'publisher'):
|
|
|
|
if k in kargs:
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('ignoring %s on openlibrary %s', k, kargs[k])
|
2014-05-14 18:46:31 +00:00
|
|
|
del kargs[k]
|
|
|
|
for k, v in kargs.iteritems():
|
|
|
|
key = KEYS.keys()[KEYS.values().index(k)]
|
|
|
|
if v:
|
|
|
|
if not isinstance(v, list):
|
|
|
|
v = [v]
|
|
|
|
#v = ['%s:"%s"' % (key, value.replace(':', '\:')) for value in v]
|
|
|
|
v = ['"%s"' % value.replace(':', ' ') for value in v]
|
|
|
|
args += v
|
|
|
|
query = ' '.join(args)
|
|
|
|
query = query.strip()
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('find %s', query)
|
2014-05-14 18:46:31 +00:00
|
|
|
r = api.search(query)
|
|
|
|
results = []
|
|
|
|
ids = [b for b in r.get('result', []) if b.startswith('/books')]
|
|
|
|
books = api.get_many(ids).get('result', [])
|
|
|
|
for olid, value in books.iteritems():
|
|
|
|
olid = olid.split('/')[-1]
|
|
|
|
book = format(value)
|
|
|
|
book['olid'] = olid
|
|
|
|
results.append(book)
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2014-05-14 09:57:11 +00:00
|
|
|
def get_ids(key, value):
|
|
|
|
ids = []
|
|
|
|
if key == 'olid':
|
|
|
|
data = lookup(value, True)
|
|
|
|
for id in ('isbn10', 'isbn13', 'lccn', 'oclc'):
|
|
|
|
if id in data:
|
|
|
|
for v in data[id]:
|
|
|
|
if (id, v) not in ids:
|
|
|
|
ids.append((id, v))
|
2014-05-14 18:46:31 +00:00
|
|
|
elif key in ('isbn10', 'isbn13', 'oclc', 'lccn'):
|
2014-05-19 01:36:37 +00:00
|
|
|
logger.debug('get_ids %s %s', key, value)
|
2014-05-14 18:46:31 +00:00
|
|
|
r = api.things({'type': '/type/edition', key.replace('isbn', 'isbn_'): value})
|
|
|
|
for b in r.get('result', []):
|
|
|
|
if b.startswith('/books'):
|
|
|
|
olid = b.split('/')[-1]
|
|
|
|
for kv in [('olid', olid)] + get_ids('olid', olid):
|
2014-05-14 09:57:11 +00:00
|
|
|
if kv not in ids:
|
|
|
|
ids.append(kv)
|
|
|
|
if ids:
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('get_ids %s %s => %s', key, value, ids)
|
2014-05-14 09:57:11 +00:00
|
|
|
return ids
|
2014-05-04 17:26:43 +00:00
|
|
|
|
2014-05-14 09:57:11 +00:00
|
|
|
def lookup(id, return_all=False):
|
|
|
|
#print 'openlibrary.lookup', id
|
2014-05-14 18:46:31 +00:00
|
|
|
info = api.get('/books/' + id).get('result', {})
|
|
|
|
#url = 'https://openlibrary.org/books/%s.json' % id
|
|
|
|
#info = json.loads(read_url(url))
|
|
|
|
data = format(info, return_all)
|
|
|
|
data['olid'] = id
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('lookup %s => %s', id, data.keys())
|
2014-05-14 18:46:31 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def format(info, return_all=False):
|
|
|
|
data = {}
|
|
|
|
for key in KEYS:
|
2014-05-04 17:26:43 +00:00
|
|
|
if key in info:
|
|
|
|
value = info[key]
|
|
|
|
if key == 'authors':
|
2014-05-14 18:46:31 +00:00
|
|
|
value = resolve_names(value)
|
2014-05-04 17:26:43 +00:00
|
|
|
elif key == 'publish_country':
|
2014-05-14 18:46:31 +00:00
|
|
|
value = value.strip()
|
2014-05-04 17:26:43 +00:00
|
|
|
value = COUNTRIES.get(value, value)
|
|
|
|
elif key == 'covers':
|
|
|
|
value = 'https://covers.openlibrary.org/b/id/%s.jpg' % value[0]
|
|
|
|
elif key == 'languages':
|
2014-05-14 18:46:31 +00:00
|
|
|
value = resolve_names(value)
|
2014-05-14 09:57:11 +00:00
|
|
|
elif not return_all and isinstance(value, list) and key not in ('publish_places'):
|
2014-05-04 17:26:43 +00:00
|
|
|
value = value[0]
|
|
|
|
if key in ('isbn_10', 'isbn_13'):
|
2014-05-14 09:57:11 +00:00
|
|
|
if isinstance(value, list):
|
|
|
|
value = map(normalize_isbn, value)
|
|
|
|
else:
|
|
|
|
value = normalize_isbn(value)
|
2014-05-14 18:46:31 +00:00
|
|
|
data[KEYS[key]] = value
|
2014-05-14 09:57:11 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def resolve_names(objects, key='name'):
|
|
|
|
r = []
|
2014-05-14 18:46:31 +00:00
|
|
|
data = api.get_many([k['key'] for k in objects]).get('result', {})
|
|
|
|
for k, value in data.iteritems():
|
|
|
|
if 'location' in value and value.get('type', {}).get('key') == '/type/redirect':
|
|
|
|
value = api.get(value['location']).get('result', {})
|
|
|
|
r.append(value[key])
|
2014-05-14 09:57:11 +00:00
|
|
|
return r
|
|
|
|
|
2014-05-14 18:46:31 +00:00
|
|
|
class API(object):
|
|
|
|
base = 'https://openlibrary.org/api'
|
|
|
|
|
|
|
|
def _request(self, action, data):
|
|
|
|
for key in data:
|
|
|
|
if not isinstance(data[key], basestring):
|
|
|
|
data[key] = json.dumps(data[key])
|
|
|
|
url = self.base + '/' + action + '?' + urlencode(data)
|
|
|
|
result = json.loads(read_url(url))
|
|
|
|
if 'status' in result and result['status'] == 'error' or 'error' in result:
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.info('FAILED %s %s', action, data)
|
|
|
|
logger.info('URL %s', url)
|
2014-05-14 18:46:31 +00:00
|
|
|
return result
|
|
|
|
|
|
|
|
def get(self, key):
|
|
|
|
data = self._request('get', {'key': key})
|
|
|
|
return data
|
|
|
|
|
|
|
|
def get_many(self, keys):
|
|
|
|
data = self._request('get_many', {'keys': keys})
|
|
|
|
return data
|
|
|
|
|
|
|
|
def search(self, query):
|
|
|
|
if isinstance(query, basestring):
|
|
|
|
query = {
|
|
|
|
'query': query
|
|
|
|
}
|
|
|
|
data = self._request('search', {'q': query})
|
|
|
|
if 'status' in data and data['status'] == 'error':
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.info('FAILED %s', query)
|
2014-05-14 18:46:31 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
def things(self, query):
|
|
|
|
data = self._request('things', {'query': query})
|
|
|
|
return data
|
2014-05-14 09:57:11 +00:00
|
|
|
|
2014-05-14 18:46:31 +00:00
|
|
|
api = API()
|