moviewyrm/bookwyrm/connectors/connector_manager.py

97 lines
3.1 KiB
Python
Raw Normal View History

2021-01-02 16:14:28 +00:00
''' interface with whatever connectors the app has '''
import importlib
from urllib.parse import urlparse
from requests import HTTPError
from bookwyrm import models
2021-01-02 16:14:28 +00:00
from bookwyrm.tasks import app
2020-03-07 20:22:28 +00:00
2021-01-02 16:14:28 +00:00
class ConnectorException(HTTPError):
''' when the connector can't do what was asked '''
2020-10-29 22:29:23 +00:00
def search(query, min_confidence=0.1):
''' find books based on arbitary keywords '''
results = []
2020-05-03 20:32:23 +00:00
dedup_slug = lambda r: '%s/%s/%s' % (r.title, r.author, r.year)
2020-05-03 22:26:47 +00:00
result_index = set()
for connector in get_connectors():
try:
2020-10-29 22:29:23 +00:00
result_set = connector.search(query, min_confidence=min_confidence)
except (HTTPError, ConnectorException):
continue
2020-05-03 20:32:23 +00:00
2020-05-03 22:26:47 +00:00
result_set = [r for r in result_set \
if dedup_slug(r) not in result_index]
# `|=` concats two sets. WE ARE GETTING FANCY HERE
result_index |= set(dedup_slug(r) for r in result_set)
results.append({
'connector': connector,
2020-05-03 22:26:47 +00:00
'results': result_set,
})
2020-04-29 17:57:20 +00:00
return results
2021-01-31 19:11:26 +00:00
def local_search(query, min_confidence=0.1, raw=False):
2020-05-04 17:15:41 +00:00
''' only look at local search results '''
connector = load_connector(models.Connector.objects.get(local=True))
2021-01-31 19:11:26 +00:00
return connector.search(query, min_confidence=min_confidence, raw=raw)
2020-05-04 17:15:41 +00:00
2020-10-29 22:29:23 +00:00
def first_search_result(query, min_confidence=0.1):
2020-05-03 22:26:47 +00:00
''' search until you find a result that fits '''
for connector in get_connectors():
2020-10-29 22:29:23 +00:00
result = connector.search(query, min_confidence=min_confidence)
2020-05-03 22:26:47 +00:00
if result:
return result[0]
return None
def get_connectors():
''' load all connectors '''
2020-05-12 17:01:36 +00:00
for info in models.Connector.objects.order_by('priority').all():
yield load_connector(info)
2021-01-02 16:14:28 +00:00
def get_or_create_connector(remote_id):
''' get the connector related to the author's server '''
url = urlparse(remote_id)
identifier = url.netloc
if not identifier:
raise ValueError('Invalid remote id')
try:
connector_info = models.Connector.objects.get(identifier=identifier)
except models.Connector.DoesNotExist:
connector_info = models.Connector.objects.create(
identifier=identifier,
connector_file='bookwyrm_connector',
base_url='https://%s' % identifier,
books_url='https://%s/book' % identifier,
covers_url='https://%s/images/covers' % identifier,
search_url='https://%s/search?q=' % identifier,
priority=2
)
return load_connector(connector_info)
@app.task
def load_more_data(connector_id, book_id):
''' background the work of getting all 10,000 editions of LoTR '''
connector_info = models.Connector.objects.get(id=connector_id)
connector = load_connector(connector_info)
book = models.Book.objects.select_subclasses().get(id=book_id)
connector.expand_book_data(book)
def load_connector(connector_info):
''' instantiate the connector class '''
connector = importlib.import_module(
'bookwyrm.connectors.%s' % connector_info.connector_file
)
return connector.Connector(connector_info.identifier)