mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-23 16:40:36 +00:00
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:
parent
525e2a591d
commit
87fe984462
4 changed files with 60 additions and 85 deletions
|
@ -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"""
|
||||||
|
|
|
@ -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:
|
||||||
|
|
||||||
def format_search_result(self, search_result):
|
|
||||||
search_result["connector"] = self
|
search_result["connector"] = self
|
||||||
return SearchResult(**search_result)
|
yield 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)
|
|
||||||
|
|
|
@ -78,15 +78,13 @@ 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"):
|
||||||
|
|
||||||
def format_search_result(self, search_result):
|
|
||||||
images = search_result.get("image")
|
images = search_result.get("image")
|
||||||
cover = f"{self.covers_url}/img/entities/{images[0]}" if images else None
|
cover = f"{self.covers_url}/img/entities/{images[0]}" if images else None
|
||||||
# a deeply messy translation of inventaire's scores
|
# a deeply messy translation of inventaire's scores
|
||||||
confidence = float(search_result.get("_score", 0.1))
|
confidence = float(search_result.get("_score", 0.1))
|
||||||
confidence = 0.1 if confidence < 150 else 0.999
|
confidence = 0.1 if confidence < 150 else 0.999
|
||||||
return SearchResult(
|
yield SearchResult(
|
||||||
title=search_result.get("label"),
|
title=search_result.get("label"),
|
||||||
key=self.get_remote_id(search_result.get("uri")),
|
key=self.get_remote_id(search_result.get("uri")),
|
||||||
author=search_result.get("description"),
|
author=search_result.get("description"),
|
||||||
|
@ -98,17 +96,12 @@ class Connector(AbstractConnector):
|
||||||
|
|
||||||
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 []
|
|
||||||
return list(results.values())
|
|
||||||
|
|
||||||
def format_isbn_search_result(self, search_result):
|
|
||||||
"""totally different format than a regular search result"""
|
|
||||||
title = search_result.get("claims", {}).get("wdt:P1476", [])
|
title = search_result.get("claims", {}).get("wdt:P1476", [])
|
||||||
if not title:
|
if not title:
|
||||||
return None
|
continue
|
||||||
return SearchResult(
|
yield SearchResult(
|
||||||
title=title[0],
|
title=title[0],
|
||||||
key=self.get_remote_id(search_result.get("uri")),
|
key=self.get_remote_id(search_result.get("uri")),
|
||||||
author=search_result.get("description"),
|
author=search_result.get("description"),
|
||||||
|
|
|
@ -153,15 +153,13 @@ 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"):
|
||||||
|
|
||||||
def format_search_result(self, search_result):
|
|
||||||
# build the remote id from the openlibrary key
|
# build the remote id from the openlibrary key
|
||||||
key = self.books_url + search_result["key"]
|
key = self.books_url + search_result["key"]
|
||||||
author = search_result.get("author_name") or ["Unknown"]
|
author = search_result.get("author_name") or ["Unknown"]
|
||||||
cover_blob = search_result.get("cover_i")
|
cover_blob = search_result.get("cover_i")
|
||||||
cover = self.get_cover_url([cover_blob], size="M") if cover_blob else None
|
cover = self.get_cover_url([cover_blob], size="M") if cover_blob else None
|
||||||
return SearchResult(
|
yield SearchResult(
|
||||||
title=search_result.get("title"),
|
title=search_result.get("title"),
|
||||||
key=key,
|
key=key,
|
||||||
author=", ".join(author),
|
author=", ".join(author),
|
||||||
|
@ -171,14 +169,12 @@ class Connector(AbstractConnector):
|
||||||
)
|
)
|
||||||
|
|
||||||
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()):
|
||||||
|
|
||||||
def format_isbn_search_result(self, search_result):
|
|
||||||
# build the remote id from the openlibrary key
|
# build the remote id from the openlibrary key
|
||||||
key = self.books_url + search_result["key"]
|
key = self.books_url + search_result["key"]
|
||||||
authors = search_result.get("authors") or [{"name": "Unknown"}]
|
authors = search_result.get("authors") or [{"name": "Unknown"}]
|
||||||
author_names = [author.get("name") for author in authors]
|
author_names = [author.get("name") for author in authors]
|
||||||
return SearchResult(
|
yield SearchResult(
|
||||||
title=search_result.get("title"),
|
title=search_result.get("title"),
|
||||||
key=key,
|
key=key,
|
||||||
author=", ".join(author_names),
|
author=", ".join(author_names),
|
||||||
|
|
Loading…
Reference in a new issue