Repos / pytaku / 24672f7cc2
commit 24672f7cc21eeea0b597c854c1601f430e3df0d9
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Sun Aug 2 15:45:56 2020 +0700

    TIL mangadex cover images aren't always jpg
    
    Yes the db read/write code could use a saner api... sometime soon.

diff --git a/src/mangoapi/__init__.py b/src/mangoapi/__init__.py
index c41cf81..d975b16 100644
--- a/src/mangoapi/__init__.py
+++ b/src/mangoapi/__init__.py
@@ -35,9 +35,13 @@ def get_title(title_id):
     md_json = md_resp.json()
     assert md_json["status"] == "OK"
 
+    cover = md_json["manga"]["cover_url"].split("/")[-1]
+    cover_ext = cover[cover.find(".") + 1 : cover.rfind("?")]
+
     title = {
         "id": title_id,
         "name": md_json["manga"]["title"],
+        "cover_ext": cover_ext,
         "alt_names": md_json["manga"]["alt_names"],
         "descriptions": md_json["manga"]["description"].split("\r\n\r\n"),
         "chapters": [
diff --git a/src/pytaku/database/migrations/latest_schema.sql b/src/pytaku/database/migrations/latest_schema.sql
index d770928..18089c6 100644
--- a/src/pytaku/database/migrations/latest_schema.sql
+++ b/src/pytaku/database/migrations/latest_schema.sql
@@ -5,6 +5,7 @@ CREATE TABLE title (
     id text,
     name text,
     site text,
+    cover_ext text,
     chapters text,
     alt_names text,
     descriptions text,
diff --git a/src/pytaku/database/migrations/m0001.sql b/src/pytaku/database/migrations/m0001.sql
index 7730151..7fb82ee 100644
--- a/src/pytaku/database/migrations/m0001.sql
+++ b/src/pytaku/database/migrations/m0001.sql
@@ -2,6 +2,7 @@ create table title (
     id text,
     name text,
     site text,
+    cover_ext text,
     chapters text,
     alt_names text,
     descriptions text,
diff --git a/src/pytaku/persistence.py b/src/pytaku/persistence.py
index 663a75e..6587b73 100644
--- a/src/pytaku/persistence.py
+++ b/src/pytaku/persistence.py
@@ -11,6 +11,7 @@ def save_title(title):
         id,
         name,
         site,
+        cover_ext,
         chapters,
         alt_names,
         descriptions
@@ -18,11 +19,13 @@ def save_title(title):
         :id,
         :name,
         :site,
+        :cover_ext,
         :chapters,
         :alt_names,
         :descriptions
     ) ON CONFLICT (id, site) DO UPDATE SET
         name=excluded.name,
+        cover_ext=excluded.cover_ext,
         chapters=excluded.chapters,
         alt_names=excluded.alt_names,
         descriptions=excluded.descriptions,
@@ -33,6 +36,7 @@ def save_title(title):
             "id": title["id"],
             "name": title["name"],
             "site": "mangadex",
+            "cover_ext": title["cover_ext"],
             "chapters": json.dumps(title["chapters"]),
             "alt_names": json.dumps(title["alt_names"]),
             "descriptions": json.dumps(title["descriptions"]),
@@ -45,7 +49,7 @@ def load_title(title_id):
     result = list(
         conn.cursor().execute(
             """
-    SELECT id, name, site, chapters, alt_names, descriptions
+    SELECT id, name, site, cover_ext, chapters, alt_names, descriptions
     FROM title
     WHERE id = ?
       AND datetime(updated_at) > datetime('now', '-6 hours');
@@ -63,9 +67,10 @@ def load_title(title_id):
             "id": title[0],
             "name": title[1],
             "site": title[2],
-            "chapters": json.loads(title[3]),
-            "alt_names": json.loads(title[4]),
-            "descriptions": json.loads(title[5]),
+            "cover_ext": title[3],
+            "chapters": json.loads(title[4]),
+            "alt_names": json.loads(title[5]),
+            "descriptions": json.loads(title[6]),
         }
 
 
diff --git a/src/pytaku/templates/title.html b/src/pytaku/templates/title.html
index ad63a95..d0d098a 100644
--- a/src/pytaku/templates/title.html
+++ b/src/pytaku/templates/title.html
@@ -20,7 +20,7 @@ <h1>{{ name }}</h1>
 {{ ibutton(href='https://mangadex.org/manga/' + id, right_icon='arrow-up-right', text='Source site', color='blue') }}
 </div>
 
-<img class="cover" src="https://mangadex.org/images/manga/{{ id }}.jpg" alt="cover" />
+<img class="cover" src="https://mangadex.org/images/manga/{{ id }}.{{ cover_ext }}" alt="cover" />
 
 <table>
   <tr>