forked from mirrors/bookwyrm
85 lines
2.6 KiB
Python
85 lines
2.6 KiB
Python
''' using a fedireads instance as a source of book data '''
|
|
from django.contrib.postgres.search import SearchVector
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from fedireads import models
|
|
from .abstract_connector import AbstractConnector, SearchResult
|
|
|
|
|
|
class Connector(AbstractConnector):
|
|
''' instantiate a connector '''
|
|
def __init__(self, identifier):
|
|
super().__init__(identifier)
|
|
|
|
|
|
def search(self, query):
|
|
''' right now you can't search fedireads sorry, but when
|
|
that gets implemented it will totally rule '''
|
|
results = models.Edition.objects.annotate(
|
|
search=SearchVector('title', weight='A') +\
|
|
SearchVector('subtitle', weight='B') +\
|
|
SearchVector('author_text', weight='A') +\
|
|
SearchVector('isbn_13', weight='A') +\
|
|
SearchVector('isbn_10', weight='A') +\
|
|
SearchVector('openlibrary_key', weight='B') +\
|
|
SearchVector('goodreads_key', weight='B') +\
|
|
SearchVector('asin', weight='B') +\
|
|
SearchVector('oclc_number', weight='B') +\
|
|
SearchVector('remote_id', weight='B') +\
|
|
SearchVector('description', weight='C') +\
|
|
SearchVector('series', weight='C')
|
|
).filter(search=query)
|
|
results = results.filter(default=True) or results
|
|
|
|
search_results = []
|
|
for book in results[:10]:
|
|
search_results.append(
|
|
self.format_search_result(book)
|
|
)
|
|
return search_results
|
|
|
|
|
|
def get_authors_from_data(self, data):
|
|
return None
|
|
|
|
def get_cover_from_data(self, data):
|
|
return None
|
|
|
|
def parse_search_data(self, data):
|
|
return data
|
|
|
|
|
|
def format_search_result(self, book):
|
|
return SearchResult(
|
|
book.title,
|
|
book.id,
|
|
book.author_text,
|
|
book.published_date.year if book.published_date else None,
|
|
)
|
|
|
|
|
|
def get_or_create_book(self, book_id):
|
|
''' since this is querying its own data source, it can only
|
|
get a book, not load one from an external source '''
|
|
try:
|
|
return models.Book.objects.select_subclasses().get(
|
|
id=book_id
|
|
)
|
|
except ObjectDoesNotExist:
|
|
return None
|
|
|
|
|
|
def get_or_create_author(self, author_id):
|
|
''' load that author '''
|
|
try:
|
|
return models.Author.objects.get(id=author_id)
|
|
except ObjectDoesNotExist:
|
|
pass
|
|
|
|
|
|
def update_book(self, book_obj, data=None):
|
|
pass
|
|
|
|
|
|
def expand_book_data(self, book):
|
|
pass
|