From bb01834a31970c121e4c248dea10dc74abd12a68 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Fri, 8 May 2020 17:56:24 -0700 Subject: [PATCH] Parser for search results in connectors --- fedireads/connectors/abstract_connector.py | 7 ++++++- fedireads/connectors/fedireads_connector.py | 4 ++++ fedireads/connectors/openlibrary.py | 4 ++++ fedireads/connectors/self_connector.py | 4 ++++ fedireads/views.py | 1 - 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/fedireads/connectors/abstract_connector.py b/fedireads/connectors/abstract_connector.py index 9e1ac6ca3..9b9cc4293 100644 --- a/fedireads/connectors/abstract_connector.py +++ b/fedireads/connectors/abstract_connector.py @@ -49,7 +49,7 @@ class AbstractConnector(ABC): data = resp.json() results = [] - for doc in data['docs'][:10]: + for doc in self.parse_search_data(data)[:10]: results.append(self.format_search_result(doc)) return results @@ -82,6 +82,11 @@ class AbstractConnector(ABC): return book + @abstractmethod + def parse_search_data(self, data): + ''' turn the result json from a search into a list ''' + + @abstractmethod def format_search_result(self, search_result): ''' create a SearchResult obj from json ''' diff --git a/fedireads/connectors/fedireads_connector.py b/fedireads/connectors/fedireads_connector.py index 1fa49ebcd..a156f1e61 100644 --- a/fedireads/connectors/fedireads_connector.py +++ b/fedireads/connectors/fedireads_connector.py @@ -26,6 +26,10 @@ class Connector(AbstractConnector): return SearchResult(**search_result) + def parse_search_data(self, data): + return data + + def get_or_create_book(self, remote_id): ''' pull up a book record by whatever means possible ''' book = models.Book.objects.select_subclasses().filter( diff --git a/fedireads/connectors/openlibrary.py b/fedireads/connectors/openlibrary.py index 5d4b272fa..94f13f6d4 100644 --- a/fedireads/connectors/openlibrary.py +++ b/fedireads/connectors/openlibrary.py @@ -47,6 +47,10 @@ class Connector(AbstractConnector): ) + def parse_search_data(self, data): + return data.get('docs') + + def get_or_create_book(self, olkey): ''' pull up a book record by whatever means possible. if you give a work key, it should give you the default edition, diff --git a/fedireads/connectors/self_connector.py b/fedireads/connectors/self_connector.py index dfcd36459..8e09e3131 100644 --- a/fedireads/connectors/self_connector.py +++ b/fedireads/connectors/self_connector.py @@ -39,6 +39,10 @@ class Connector(AbstractConnector): return search_results + def parse_search_data(self, data): + return data + + def format_search_result(self, book): return SearchResult( book.title, diff --git a/fedireads/views.py b/fedireads/views.py index 6cfe7c38b..74bd6b8f6 100644 --- a/fedireads/views.py +++ b/fedireads/views.py @@ -143,7 +143,6 @@ def get_activity_feed(user, filter_level, model=models.Status): return activities -@login_required def search(request): ''' that search bar up top ''' query = request.GET.get('q')