Repos / pytaku / c3d89aecc6
commit c3d89aecc60941ac2ff9f8c1ff0cb472dab3b4b9
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Fri Jan 22 14:55:32 2021 +0700

    move is_webtoon logic from chapter to title
    
    Eventually we should properly implement tags.

diff --git a/pyproject.toml b/pyproject.toml
index d79d58c..bcb2885 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "pytaku"
-version = "0.3.33"
+version = "0.3.34"
 description = "Self-hostable web-based manga reader"
 authors = ["Bùi Thành Nhân <hi@imnhan.com>"]
 license = "AGPL-3.0-only"
diff --git a/src/mangoapi/mangadex.py b/src/mangoapi/mangadex.py
index 336bdfd..c03b350 100644
--- a/src/mangoapi/mangadex.py
+++ b/src/mangoapi/mangadex.py
@@ -5,6 +5,7 @@
 from mangoapi.base_site import Site, requires_login
 
 MANGAPLUS_GROUP_ID = 9097
+LONG_STRIP_TAG_ID = 36
 
 
 class Mangadex(Site):
@@ -30,6 +31,7 @@ def get_title(self, title_id):
             "cover_ext": cover_ext,
             "alt_names": manga["altTitles"],
             "descriptions": html.unescape(manga["description"]).split("\r\n\r\n"),
+            "is_webtoon": LONG_STRIP_TAG_ID in manga["tags"],
             "chapters": [
                 {
                     "id": str(chap["id"]),
@@ -89,8 +91,6 @@ def get_chapter(self, title_id, chapter_id):
             if mdah_server
             else [],
             "groups": [html.unescape(group["name"]) for group in data["groups"]],
-            # TODO: longStrip doesn't exist in v2 API yet. Nagging for it on Discord.
-            "is_webtoon": bool(data.get("longString")),
             **_parse_chapter_number(data["chapter"]),
         }
         return chapter
diff --git a/src/mangoapi/mangasee.py b/src/mangoapi/mangasee.py
index a34fe54..c531989 100644
--- a/src/mangoapi/mangasee.py
+++ b/src/mangoapi/mangasee.py
@@ -47,6 +47,7 @@ def get_title(self, title_id):
             "name": name,
             "site": "mangasee",
             "cover_ext": "jpg",
+            "is_webtoon": False,
             "chapters": chapters,
             "alt_names": [],
             "descriptions": [desc],
@@ -76,7 +77,6 @@ def get_chapter(self, title_id, chapter_id):
             ],
             "pages_alt": [],
             "groups": [],
-            "is_webtoon": False,
             **numbers,
         }
         return result
diff --git a/src/pytaku/database/migrations/latest_schema.sql b/src/pytaku/database/migrations/latest_schema.sql
index 307e5a7..c684f1b 100644
--- a/src/pytaku/database/migrations/latest_schema.sql
+++ b/src/pytaku/database/migrations/latest_schema.sql
@@ -9,7 +9,7 @@ CREATE TABLE title (
     chapters text,
     alt_names text,
     descriptions text,
-    updated_at text default (datetime('now')),
+    updated_at text default (datetime('now')), is_webtoon boolean not null default false,
 
     unique(id, site)
 );
@@ -53,7 +53,7 @@ CREATE TABLE IF NOT EXISTS "read" (
     foreign key (user_id) references user (id),
     unique(user_id, site, title_id, chapter_id)
 );
-CREATE TABLE IF NOT EXISTS "chapter"(
+CREATE TABLE IF NOT EXISTS "chapter" (
     id text,
     title_id text,
     site text,
@@ -63,8 +63,7 @@ CREATE TABLE IF NOT EXISTS "chapter"(
     pages text,
     groups text,
     updated_at text default (datetime('now')),
-    is_webtoon boolean, pages_alt text not null default '[]',
-
+    pages_alt text not null default '[]',
     foreign key (title_id, site) references title (id, site),
     unique(site, title_id, id)
 );
diff --git a/src/pytaku/database/migrations/m0008.sql b/src/pytaku/database/migrations/m0008.sql
new file mode 100644
index 0000000..fa872cd
--- /dev/null
+++ b/src/pytaku/database/migrations/m0008.sql
@@ -0,0 +1,40 @@
+-- Move is_webtoon column from chapter to title.
+-- No need to migrate existing data: scheduled title updates will populate the
+-- correct data in no time.
+
+pragma foreign_keys = off; -- to let us do anything at all
+begin transaction;
+
+-- Easy part: add is_webtoon column to "title" table
+alter table title add column is_webtoon boolean not null default false;
+
+-- Clumsy part: remove is_webtoon column from "chapter" table
+---- 1. create new
+create table new_chapter (
+    id text,
+    title_id text,
+    site text,
+    num_major integer,
+    num_minor integer,
+    name text,
+    pages text,
+    groups text,
+    updated_at text default (datetime('now')),
+    pages_alt text not null default '[]',
+    foreign key (title_id, site) references title (id, site),
+    unique(site, title_id, id)
+);
+---- 2. copy from old to new
+insert into new_chapter (id, title_id, site, num_major, num_minor, name,
+                         pages, groups, updated_at, pages_alt)
+    select id, title_id, site, num_major, num_minor, name, pages, groups,
+           updated_at, pages_alt
+    from chapter;
+---- 3. drop old
+drop table chapter;
+---- 4. rename new
+alter table new_chapter rename to chapter;
+
+pragma foreign_key_check;
+commit;
+pragma foreign_keys = on;
diff --git a/src/pytaku/persistence.py b/src/pytaku/persistence.py
index fa78bd9..aa77aca 100644
--- a/src/pytaku/persistence.py
+++ b/src/pytaku/persistence.py
@@ -16,6 +16,7 @@ def save_title(title):
         name,
         site,
         cover_ext,
+        is_webtoon,
         chapters,
         alt_names,
         descriptions
@@ -24,12 +25,14 @@ def save_title(title):
         :name,
         :site,
         :cover_ext,
+        :is_webtoon,
         :chapters,
         :alt_names,
         :descriptions
     ) ON CONFLICT (id, site) DO UPDATE SET
         name=excluded.name,
         cover_ext=excluded.cover_ext,
+        is_webtoon=excluded.is_webtoon,
         chapters=excluded.chapters,
         alt_names=excluded.alt_names,
         descriptions=excluded.descriptions,
@@ -41,6 +44,7 @@ def save_title(title):
             "name": title["name"],
             "site": title["site"],
             "cover_ext": title["cover_ext"],
+            "is_webtoon": title["is_webtoon"],
             "chapters": json.dumps(title["chapters"]),
             "alt_names": json.dumps(title["alt_names"]),
             "descriptions": json.dumps(title["descriptions"]),
@@ -51,7 +55,7 @@ def save_title(title):
 def load_title(site, title_id, user_id=None):
     result = run_sql(
         """
-        SELECT id, name, site, cover_ext, chapters, alt_names, descriptions
+        SELECT id, name, site, cover_ext, is_webtoon, chapters, alt_names, descriptions
         FROM title
         WHERE id = ?
           AND site = ?;
@@ -106,8 +110,7 @@ def save_chapter(chapter):
         name,
         pages,
         pages_alt,
-        groups,
-        is_webtoon
+        groups
     ) VALUES (
         :id,
         :title_id,
@@ -117,8 +120,7 @@ def save_chapter(chapter):
         :name,
         :pages,
         :pages_alt,
-        :groups,
-        :is_webtoon
+        :groups
     ) ON CONFLICT (id, title_id, site) DO UPDATE SET
         num_major=excluded.num_major,
         num_minor=excluded.num_minor,
@@ -126,7 +128,6 @@ def save_chapter(chapter):
         pages=excluded.pages,
         pages_alt=excluded.pages_alt,
         groups=excluded.groups,
-        is_webtoon=excluded.is_webtoon,
         updated_at=datetime('now')
     ;
     """,
@@ -140,7 +141,6 @@ def save_chapter(chapter):
             "pages": json.dumps(chapter["pages"]),
             "pages_alt": json.dumps(chapter["pages_alt"]),
             "groups": json.dumps(chapter["groups"]),
-            "is_webtoon": chapter["is_webtoon"],
         },
     )
 
@@ -149,7 +149,7 @@ def load_chapter(site, title_id, chapter_id, ignore_old=True):
     updated_at = "datetime('now', '-1 days')" if ignore_old else "'1980-01-01'"
     result = run_sql(
         f"""
-        SELECT id, title_id, site, num_major, num_minor, name, pages, pages_alt, groups, is_webtoon
+        SELECT id, title_id, site, num_major, num_minor, name, pages, pages_alt, groups
         FROM chapter
         WHERE site=? AND title_id=? AND id=? AND updated_at > {updated_at};
         """,
diff --git a/tests/mangoapi/test_mangadex.py b/tests/mangoapi/test_mangadex.py
index 1dec5a5..8815031 100644
--- a/tests/mangoapi/test_mangadex.py
+++ b/tests/mangoapi/test_mangadex.py
@@ -10,6 +10,7 @@ def test_get_title():
         "site": "mangadex",
         "cover_ext": "jpg",
         "alt_names": ["Adiós al fútbol", "さよならフットボール", "再见足球"],
+        "is_webtoon": False,
         "descriptions": [
             "Nozomi wants to enter the newcomer's competition. But the coach is against it, because their club is a boy's football club and she's... a she. Will she be able to enter the match she wants to play in?"
         ],
@@ -82,6 +83,11 @@ def test_get_title():
     }
 
 
+def test_get_title_webtoon():
+    title = Mangadex().get_title("1")
+    assert title["is_webtoon"] is True
+
+
 def test_get_chapter():
     chap = Mangadex().get_chapter("doesn't matter", "696882")
     pages = chap.pop("pages")
@@ -92,7 +98,6 @@ def test_get_chapter():
         "site": "mangadex",
         "name": "Extras",
         "groups": ["Träumerei Scans", "GlassChair"],
-        "is_webtoon": False,
         "number": "81.5",
         "num_major": 81,
         "num_minor": 5,