Repos / pytaku / efb3b6109f
commit efb3b6109fdd3eaeb3ba8d6e8a21eb93050c60b3
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Fri Sep 3 23:47:31 2021 +0700

    more mangadex bug fixes found during update

diff --git a/src/mangoapi/base_site.py b/src/mangoapi/base_site.py
index e308cea..b94fb36 100644
--- a/src/mangoapi/base_site.py
+++ b/src/mangoapi/base_site.py
@@ -46,7 +46,7 @@ def title_cover(self, title_id, cover_ext):
         pass
 
     @abstractmethod
-    def title_thumbnail(self, title_id):
+    def title_thumbnail(self, title_id, cover_ext):
         pass
 
     @abstractmethod
diff --git a/src/mangoapi/mangadex.py b/src/mangoapi/mangadex.py
index 7340e19..209eff4 100644
--- a/src/mangoapi/mangadex.py
+++ b/src/mangoapi/mangadex.py
@@ -35,15 +35,19 @@ def get_title(self, title_id):
             if rel["type"] == "cover_art":
                 cover = rel["attributes"]["fileName"]
 
+        descriptions = attrs["description"]
+        if "en" in descriptions:
+            description = descriptions["en"]
+        else:
+            description = list(descriptions.values())[0]
+
         title = {
             "id": title_id,
-            "name": attrs["title"]["en"],
+            "name": list(attrs["title"].values())[0],
             "site": "mangadex",
             "cover_ext": cover,
-            "alt_names": [alt["en"] for alt in attrs["altTitles"]],
-            "descriptions": [
-                _bbparser.format(html.unescape(attrs["description"]["en"]).strip())
-            ],
+            "alt_names": [list(alt.values())[0] for alt in attrs["altTitles"]],
+            "descriptions": [_bbparser.format(html.unescape(description).strip())],
             "descriptions_format": "html",
             "is_webtoon": is_web_comic,
             "chapters": self.get_chapters_list(title_id),
@@ -59,21 +63,29 @@ def get_chapters_list(self, title_id):
         volumes: dict = resp.json()["volumes"]
         chapters = []
 
-        # Counting on python's spanking new key-order-preserving dicts here.
-        # But WHY THE ACTUAL FUCK would you (mangadex) depend on JSON's key-value pairs ordering?
-        # A JSON object's keys is supposed to be unordered FFS.
-        # If it actually becomes a problem I'll do chapter sorting later. Soon. Ish.
+        # If there are no volumes, it's an empty list.
+        # But if there are actual volumes, it's a dict.
+        if type(volumes) is list:
+            return []
         for vol in volumes.values():
-            chapters += [
-                {
-                    "id": chap["id"],
-                    "name": "",
-                    "groups": [],  # TODO
-                    "volume": None if vol["volume"] == "none" else int(vol["volume"]),
-                    **_parse_chapter_number(chap["chapter"]),
-                }
-                for chap in vol["chapters"].values()  # again, fucking yikes
-            ]
+            # Counting on python's spanking new key-order-preserving dicts here.
+            # But WHY THE ACTUAL FUCK would you (mangadex) depend on JSON's key-value
+            # pairs ordering?  A JSON object's keys is supposed to be unordered FFS.
+            # If it actually becomes a problem I'll do chapter sorting later. Soon. Ish.
+            chapters += (
+                [
+                    {
+                        "id": chap["id"],
+                        "name": "",
+                        "groups": [],  # TODO
+                        "volume": vol["volume"],
+                        **_parse_chapter_number(chap["chapter"]),
+                    }
+                    for chap in vol["chapters"].values()  # again, fucking yikes
+                ]
+                if type(vol["chapters"]) is dict
+                else []
+            )
 
         return chapters
 
@@ -121,7 +133,12 @@ def _get_md_at_home_server(self, chapter_id):
         return resp.json()["baseUrl"] if resp.status_code == 200 else None
 
     def search_title(self, query):
-        params = {"limit": 100, "title": query, "includes[]": "cover_art"}
+        params = {
+            "limit": 100,
+            "title": query,
+            "includes[]": "cover_art",
+            "order[relevance]": "desc",
+        }
         md_resp = self.http_get("https://api.mangadex.org/manga", params=params)
         assert md_resp.status_code == 200
         results = md_resp.json()["results"]
@@ -147,8 +164,8 @@ def search_title(self, query):
     def title_cover(self, title_id, cover_ext):
         return f"https://uploads.mangadex.org/covers/{title_id}/{cover_ext}.256.jpg"
 
-    def title_thumbnail(self, title_id):
-        return f"https://mangadex.org/images/manga/{title_id}.large.jpg"
+    def title_thumbnail(self, title_id, cover_ext):
+        return f"https://uploads.mangadex.org/covers/{title_id}/{cover_ext}.256.jpg"
 
     def title_source_url(self, title_id):
         return f"https://mangadex.org/manga/{title_id}"
@@ -162,9 +179,9 @@ def title_source_url(self, title_id):
 
 
 def _parse_chapter_number(string):
-    if string == "none":
+    if string in (None, "none"):
         # most likely a oneshot
-        return {"number": ""}
+        return {"number": "0.0", "num_major": 0, "num_minor": 0}
     nums = string.split(".")
     count = len(nums)
     assert count == 1 or count == 2
diff --git a/src/mangoapi/mangasee.py b/src/mangoapi/mangasee.py
index ded8b8c..929b988 100644
--- a/src/mangoapi/mangasee.py
+++ b/src/mangoapi/mangasee.py
@@ -131,7 +131,7 @@ def search_title(self, query):
     def title_cover(self, title_id, cover_ext):
         return self.title_thumbnail(title_id)
 
-    def title_thumbnail(self, title_id):
+    def title_thumbnail(self, title_id, cover_ext):
         return f"https://cover.nep.li/cover/{title_id}.jpg"
 
     def title_source_url(self, title_id):
diff --git a/src/pytaku/main.py b/src/pytaku/main.py
index 8096c12..7246224 100644
--- a/src/pytaku/main.py
+++ b/src/pytaku/main.py
@@ -371,7 +371,7 @@ def api_logout():
 def api_follows():
     titles = get_followed_titles(request.user_id)
     for title in titles:
-        thumbnail = title_thumbnail(title["site"], title["id"])
+        thumbnail = title_thumbnail(title["site"], title["id"], title["cover_ext"])
         if title["site"] == "mangadex":
             thumbnail = proxied(thumbnail)
         title["thumbnail"] = thumbnail
diff --git a/src/pytaku/persistence.py b/src/pytaku/persistence.py
index 9b21cf1..f0bf43f 100644
--- a/src/pytaku/persistence.py
+++ b/src/pytaku/persistence.py
@@ -305,7 +305,11 @@ def unread(user_id, site, title_id, chapter_id):
 
 def find_outdated_titles(since=f"-{config.MANGA_HOURS_UNTIL_OUTDATED} hours"):
     return run_sql(
-        "SELECT id, site FROM title WHERE updated_at <= datetime('now', ?);", (since,)
+        """
+        SELECT id, name, site FROM title WHERE updated_at <= datetime('now', ?)
+        ORDER BY updated_at;
+        """,
+        (since,),
     )
 
 
diff --git a/src/pytaku/scheduler.py b/src/pytaku/scheduler.py
index 07255b8..c25bcc4 100644
--- a/src/pytaku/scheduler.py
+++ b/src/pytaku/scheduler.py
@@ -67,11 +67,10 @@ def run(self):
         outdated_titles = find_outdated_titles()
         print(f"Found {len(outdated_titles)} outdated titles")
         for title in outdated_titles:
-            if title["site"] == "mangadex":
-                # print(f"Skipped title {title['id']} from {title['site']}.")
-                continue
-
-            print(f"Updating title {title['id']} from {title['site']}...", end="")
+            print(
+                f"Updating title {title['id']} - {title['name'][:36]} from {title['site']}...",
+                end="",
+            )
             try:
                 updated_title = get_title(title["site"], title["id"])
                 save_title(updated_title)
diff --git a/src/pytaku/source_sites.py b/src/pytaku/source_sites.py
index 62f0438..edc1821 100644
--- a/src/pytaku/source_sites.py
+++ b/src/pytaku/source_sites.py
@@ -45,8 +45,8 @@ def title_cover(site_name, title_id, cover_ext):
     return _get_site(site_name).title_cover(title_id, cover_ext)
 
 
-def title_thumbnail(site_name, title_id):
-    return _get_site(site_name).title_thumbnail(title_id)
+def title_thumbnail(site_name, title_id, cover_ext):
+    return _get_site(site_name).title_thumbnail(title_id, cover_ext)
 
 
 def title_source_url(site_name, title_id):