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()

View file

@ -1,3 +1,2 @@
''' bring connectors into the namespace '''
from .settings import CONNECTORS
from .openlibrary import OpenLibraryConnector

View file

@ -1,6 +1,7 @@
''' functionality outline for a book data connector '''
from abc import ABC, abstractmethod
from fedireads import models
from fedireads.connectors import CONNECTORS
@ -13,15 +14,22 @@ class AbstractConnector(ABC):
if not settings:
raise ValueError('No connector with name "%s"' % connector_name)
try:
self.url = settings['BASE_URL']
self.covers_url = settings['COVERS_URL']
self.db_field = settings['DB_KEY_FIELD']
self.key_name = settings['KEY_NAME']
except KeyError:
raise KeyError('Invalid connector settings')
# TODO: politeness settings
info = models.Connector.objects.get(name=settings['db_name'])
self.model = info
self.url = info.base_url
self.covers_url = info.covers_url
self.search_url = info.search_url
self.key_name = info.key_name
self.max_query_count = info.max_query_count
def is_available(self):
''' check if you're allowed to use this connector '''
if self.model.max_query_count is not None:
if self.model.query_count >= self.model.max_query_count:
return False
return True
@abstractmethod
def search(self, query):

View file

@ -8,10 +8,10 @@ from fedireads import models
from .abstract_connector import AbstractConnector, SearchResult
class OpenLibraryConnector(AbstractConnector):
class Connector(AbstractConnector):
''' instantiate a connector for OL '''
def __init__(self):
super().__init__('openlibrary')
super().__init__('OpenLibrary')
def search(self, query):
@ -76,6 +76,8 @@ class OpenLibraryConnector(AbstractConnector):
key = data.get('works')[0]['key']
key = key.split('/')[-1]
work = self.get_or_create_book(key)
h
book.parent_work = work
# we also need to know the author get the cover

View file

@ -1,28 +1,7 @@
''' settings book data connectors '''
CONNECTORS = {
'openlibrary': {
'KEY_NAME': 'olkey',
'DB_KEY_FIELD': 'openlibrary_key',
'POLITENESS_DELAY': 0,
'MAX_DAILY_QUERIES': -1,
'BASE_URL': 'https://openlibrary.org',
'COVERS_URL': 'https://covers.openlibrary.org',
'OpenLibrary': {
'db_name': 'OpenLibrary',
'classname': 'fedireads.connectors.openlibrary',
},
}
''' not implemented yet:
'librarything': {
'KEY_NAME': 'ltkey',
'DB_KEY_FIELD': 'librarything_key',
'POLITENESS_DELAY': 1,
'MAX_DAILY_QUERIES': 1000,
'BASE_URL': 'https://librarything.com',
},
'worldcat': {
'KEY_NAME': 'ocn',
'DB_KEY_FIELD': 'oclc_number',
'POLITENESS_DELAY': 0,
'MAX_DAILY_QUERIES': -1,
'BASE_URL': 'https://worldcat.org',
},
'''

View file

@ -1,4 +1,4 @@
# Generated by Django 3.0.3 on 2020-03-27 21:14
# Generated by Django 3.0.3 on 2020-03-27 22:01
from django.db import migrations, models
import django.db.models.deletion
@ -17,12 +17,12 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_date', models.DateTimeField(auto_now_add=True)),
('updated_date', models.DateTimeField(auto_now=True)),
('name', models.CharField(max_length=255)),
('name', models.CharField(max_length=255, unique=True)),
('api_key', models.CharField(max_length=255, null=True)),
('base_url', models.CharField(max_length=255)),
('covers_url', models.CharField(max_length=255)),
('search_url', models.CharField(max_length=255, null=True)),
('key_name', models.CharField(max_length=255)),
('key_name', models.CharField(max_length=255, unique=True)),
('politeness_delay', models.IntegerField(null=True)),
('max_query_count', models.IntegerField(null=True)),
('query_count', models.IntegerField(default=0)),

View file

@ -10,14 +10,14 @@ from fedireads.utils.models import FedireadsModel
class Connector(FedireadsModel):
''' book data source connectors '''
name = models.CharField(max_length=255)
name = models.CharField(max_length=255, unique=True)
api_key = models.CharField(max_length=255, null=True)
base_url = models.CharField(max_length=255)
covers_url = models.CharField(max_length=255)
search_url = models.CharField(max_length=255, null=True)
key_name = models.CharField(max_length=255)
key_name = models.CharField(max_length=255, unique=True)
politeness_delay = models.IntegerField(null=True) #seconds
max_query_count = models.IntegerField(null=True)