瀏覽代碼

[books] Add comicvine

Colin Powell 1 年之前
父節點
當前提交
2d26a7c3bd
共有 2 個文件被更改,包括 31 次插入19 次删除
  1. 17 16
      vrobbler/apps/books/comicvine.py
  2. 14 3
      vrobbler/apps/books/models.py

+ 17 - 16
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

+ 14 - 3
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