Use database as source for initializing connector

This commit is contained in:
Mouse Reeve
2020-03-27 15:25:08 -07:00
parent 8494719512
commit 5d2fbb8500
7 changed files with 56 additions and 46 deletions

View File

@ -1,15 +1,37 @@
''' select and call a connector for whatever book task needs doing '''
from fedireads.connectors import OpenLibraryConnector
import importlib
from fedireads import models
from fedireads.connectors.settings import CONNECTORS
openlibrary = OpenLibraryConnector()
def get_or_create_book(key):
''' pull up a book record by whatever means possible '''
return openlibrary.get_or_create_book(key)
try:
book = models.Book.objects.get(
openlibrary_key=key
).select_subclasses()
return book
except models.Book.DoesNotExist:
pass
connector = get_connector()
return connector.get_or_create_book(key)
def search(query):
''' ya '''
return openlibrary.search(query)
connector = get_connector()
return connector.search(query)
def update_book(key):
return openlibrary.update_book(key)
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()