From 855b4a4eb01835833c7fecb41b08a024287d55d3 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sun, 3 May 2020 15:26:47 -0700 Subject: [PATCH] Search cleanup --- fedireads/books_manager.py | 28 +++++++++++++++++----------- fedireads/models/import_job.py | 12 ++---------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/fedireads/books_manager.py b/fedireads/books_manager.py index 9a9c1436c..28effe7e8 100644 --- a/fedireads/books_manager.py +++ b/fedireads/books_manager.py @@ -29,29 +29,35 @@ def load_more_data(book_id): connector.expand_book_data(book) -def search(query, first=False): +def search(query): ''' find books based on arbitary keywords ''' results = [] dedup_slug = lambda r: '%s/%s/%s' % (r.title, r.author, r.year) - result_index = [] + result_index = set() for connector in get_connectors(): - result = connector.search(query) - if first and result: - return result[0] + result_set = connector.search(query) - result = [r for r in result if dedup_slug(r) not in result_index] - result_index += [dedup_slug(r) for r in result] + result_set = [r for r in result_set \ + 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({ 'connector': connector, - 'results': result, + 'results': result_set, }) - if first: - return None - 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): ''' re-sync with the original data source ''' connector = get_connector(book) diff --git a/fedireads/models/import_job.py b/fedireads/models/import_job.py index 7c7030801..9836bea95 100644 --- a/fedireads/models/import_job.py +++ b/fedireads/models/import_job.py @@ -53,21 +53,13 @@ class ImportItem(models.Model): def resolve(self): ''' try various ways to lookup a book ''' self.book = ( - self.get_book_from_db_isbn() or self.get_book_from_isbn() or 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): ''' search by isbn ''' - search_result = books_manager.search(self.isbn, first=True) + search_result = books_manager.first_search_result(self.isbn) if search_result: return books_manager.get_or_create_book(search_result.key) @@ -77,7 +69,7 @@ class ImportItem(models.Model): self.data['Title'], self.data['Author'] ) - search_result = books_manager.search(search_term, first=True) + search_result = books_manager.first_search_result(search_term) if search_result: return books_manager.get_or_create_book(search_result.key)