moviewyrm/fedireads/books_manager.py
Mouse Reeve 0f98610629 Use local key instead of openlibrary key in urls
but uh maybe the local key shouldn't look so gnarly?
2020-03-27 15:43:19 -07:00

37 lines
976 B
Python

''' select and call a connector for whatever book task needs doing '''
import importlib
from fedireads import models
from fedireads.connectors.settings import CONNECTORS
def get_or_create_book(key):
''' pull up a book record by whatever means possible '''
try:
book = models.Book.objects.select_subclasses().get(
local_key=key
)
return book
except models.Book.DoesNotExist:
pass
connector = get_connector()
return connector.get_or_create_book(key)
def search(query):
''' ya '''
connector = get_connector()
return connector.search(query)
def get_connector(book=None):
''' pick a book data connector '''
if book and book.connector:
connector_info = book.connector
else:
connector_info = models.Connector.objects.first()
classname = CONNECTORS[connector_info.name]['classname']
connector = importlib.import_module(classname)
return connector.Connector()