2020-03-07 20:22:28 +00:00
|
|
|
''' select and call a connector for whatever book task needs doing '''
|
2020-03-27 22:25:08 +00:00
|
|
|
import importlib
|
|
|
|
|
|
|
|
from fedireads import models
|
2020-04-02 05:11:31 +00:00
|
|
|
from fedireads.tasks import app
|
2020-03-27 22:25:08 +00:00
|
|
|
|
2020-03-07 20:22:28 +00:00
|
|
|
|
|
|
|
def get_or_create_book(key):
|
|
|
|
''' pull up a book record by whatever means possible '''
|
2020-03-27 22:25:08 +00:00
|
|
|
try:
|
2020-03-27 22:42:44 +00:00
|
|
|
book = models.Book.objects.select_subclasses().get(
|
2020-03-27 23:36:52 +00:00
|
|
|
fedireads_key=key
|
2020-03-27 22:42:44 +00:00
|
|
|
)
|
2020-03-27 22:25:08 +00:00
|
|
|
return book
|
|
|
|
except models.Book.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
connector = get_connector()
|
2020-04-02 05:11:31 +00:00
|
|
|
book = connector.get_or_create_book(key)
|
|
|
|
load_more_data.delay(book.id)
|
|
|
|
return book
|
|
|
|
|
|
|
|
|
|
|
|
@app.task
|
|
|
|
def load_more_data(book_id):
|
|
|
|
''' background the work of getting all 10,000 editions of LoTR '''
|
|
|
|
book = models.Book.objects.select_subclasses().get(id=book_id)
|
|
|
|
connector = get_connector(book)
|
|
|
|
connector.expand_book_data(book)
|
|
|
|
|
2020-03-27 22:25:08 +00:00
|
|
|
|
2020-05-03 19:59:06 +00:00
|
|
|
def search(query, first=False):
|
|
|
|
''' find books based on arbitary keywords '''
|
|
|
|
results = []
|
|
|
|
for connector in get_connectors():
|
|
|
|
result = connector.search(query)
|
|
|
|
if first and result:
|
|
|
|
return result[0]
|
|
|
|
results.append({
|
|
|
|
'connector': connector,
|
|
|
|
'results': result,
|
|
|
|
})
|
2020-04-29 17:57:20 +00:00
|
|
|
|
|
|
|
return results
|
2020-03-27 22:25:08 +00:00
|
|
|
|
2020-05-03 19:59:06 +00:00
|
|
|
|
2020-03-28 23:30:54 +00:00
|
|
|
def update_book(book):
|
|
|
|
''' re-sync with the original data source '''
|
|
|
|
connector = get_connector(book)
|
|
|
|
connector.update_book(book)
|
|
|
|
|
2020-03-27 21:14:28 +00:00
|
|
|
|
2020-05-03 19:59:06 +00:00
|
|
|
def get_connectors():
|
|
|
|
''' load all connectors '''
|
|
|
|
connectors_info = models.Connector.objects.order_by('priority').all()
|
|
|
|
return [load_connector(c) for c in connectors_info]
|
2020-04-29 17:57:20 +00:00
|
|
|
|
|
|
|
|
2020-05-03 19:59:06 +00:00
|
|
|
def get_connector(book=None):
|
2020-03-27 22:25:08 +00:00
|
|
|
''' pick a book data connector '''
|
|
|
|
if book and book.connector:
|
|
|
|
connector_info = book.connector
|
|
|
|
else:
|
2020-04-29 17:57:20 +00:00
|
|
|
# only select from external connectors
|
2020-05-03 19:59:06 +00:00
|
|
|
connector_info = models.Connector.objects.filter(
|
2020-05-03 20:12:42 +00:00
|
|
|
local=False
|
2020-05-03 19:59:06 +00:00
|
|
|
).order_by('priority').first()
|
|
|
|
return load_connector(connector_info)
|
|
|
|
|
2020-03-27 21:14:28 +00:00
|
|
|
|
2020-05-03 19:59:06 +00:00
|
|
|
def load_connector(connector_info):
|
|
|
|
''' instantiate the connector class '''
|
2020-03-27 23:36:52 +00:00
|
|
|
connector = importlib.import_module(
|
|
|
|
'fedireads.connectors.%s' % connector_info.connector_file
|
|
|
|
)
|
|
|
|
return connector.Connector(connector_info.identifier)
|