openmedialibrary/oml/meta/google.py

96 lines
3.1 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from ox.cache import get_json, store
import ox.web.google
import stdnum.isbn
from .utils import find_isbns, get_language, decode_html_data, to_isbn13
import logging
logger = logging.getLogger(__name__)
def find(query):
logger.debug('find %s', query)
query += ' isbn'
isbns = []
for r in ox.web.google.find(query):
isbns += find_isbns(' '.join(r))
logger.debug('isbns', isbns)
results = []
done = set()
for isbn in isbns:
if isbn not in done:
r = {
'isbn': isbn,
'primaryid': ['isbn', isbn]
}
results.append(r)
done.add(isbn)
if len(isbn) == 10:
done.add(stdnum.isbn.to_isbn13(isbn))
if len(isbn) == 13 and isbn.startswith('978'):
done.add(stdnum.isbn.to_isbn10(isbn))
return results
def info(key, value):
if key not in ('isbn', 'lccn', 'oclc'):
raise IOError('unknwon key %s' % key)
url = 'https://www.googleapis.com/books/v1/volumes?q=%s:%s' % (key, value)
r = get_json(url, timeout=-1)
if 'error' in r:
store.delete(url)
raise IOError(url, r)
if not 'items' in r:
print('unknown %s: %s [%s]' % (key, value, r))
return {}
_data = r['items'][0]['volumeInfo']
_id = r['items'][0]['id']
data = {}
for key in [
'authors',
'categories',
'description',
'pageCount',
'publishedDate',
'publisher',
'title',
]:
if key in _data:
data[{
'authors': 'author',
'pageCount': 'pages',
'publishedDate': 'date',
}.get(key,key)] = _data[key]
if 'subtitle' in _data and _data['subtitle'].strip():
data['title'] = '{title}: {subtitle}'.format(**_data)
if r['items'][0]['accessInfo']['viewability'] != 'NO_PAGES':
#data['cover'] = 'https://books.google.com/books?id=%s&pg=PP1&img=1&zoom=0&hl=en' % _id
data['cover'] = 'https://books.google.com/books/content/images/frontcover/%s?fife=w600-rw' % _id
elif 'imageLinks' in _data:
for size in ('extraLarge', 'large', 'medium', 'small', 'thumbnail', 'smallThumbnail'):
if size in _data['imageLinks']:
data['cover'] = _data['imageLinks'][size]
break
if 'industryIdentifiers' in _data:
for k in _data['industryIdentifiers']:
if k['type'].startswith('ISBN'):
if not 'isbn' in data:
data['isbn'] = []
data['isbn'].append(k['identifier'])
else:
print('unknown identifier', k)
if 'isbn' in data:
data['isbn'] = [to_isbn13(i) for i in data['isbn']][0]
if 'publisher' in data and isinstance(data['publisher'], str):
data['publisher'] = [data['publisher']]
if 'language' in _data:
data['language'] = [get_language(_data['language'])]
data = decode_html_data(data)
return data