Search cleanup

This commit is contained in:
Mouse Reeve 2020-05-03 15:26:47 -07:00
parent 3094e2b539
commit 855b4a4eb0
2 changed files with 19 additions and 21 deletions

View file

@ -29,29 +29,35 @@ def load_more_data(book_id):
connector.expand_book_data(book) connector.expand_book_data(book)
def search(query, first=False): def search(query):
''' find books based on arbitary keywords ''' ''' find books based on arbitary keywords '''
results = [] results = []
dedup_slug = lambda r: '%s/%s/%s' % (r.title, r.author, r.year) dedup_slug = lambda r: '%s/%s/%s' % (r.title, r.author, r.year)
result_index = [] result_index = set()
for connector in get_connectors(): for connector in get_connectors():
result = connector.search(query) result_set = connector.search(query)
if first and result:
return result[0]
result = [r for r in result if dedup_slug(r) not in result_index] result_set = [r for r in result_set \
result_index += [dedup_slug(r) for r in result] if dedup_slug(r) not in result_index]
# `|=` concats two sets. WE ARE GETTING FANCY HERE
result_index |= set(dedup_slug(r) for r in result_set)
results.append({ results.append({
'connector': connector, 'connector': connector,
'results': result, 'results': result_set,
}) })
if first:
return None
return results return results
def first_search_result(query):
''' search until you find a result that fits '''
for connector in get_connectors():
result = connector.search(query)
if result:
return result[0]
return None
def update_book(book): def update_book(book):
''' re-sync with the original data source ''' ''' re-sync with the original data source '''
connector = get_connector(book) connector = get_connector(book)

View file

@ -53,21 +53,13 @@ class ImportItem(models.Model):
def resolve(self): def resolve(self):
''' try various ways to lookup a book ''' ''' try various ways to lookup a book '''
self.book = ( self.book = (
self.get_book_from_db_isbn() or
self.get_book_from_isbn() or self.get_book_from_isbn() or
self.get_book_from_title_author() self.get_book_from_title_author()
) )
def get_book_from_db_isbn(self):
''' see if we already know about the book '''
try:
return Edition.objects.filter(isbn_13=self.isbn).first()
except Edition.DoesNotExist:
return None
def get_book_from_isbn(self): def get_book_from_isbn(self):
''' search by isbn ''' ''' search by isbn '''
search_result = books_manager.search(self.isbn, first=True) search_result = books_manager.first_search_result(self.isbn)
if search_result: if search_result:
return books_manager.get_or_create_book(search_result.key) return books_manager.get_or_create_book(search_result.key)
@ -77,7 +69,7 @@ class ImportItem(models.Model):
self.data['Title'], self.data['Title'],
self.data['Author'] self.data['Author']
) )
search_result = books_manager.search(search_term, first=True) search_result = books_manager.first_search_result(search_term)
if search_result: if search_result:
return books_manager.get_or_create_book(search_result.key) return books_manager.get_or_create_book(search_result.key)