From 0b3bc5370442c02fa251b5ec78d08d0b664c9bb9 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 26 Jan 2024 23:47:13 -0500 Subject: [PATCH] Fix openlibrary lookups --- vrobbler/apps/books/openlibrary.py | 16 +++++++++-- vrobbler/apps/books/tests/test_openlibrary.py | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 vrobbler/apps/books/tests/test_openlibrary.py diff --git a/vrobbler/apps/books/openlibrary.py b/vrobbler/apps/books/openlibrary.py index 9bb196e..66d719a 100644 --- a/vrobbler/apps/books/openlibrary.py +++ b/vrobbler/apps/books/openlibrary.py @@ -102,10 +102,20 @@ def lookup_book_from_openlibrary( top = None for result in results.get("docs"): - # These Summary things suck and ruin our one-shot search - if "Summary of" not in result.get("title"): + if title.lower() == result.get("title", "").lower(): top = result - break + + if not top: + for result in results.get("docs"): + # These Summary things suck and ruin our one-shot search + if "Summary of" in result.get("title"): + continue + + if title.lower() in result.get("title", "").lower(): + top = result + + if not top and len(results.get("docs")) > 0: + top = results.get("docs")[0] if not top: logger.warn(f"No book found for query {query}") diff --git a/vrobbler/apps/books/tests/test_openlibrary.py b/vrobbler/apps/books/tests/test_openlibrary.py new file mode 100644 index 0000000..cdf6224 --- /dev/null +++ b/vrobbler/apps/books/tests/test_openlibrary.py @@ -0,0 +1,28 @@ +import pytest + +from vrobbler.apps.books.openlibrary import ( + lookup_book_from_openlibrary, +) + + +def test_lookup_modern_book(): + book = lookup_book_from_openlibrary("Matrix", "Lauren Groff") + assert book.get("title") == "Matrix" + assert book.get("openlibrary_id") == "OL47572299M" + assert book.get("ol_author_id") == "OL3675729A" + + +def test_lookup_classic_book(): + book = lookup_book_from_openlibrary( + "The Life of Castruccio Castracani", "Machiavelli" + ) + assert book.get("title") == "The Life of Castruccio Castracani of Lucca" + assert book.get("openlibrary_id") == "OL8950869M" + assert book.get("ol_author_id") == "OL23135A" + + +def test_lookup_foreign_book(): + book = lookup_book_from_openlibrary("Ravagé", "René Barjavel") + assert book.get("title") == "Ravage" + assert book.get("openlibrary_id") == "OL8837839M" + assert book.get("ol_author_id") == "OL152472A"