Exemplo n.º 1
0
 def format_isbn_search_result(self, search_result):
     # build the remote id from the openlibrary key
     key = self.books_url + search_result["key"]
     authors = search_result.get("authors") or [{"name": "Unknown"}]
     author_names = [author.get("name") for author in authors]
     return SearchResult(
         title=search_result.get("title"),
         key=key,
         author=", ".join(author_names),
         connector=self,
         year=search_result.get("publish_date"),
     )
Exemplo n.º 2
0
 def format_isbn_search_result(self, search_result):
     """totally different format than a regular search result"""
     title = search_result.get("claims", {}).get("wdt:P1476", [])
     if not title:
         return None
     return SearchResult(
         title=title[0],
         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,
     )
Exemplo n.º 3
0
 def format_search_result(self, search_result):
     # build the remote id from the openlibrary key
     key = self.books_url + search_result["key"]
     author = search_result.get("author_name") or ["Unknown"]
     cover_blob = search_result.get("cover_i")
     cover = self.get_cover_url([cover_blob], size="M") if cover_blob else None
     return SearchResult(
         title=search_result.get("title"),
         key=key,
         author=", ".join(author),
         connector=self,
         year=search_result.get("first_publish_year"),
         cover=cover,
     )
Exemplo n.º 4
0
 def format_search_result(self, search_result):
     images = search_result.get("image")
     cover = f"{self.covers_url}/img/entities/{images[0]}" if images else None
     # a deeply messy translation of inventaire's scores
     confidence = float(search_result.get("_score", 0.1))
     confidence = 0.1 if confidence < 150 else 0.999
     return SearchResult(
         title=search_result.get("label"),
         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=cover,
         confidence=confidence,
         connector=self,
     )
Exemplo n.º 5
0
    def test_get_book_from_identifier(self):
        """search and load books by isbn (9780356506999)"""
        item = models.ImportItem.objects.create(
            index=1,
            job=self.job,
            data={},
            normalized_data={
                "isbn_13": '="9780356506999"',
            },
        )
        connector_info = models.Connector.objects.create(
            identifier="openlibrary.org",
            name="OpenLibrary",
            connector_file="openlibrary",
            base_url="https://openlibrary.org",
            books_url="https://openlibrary.org",
            covers_url="https://covers.openlibrary.org",
            search_url="https://openlibrary.org/search?q=",
            priority=3,
        )
        connector = connector_manager.load_connector(connector_info)
        result = SearchResult(
            title="Test Result",
            key="https://openlibrary.org/works/OL1234W",
            author="An Author",
            year="1980",
            connector=connector,
        )

        datafile = pathlib.Path(__file__).parent.joinpath(
            "../data/ol_edition.json")
        bookdata = json.loads(datafile.read_bytes())
        responses.add(
            responses.GET,
            "https://openlibrary.org/works/OL1234W",
            json=bookdata,
            status=200,
        )
        responses.add(
            responses.GET,
            "https://openlibrary.org/works/OL15832982W",
            json=bookdata,
            status=200,
        )
        responses.add(
            responses.GET,
            "https://openlibrary.org/authors/OL382982A",
            json={"name": "test author"},
            status=200,
        )

        with patch(
                "bookwyrm.connectors.abstract_connector.load_more_data.delay"):
            with patch(
                    "bookwyrm.connectors.connector_manager.first_search_result"
            ) as search:
                search.return_value = result
                with patch("bookwyrm.connectors.openlibrary.Connector."
                           "get_authors_from_data"):
                    book = item.get_book_from_identifier()

        self.assertEqual(book.title, "Sabriel")
Exemplo n.º 6
0
 def format_search_result(self, search_result):
     search_result["connector"] = self
     return SearchResult(**search_result)