2014-05-14 09:57:11 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
from __future__ import division
|
|
|
|
|
2014-05-18 23:24:04 +00:00
|
|
|
import stdnum.isbn
|
2014-05-17 14:26:59 +00:00
|
|
|
|
2014-05-14 09:57:11 +00:00
|
|
|
import abebooks
|
|
|
|
import loc
|
|
|
|
import lookupbyisbn
|
|
|
|
import openlibrary
|
|
|
|
import worldcat
|
2014-05-16 08:06:11 +00:00
|
|
|
import google
|
2014-05-17 09:19:32 +00:00
|
|
|
import duckduckgo
|
2014-05-14 09:57:11 +00:00
|
|
|
|
2014-05-18 23:24:04 +00:00
|
|
|
import logging
|
|
|
|
logger = logging.getLogger('meta')
|
|
|
|
|
|
|
|
|
2014-05-14 09:57:11 +00:00
|
|
|
providers = [
|
|
|
|
('openlibrary', 'olid'),
|
|
|
|
('loc', 'lccn'),
|
|
|
|
('worldcat', 'oclc'),
|
|
|
|
('lookupbyisbn', 'asin'),
|
|
|
|
('abebooks', 'isbn10')
|
|
|
|
]
|
|
|
|
|
2014-05-19 20:58:00 +00:00
|
|
|
def find(**kargs):
|
|
|
|
title = kargs.get('title')
|
|
|
|
author = kargs.get('author')
|
|
|
|
publisher = kargs.get('publisher')
|
|
|
|
date = kargs.get('date')
|
2014-05-17 09:19:32 +00:00
|
|
|
#results = google.find(title=title, author=author, publisher=publisher, date=date)
|
|
|
|
results = duckduckgo.find(title=title, author=author, publisher=publisher, date=date)
|
2014-05-16 08:06:11 +00:00
|
|
|
'''
|
2014-05-14 18:46:31 +00:00
|
|
|
results = openlibrary.find(title=title, author=author, publisher=publisher, date=date)
|
|
|
|
for r in results:
|
|
|
|
r['mainid'] = '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(key, value):
|
2014-05-18 23:24:04 +00:00
|
|
|
if not isvalid_id(key, value):
|
|
|
|
return {}
|
2014-05-14 09:57:11 +00:00
|
|
|
data = {key: value}
|
|
|
|
ids = [(key, value)]
|
|
|
|
provider_data = {}
|
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
done = True
|
|
|
|
for provider, id in providers:
|
|
|
|
for key, value in ids:
|
|
|
|
for kv in globals()[provider].get_ids(key, value):
|
|
|
|
if not kv in ids:
|
|
|
|
ids.append(kv)
|
|
|
|
done = False
|
2014-05-17 15:16:18 +00:00
|
|
|
logger.debug('lookup %s=%s => %s', ids[0][0], ids[0][1], ids)
|
2014-05-14 09:57:11 +00:00
|
|
|
for k, v in ids:
|
|
|
|
for provider, id in providers:
|
2014-05-14 18:46:31 +00:00
|
|
|
if id == k and provider not in provider_data:
|
2014-05-14 09:57:11 +00:00
|
|
|
provider_data[provider] = globals()[provider].lookup(v)
|
|
|
|
for provider in sorted(
|
|
|
|
provider_data.keys(),
|
|
|
|
key=lambda x: -len(provider_data[x])
|
|
|
|
):
|
2014-05-17 14:26:59 +00:00
|
|
|
logger.debug('%s %s %s', provider, len(provider_data[provider]), provider_data[provider].keys())
|
2014-05-14 09:57:11 +00:00
|
|
|
for k_, v_ in provider_data[provider].iteritems():
|
|
|
|
if not k_ in data:
|
|
|
|
data[k_] = v_
|
|
|
|
return data
|
|
|
|
|
2014-05-18 23:24:04 +00:00
|
|
|
def isvalid_id(key, value):
|
|
|
|
if key in ('isbn10', 'isbn13'):
|
|
|
|
if 'isbn%d'%len(value) != key or not stdnum.isbn.is_valid(value):
|
|
|
|
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
|
2014-05-14 09:57:11 +00:00
|
|
|
|