Lookup by openlibrary key

This commit is contained in:
Mouse Reeve 2021-12-14 12:49:00 -08:00
parent 4f1d4d9bc0
commit bee3c86223
2 changed files with 16 additions and 9 deletions

View file

@ -88,7 +88,9 @@ class ImportItem(models.Model):
return return
if self.isbn: if self.isbn:
self.book = self.get_book_from_isbn() self.book = self.get_book_from_identifier()
elif self.openlibrary_key:
self.book = self.get_book_from_identifier(field="openlibrary_key")
else: else:
# don't fall back on title/author search if isbn is present. # don't fall back on title/author search if isbn is present.
# you're too likely to mismatch # you're too likely to mismatch
@ -98,10 +100,10 @@ class ImportItem(models.Model):
else: else:
self.book_guess = book self.book_guess = book
def get_book_from_isbn(self): def get_book_from_identifier(self, field="isbn"):
"""search by isbn""" """search by isbn or other unique identifier"""
search_result = connector_manager.first_search_result( search_result = connector_manager.first_search_result(
self.isbn, min_confidence=0.999 getattr(self, field), min_confidence=0.999
) )
if search_result: if search_result:
# it's already in the right format # it's already in the right format
@ -147,6 +149,13 @@ class ImportItem(models.Model):
self.normalized_data.get("isbn_10") self.normalized_data.get("isbn_10")
) )
@property
def openlibrary_key(self):
"""the edition identifier is preferable to the work key"""
return self.normalized_data.get("openlibrary_key") or self.normalized_data.get(
"openlibrary_work_key"
)
@property @property
def shelf(self): def shelf(self):
"""the goodreads shelf field""" """the goodreads shelf field"""

View file

@ -56,14 +56,12 @@ class OpenLibraryImport(TestCase):
self.assertEqual(import_items[1].data["Edition Id"], "OL7798182M") self.assertEqual(import_items[1].data["Edition Id"], "OL7798182M")
self.assertEqual(import_items[0].normalized_data["shelf"], "reading") self.assertEqual(import_items[0].normalized_data["shelf"], "reading")
self.assertIsNone(import_items[0].normalized_data["openlibrary_key"]) self.assertEqual(import_items[0].normalized_data["openlibrary_key"], "")
self.assertEqual( self.assertEqual(
import_items[0].normalized_data["openlibrary_work_key"], import_items[0].normalized_data["openlibrary_work_key"], "OL102749W"
"OL102749W"
) )
self.assertEqual( self.assertEqual(
import_items[1].normalized_data["openlibrary_key"], import_items[1].normalized_data["openlibrary_key"], "OL7798182M"
"OL7798182M"
) )
self.assertEqual(import_items[2].normalized_data["shelf"], "to-read") self.assertEqual(import_items[2].normalized_data["shelf"], "to-read")
self.assertEqual(import_items[3].normalized_data["shelf"], "read") self.assertEqual(import_items[3].normalized_data["shelf"], "read")