2020-09-21 11:10:37 -04:00
|
|
|
''' using a bookwyrm instance as a source of book data '''
|
2020-05-12 16:03:46 -04:00
|
|
|
from django.contrib.postgres.search import SearchRank, SearchVector
|
2020-11-04 13:35:13 -05:00
|
|
|
from django.db.models import F
|
2020-03-28 15:55:53 -04:00
|
|
|
|
2020-09-21 11:10:37 -04:00
|
|
|
from bookwyrm import models
|
2020-04-29 13:57:20 -04:00
|
|
|
from .abstract_connector import AbstractConnector, SearchResult
|
2020-03-28 15:55:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Connector(AbstractConnector):
|
|
|
|
''' instantiate a connector '''
|
2020-10-29 18:29:23 -04:00
|
|
|
def search(self, query, min_confidence=0.1):
|
2021-01-02 14:29:50 -05:00
|
|
|
''' search your local database '''
|
2020-05-12 16:03:46 -04:00
|
|
|
vector = SearchVector('title', weight='A') +\
|
|
|
|
SearchVector('subtitle', weight='B') +\
|
2020-12-21 14:57:45 -05:00
|
|
|
SearchVector('authors__name', weight='C') +\
|
2020-05-12 16:03:46 -04:00
|
|
|
SearchVector('isbn_13', weight='A') +\
|
|
|
|
SearchVector('isbn_10', weight='A') +\
|
2020-11-13 14:03:39 -05:00
|
|
|
SearchVector('openlibrary_key', weight='C') +\
|
|
|
|
SearchVector('goodreads_key', weight='C') +\
|
|
|
|
SearchVector('asin', weight='C') +\
|
|
|
|
SearchVector('oclc_number', weight='C') +\
|
|
|
|
SearchVector('remote_id', weight='C') +\
|
|
|
|
SearchVector('description', weight='D') +\
|
|
|
|
SearchVector('series', weight='D')
|
2020-05-12 16:03:46 -04:00
|
|
|
|
2020-04-29 13:57:20 -04:00
|
|
|
results = models.Edition.objects.annotate(
|
2020-05-12 16:03:46 -04:00
|
|
|
search=vector
|
|
|
|
).annotate(
|
|
|
|
rank=SearchRank(vector, query)
|
|
|
|
).filter(
|
2020-10-29 18:29:23 -04:00
|
|
|
rank__gt=min_confidence
|
2020-05-12 16:03:46 -04:00
|
|
|
).order_by('-rank')
|
2020-11-04 13:35:13 -05:00
|
|
|
|
|
|
|
# remove non-default editions, if possible
|
|
|
|
results = results.filter(parent_work__default_edition__id=F('id')) \
|
|
|
|
or results
|
2020-04-29 13:57:20 -04:00
|
|
|
|
|
|
|
search_results = []
|
2021-01-02 17:33:01 -05:00
|
|
|
for book in set(results[:10]):
|
2020-04-29 13:57:20 -04:00
|
|
|
search_results.append(
|
2020-05-04 00:00:25 -04:00
|
|
|
self.format_search_result(book)
|
2020-04-29 13:57:20 -04:00
|
|
|
)
|
|
|
|
return search_results
|
2020-03-28 15:55:53 -04:00
|
|
|
|
|
|
|
|
2020-09-21 13:25:26 -04:00
|
|
|
def format_search_result(self, search_result):
|
2020-05-04 00:00:25 -04:00
|
|
|
return SearchResult(
|
2020-10-29 18:29:23 -04:00
|
|
|
title=search_result.title,
|
2020-11-13 12:47:35 -05:00
|
|
|
key=search_result.remote_id,
|
2020-10-29 18:29:23 -04:00
|
|
|
author=search_result.author_text,
|
|
|
|
year=search_result.published_date.year if \
|
2020-09-21 13:25:26 -04:00
|
|
|
search_result.published_date else None,
|
2020-12-27 17:27:18 -05:00
|
|
|
connector=self,
|
2020-10-29 18:29:23 -04:00
|
|
|
confidence=search_result.rank,
|
2020-05-04 00:00:25 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-10 15:56:59 -04:00
|
|
|
def is_work_data(self, data):
|
|
|
|
pass
|
2020-03-28 15:55:53 -04:00
|
|
|
|
2020-05-10 15:56:59 -04:00
|
|
|
def get_edition_from_work_data(self, data):
|
|
|
|
pass
|
2020-03-28 15:55:53 -04:00
|
|
|
|
2020-12-27 17:27:18 -05:00
|
|
|
def get_work_from_edition_data(self, data):
|
2020-05-10 15:56:59 -04:00
|
|
|
pass
|
2020-05-09 16:36:10 -04:00
|
|
|
|
|
|
|
def get_authors_from_data(self, data):
|
|
|
|
return None
|
|
|
|
|
2020-05-10 15:56:59 -04:00
|
|
|
def parse_search_data(self, data):
|
|
|
|
''' it's already in the right format, don't even worry about it '''
|
|
|
|
return data
|
2020-04-29 13:57:20 -04:00
|
|
|
|
|
|
|
def expand_book_data(self, book):
|
|
|
|
pass
|