2014-05-14 11:57:11 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2014-09-03 00:32:44 +02:00
|
|
|
|
2014-05-14 11:57:11 +02:00
|
|
|
|
2014-05-19 01:24:04 +02:00
|
|
|
import stdnum.isbn
|
2014-05-21 02:02:21 +02:00
|
|
|
import ox
|
2014-05-17 16:26:59 +02:00
|
|
|
|
2014-09-03 00:32:44 +02:00
|
|
|
from . import google
|
|
|
|
from . import duckduckgo
|
2014-05-14 11:57:11 +02:00
|
|
|
|
2016-01-08 15:52:07 +05:30
|
|
|
from .utils import decode_html_data
|
|
|
|
|
2016-01-05 21:44:57 +05:30
|
|
|
from oml import settings
|
|
|
|
|
2014-05-19 01:24:04 +02:00
|
|
|
import logging
|
2015-11-29 15:56:38 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-05-19 01:24:04 +02:00
|
|
|
|
|
|
|
|
2016-02-03 01:00:40 +05:30
|
|
|
def find(title=None, author=None):
|
|
|
|
results = google.find(title=title, author=author)
|
2016-07-17 13:07:25 +02:00
|
|
|
# results = duckduckgo.find(query)
|
2014-05-16 10:06:11 +02:00
|
|
|
'''
|
2014-05-21 02:02:21 +02:00
|
|
|
results = openlibrary.find(query)
|
2014-05-14 20:46:31 +02:00
|
|
|
for r in results:
|
2014-05-21 02:02:21 +02:00
|
|
|
r['primaryid'] = 'olid'
|
2014-05-16 10:06:11 +02:00
|
|
|
'''
|
2014-05-14 20:46:31 +02:00
|
|
|
return results
|
2014-05-14 11:57:11 +02:00
|
|
|
|
2016-07-17 13:07:25 +02:00
|
|
|
def lookup_isbn(value):
|
|
|
|
if not isvalid_id('isbn', value):
|
2014-05-19 01:24:04 +02:00
|
|
|
return {}
|
2016-07-17 13:07:25 +02:00
|
|
|
try:
|
|
|
|
data = google.info(value)
|
|
|
|
except:
|
2019-01-10 21:52:49 +05:30
|
|
|
logger.debug('google.info failed for %s', value, exc_info=True)
|
2016-07-17 13:07:25 +02:00
|
|
|
data = {}
|
2016-01-05 21:44:57 +05:30
|
|
|
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 15:52:07 +05:30
|
|
|
data = decode_html_data(data)
|
2014-05-14 11:57:11 +02:00
|
|
|
return data
|
|
|
|
|
2014-05-19 01:24:04 +02:00
|
|
|
def isvalid_id(key, value):
|
2014-05-21 02:02:21 +02:00
|
|
|
if key == 'isbn':
|
|
|
|
if len(value) not in (10, 13) or not stdnum.isbn.is_valid(value):
|
2014-05-19 01:24:04 +02: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
|