openmedialibrary/oml/meta/__init__.py

51 lines
1.2 KiB
Python
Raw Normal View History

2014-05-14 09:57:11 +00:00
# -*- coding: utf-8 -*-
2014-09-02 22:32:44 +00:00
2014-05-14 09:57:11 +00:00
2014-05-18 23:24:04 +00:00
import stdnum.isbn
2014-05-21 00:02:21 +00:00
import ox
2014-05-17 14:26:59 +00:00
2014-09-02 22:32:44 +00:00
from . import google
from . import duckduckgo
2014-05-14 09:57:11 +00:00
2016-01-08 10:22:07 +00:00
from .utils import decode_html_data
2016-01-05 16:14:57 +00:00
from oml import settings
2014-05-18 23:24:04 +00:00
import logging
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
2014-05-18 23:24:04 +00:00
2016-02-02 19:30:40 +00:00
def find(title=None, author=None):
results = google.find(title=title, author=author)
# results = duckduckgo.find(query)
2014-05-16 08:06:11 +00:00
'''
2014-05-21 00:02:21 +00:00
results = openlibrary.find(query)
2014-05-14 18:46:31 +00:00
for r in results:
2014-05-21 00:02:21 +00:00
r['primaryid'] = 'olid'
2014-05-16 08:06:11 +00:00
'''
2014-05-14 18:46:31 +00:00
return results
2014-05-14 09:57:11 +00:00
def lookup_isbn(value):
if not isvalid_id('isbn', value):
2014-05-18 23:24:04 +00:00
return {}
try:
data = google.info(value)
except:
2019-01-10 16:22:49 +00:00
logger.debug('google.info failed for %s', value, exc_info=True)
data = {}
2016-01-05 16:14:57 +00:00
for key in [k['id'] for k in settings.config['itemKeys'] if isinstance(k['type'], list)]:
if key in data and not isinstance(data[key], list):
data[key] = [data[key]]
2016-01-08 10:22:07 +00:00
data = decode_html_data(data)
2014-05-14 09:57:11 +00:00
return data
2014-05-18 23:24:04 +00:00
def isvalid_id(key, value):
2014-05-21 00:02:21 +00:00
if key == 'isbn':
if len(value) not in (10, 13) or not stdnum.isbn.is_valid(value):
2014-05-18 23:24:04 +00:00
return False
if key == 'asin' and len(value) != 10:
return False
if key == 'olid' and not (value.startswith('OL') and value.endswith('M')):
return False
return True