From 2d26a7c3bde4d3961c5575ff5d2e754207f11434 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Tue, 26 Mar 2024 22:02:15 -0400 Subject: [PATCH] [books] Add comicvine --- vrobbler/apps/books/comicvine.py | 33 ++++++++++++++++---------------- vrobbler/apps/books/models.py | 17 +++++++++++++--- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/vrobbler/apps/books/comicvine.py b/vrobbler/apps/books/comicvine.py index 6795d99..b5a3d98 100644 --- a/vrobbler/apps/books/comicvine.py +++ b/vrobbler/apps/books/comicvine.py @@ -208,25 +208,26 @@ def lookup_comic_from_comicvine(title: str) -> dict: client = ComicVineClient( api_key=getattr(settings, "COMICVINE_API_KEY", None) ) - return client.search(title) - results = [ + result = [ r for r in client.search(title).get("results") if r.get("resource_type") == "volume" - ] + ][0] - return results - """ - { - "title": top.get("title"), - "isbn": isbn, - "comicvine_id": ol_id, - "first_publish_year": top.get("first_publish_year"), - "first_sentence": first_sentence, - "pages": top.get("number_of_pages_median", None), - "cover_url": COVER_URL.format(id=ol_id), - "cv_author_id": ol_author_id, - "subject_key_list": top.get("subject_key", []), + if "volume" not in result.keys(): + logger.warn("No result found on ComicVine", extra={"title": title}) + return {} + + title = " ".join([result.get("volume").get("name"), result.get("name)")]) + data_dict = { + "title": title, + "cover_url": result.get("image").get("original_url"), + "comicvine_data": { + "id": result.get("id"), + "site_detail_url": result.get("site_detail_url"), + "description": result.get("description"), + "image": result.get("image").get("original_url"), + }, } - """ + return data_dict diff --git a/vrobbler/apps/books/models.py b/vrobbler/apps/books/models.py index 1eaf37d..6ea5306 100644 --- a/vrobbler/apps/books/models.py +++ b/vrobbler/apps/books/models.py @@ -23,6 +23,10 @@ from scrobbles.mixins import ( from scrobbles.utils import get_scrobbles_for_media from taggit.managers import TaggableManager from thefuzz import fuzz +from vrobbler.apps.books.comicvine import ( + ComicVineClient, + lookup_comic_from_comicvine, +) from vrobbler.apps.books.locg import ( lookup_comic_by_locg_slug, @@ -30,6 +34,8 @@ from vrobbler.apps.books.locg import ( lookup_comic_writer_by_locg_slug, ) +COMICVINE_API_KEY = getattr(settings, "COMICVINE_API_KEY", "") + logger = logging.getLogger(__name__) User = get_user_model() BNULL = {"blank": True, "null": True} @@ -150,7 +156,7 @@ class Book(LongPlayScrobblableMixin): author_name = self.author.name if not data: - logger.warn(f"rChecking openlibrary for {self.title}") + logger.warn(f"Checking openlibrary for {self.title}") if self.openlibrary_id and force_update: data = lookup_book_from_openlibrary( str(self.openlibrary_id) @@ -163,13 +169,18 @@ class Book(LongPlayScrobblableMixin): if not data: if self.locg_slug: logger.warn( - f"rChecking openlibrary for {self.title} with slug {self.locg_slug}" + f"Checking LOCG for {self.title} with slug {self.locg_slug}" ) data = lookup_comic_by_locg_slug(str(self.locg_slug)) else: - logger.warn(f"rChecking openlibrary for {self.title}") + logger.warn(f"Checking LOCG for {self.title}") data = lookup_comic_from_locg(str(self.title)) + if not data and COMICVINE_API_KEY: + logger.warn(f"Checking ComicVine for {self.title}") + cv_client = ComicVineClient(api_key=COMICVINE_API_KEY) + data = lookup_comic_from_comicvine(str(self.title)) + if not data: logger.warn(f"Book not found in any sources: {self.title}") return