Combines search formatter and parser function

The parser was extracting the list of search results from the json
object returned by the search endpoint, and the formatter was converting
an individual json entry into a SearchResult object. This just merged
them into one function, because they are never used separately.
This commit is contained in:
Mouse Reeve 2022-05-30 12:52:31 -07:00
parent 525e2a591d
commit 87fe984462
4 changed files with 60 additions and 85 deletions

View file

@ -52,11 +52,9 @@ class AbstractMinimalConnector(ABC):
"""Format the search results based on the formt of the query""" """Format the search results based on the formt of the query"""
# TODO: inventaire min confidence # TODO: inventaire min confidence
parser = self.parse_search_data parser = self.parse_search_data
formatter = self.format_search_result
if maybe_isbn(query): if maybe_isbn(query):
parser = self.parse_isbn_search_data parser = self.parse_isbn_search_data
formatter = self.format_isbn_search_result return list(parser(data))[:10]
return [formatter(doc) for doc in parser(data)[:10]]
@abstractmethod @abstractmethod
def get_or_create_book(self, remote_id): def get_or_create_book(self, remote_id):
@ -66,18 +64,10 @@ class AbstractMinimalConnector(ABC):
def parse_search_data(self, data): def parse_search_data(self, data):
"""turn the result json from a search into a list""" """turn the result json from a search into a list"""
@abstractmethod
def format_search_result(self, search_result):
"""create a SearchResult obj from json"""
@abstractmethod @abstractmethod
def parse_isbn_search_data(self, data): def parse_isbn_search_data(self, data):
"""turn the result json from a search into a list""" """turn the result json from a search into a list"""
@abstractmethod
def format_isbn_search_result(self, search_result):
"""create a SearchResult obj from json"""
class AbstractConnector(AbstractMinimalConnector): class AbstractConnector(AbstractMinimalConnector):
"""generic book data connector""" """generic book data connector"""

View file

@ -11,14 +11,10 @@ class Connector(AbstractMinimalConnector):
return activitypub.resolve_remote_id(remote_id, model=models.Edition) return activitypub.resolve_remote_id(remote_id, model=models.Edition)
def parse_search_data(self, data): def parse_search_data(self, data):
return data for search_result in data:
search_result["connector"] = self
def format_search_result(self, search_result): yield SearchResult(**search_result)
search_result["connector"] = self
return SearchResult(**search_result)
def parse_isbn_search_data(self, data): def parse_isbn_search_data(self, data):
return data for search_result in data:
yield self.format_search_result(search_result)
def format_isbn_search_result(self, search_result):
return self.format_search_result(search_result)

View file

@ -78,44 +78,37 @@ class Connector(AbstractConnector):
} }
def parse_search_data(self, data): def parse_search_data(self, data):
return data.get("results") for search_result in data.get("results"):
images = search_result.get("image")
def format_search_result(self, search_result): cover = f"{self.covers_url}/img/entities/{images[0]}" if images else None
images = search_result.get("image") # a deeply messy translation of inventaire's scores
cover = f"{self.covers_url}/img/entities/{images[0]}" if images else None confidence = float(search_result.get("_score", 0.1))
# a deeply messy translation of inventaire's scores confidence = 0.1 if confidence < 150 else 0.999
confidence = float(search_result.get("_score", 0.1)) yield SearchResult(
confidence = 0.1 if confidence < 150 else 0.999 title=search_result.get("label"),
return SearchResult( key=self.get_remote_id(search_result.get("uri")),
title=search_result.get("label"), author=search_result.get("description"),
key=self.get_remote_id(search_result.get("uri")), view_link=f"{self.base_url}/entity/{search_result.get('uri')}",
author=search_result.get("description"), cover=cover,
view_link=f"{self.base_url}/entity/{search_result.get('uri')}", confidence=confidence,
cover=cover, connector=self,
confidence=confidence, )
connector=self,
)
def parse_isbn_search_data(self, data): def parse_isbn_search_data(self, data):
"""got some daaaata""" """got some daaaata"""
results = data.get("entities") results = data.get("entities", [])
if not results: for search_result in list(results.values()):
return [] title = search_result.get("claims", {}).get("wdt:P1476", [])
return list(results.values()) if not title:
continue
def format_isbn_search_result(self, search_result): yield SearchResult(
"""totally different format than a regular search result""" title=title[0],
title = search_result.get("claims", {}).get("wdt:P1476", []) key=self.get_remote_id(search_result.get("uri")),
if not title: author=search_result.get("description"),
return None view_link=f"{self.base_url}/entity/{search_result.get('uri')}",
return SearchResult( cover=self.get_cover_url(search_result.get("image")),
title=title[0], connector=self,
key=self.get_remote_id(search_result.get("uri")), )
author=search_result.get("description"),
view_link=f"{self.base_url}/entity/{search_result.get('uri')}",
cover=self.get_cover_url(search_result.get("image")),
connector=self,
)
def is_work_data(self, data): def is_work_data(self, data):
return data.get("type") == "work" return data.get("type") == "work"

View file

@ -153,38 +153,34 @@ class Connector(AbstractConnector):
return f"{self.covers_url}/b/id/{image_name}" return f"{self.covers_url}/b/id/{image_name}"
def parse_search_data(self, data): def parse_search_data(self, data):
return data.get("docs") for search_result in data.get("docs"):
# build the remote id from the openlibrary key
def format_search_result(self, search_result): key = self.books_url + search_result["key"]
# build the remote id from the openlibrary key author = search_result.get("author_name") or ["Unknown"]
key = self.books_url + search_result["key"] cover_blob = search_result.get("cover_i")
author = search_result.get("author_name") or ["Unknown"] cover = self.get_cover_url([cover_blob], size="M") if cover_blob else None
cover_blob = search_result.get("cover_i") yield SearchResult(
cover = self.get_cover_url([cover_blob], size="M") if cover_blob else None title=search_result.get("title"),
return SearchResult( key=key,
title=search_result.get("title"), author=", ".join(author),
key=key, connector=self,
author=", ".join(author), year=search_result.get("first_publish_year"),
connector=self, cover=cover,
year=search_result.get("first_publish_year"), )
cover=cover,
)
def parse_isbn_search_data(self, data): def parse_isbn_search_data(self, data):
return list(data.values()) for search_result in list(data.values()):
# build the remote id from the openlibrary key
def format_isbn_search_result(self, search_result): key = self.books_url + search_result["key"]
# build the remote id from the openlibrary key authors = search_result.get("authors") or [{"name": "Unknown"}]
key = self.books_url + search_result["key"] author_names = [author.get("name") for author in authors]
authors = search_result.get("authors") or [{"name": "Unknown"}] yield SearchResult(
author_names = [author.get("name") for author in authors] title=search_result.get("title"),
return SearchResult( key=key,
title=search_result.get("title"), author=", ".join(author_names),
key=key, connector=self,
author=", ".join(author_names), year=search_result.get("publish_date"),
connector=self, )
year=search_result.get("publish_date"),
)
def load_edition_data(self, olkey): def load_edition_data(self, olkey):
"""query openlibrary for editions of a work""" """query openlibrary for editions of a work"""