[books] Fix book lookup when OL fails
This commit is contained in:
@ -108,11 +108,9 @@ class Author(TimeStampedModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name}"
|
return f"{self.name}"
|
||||||
|
|
||||||
def enrich_from_semantic(self, overwrite=False):
|
def enrich_from_semantic(self, overwrite=False): ...
|
||||||
...
|
|
||||||
|
|
||||||
def enrich_from_google_books(self, overwrite=False):
|
def enrich_from_google_books(self, overwrite=False): ...
|
||||||
...
|
|
||||||
|
|
||||||
def enrich_from_openlibrary(self, overwrite=False):
|
def enrich_from_openlibrary(self, overwrite=False):
|
||||||
data_dict = lookup_author_from_openlibrary(self.openlibrary_id)
|
data_dict = lookup_author_from_openlibrary(self.openlibrary_id)
|
||||||
@ -311,7 +309,8 @@ class Book(LongPlayScrobblableMixin):
|
|||||||
book.save()
|
book.save()
|
||||||
|
|
||||||
book.save_image_from_url(cover_url)
|
book.save_image_from_url(cover_url)
|
||||||
book.genre.add(*genres)
|
if genres:
|
||||||
|
book.genre.add(*genres)
|
||||||
book.authors.add(*author_list)
|
book.authors.add(*author_list)
|
||||||
|
|
||||||
return book
|
return book
|
||||||
@ -390,7 +389,7 @@ class Book(LongPlayScrobblableMixin):
|
|||||||
# Pop this, so we can look it up later
|
# Pop this, so we can look it up later
|
||||||
cover_url = data.pop("cover_url", "")
|
cover_url = data.pop("cover_url", "")
|
||||||
|
|
||||||
subject_key_list = data.pop("subject_key_list", "")
|
subject_key_list = data.pop("subject_key_list", [])
|
||||||
|
|
||||||
# Fun trick for updating all fields at once
|
# Fun trick for updating all fields at once
|
||||||
Book.objects.filter(pk=self.id).update(**data)
|
Book.objects.filter(pk=self.id).update(**data)
|
||||||
|
|||||||
@ -11,7 +11,7 @@ def lookup_book_from_openlibrary(title: str, author: str | None = None) -> dict:
|
|||||||
try:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
f"{OPENLIBRARY_BASE_URL}/search.json",
|
f"{OPENLIBRARY_BASE_URL}/search.json",
|
||||||
params={"title": title, "limit": 1},
|
params={"title": title, "limit": 5},
|
||||||
timeout=10,
|
timeout=10,
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
@ -21,12 +21,17 @@ def lookup_book_from_openlibrary(title: str, author: str | None = None) -> dict:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
docs = search_results.get("docs", [])
|
docs = search_results.get("docs", [])
|
||||||
if not docs:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
work_doc = docs[0]
|
title_lower = title.lower()
|
||||||
olid = work_doc.get("key", "").replace("/works/", "")
|
for work_doc in docs:
|
||||||
if not olid:
|
found_title = work_doc.get("title", "")
|
||||||
|
if found_title.lower() == title_lower:
|
||||||
|
olid = work_doc.get("key", "").replace("/works/", "")
|
||||||
|
if not olid:
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
logger.info(f"OpenLibrary: no close match for '{title}', skipping")
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user