~nhanb/pytaku

ref: fd3f4fbbe8130926062f2e667f9692380edef507 pytaku/src/mangoapi/mangasee.py -rw-r--r-- 7.4 KiB
fd3f4fbbBùi Thành Nhân apparently builds.srht now has branch in tags? 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import json
import re

import apsw

from mangoapi.base_site import Site

regexes = {
    "title_name": re.compile(r"<title>\s*([^|]+) | MangaSee</title>"),
    "title_chapters": re.compile(r"vm\.Chapters = (\[[^\]]+\])"),
    "title_desc": re.compile(
        r"<span +class=\"mlabel\">Description:</span>[^<]+<div[^>]*>([^<]+)<"
    ),
    "chapter_title_name": re.compile(r'vm\.IndexName = "([^"]+)"'),
    "chapter_data": re.compile(r"vm\.CurChapter = (\{[^\}]+\})"),
    "chapter_img_server": re.compile(r'vm\.CurPathNamez? = "([^"]+)"'),
}


class Mangasee(Site):
    search_table = None

    def __init__(self, keyval_store=None):
        super().__init__()
        self.keyval_store = keyval_store

    def get_title(self, title_id):
        resp = self.http_get(f"https://mangasee123.com/manga/{title_id}")
        html = resp.text
        name = regexes["title_name"].search(html).group(1).strip()
        desc = regexes["title_desc"].search(html).group(1).strip()
        chapters_str = regexes["title_chapters"].search(html).group(1)
        chapters = []
        for ch in json.loads(chapters_str):
            numbers = _parse_chapter_number(ch["Chapter"])
            chapters.append(
                {
                    "id": numbers["raw_id"],
                    "name": ch["ChapterName"],
                    "volume": "",
                    "groups": [],
                    **numbers,
                }
            )
        return {
            "id": title_id,
            "name": name,
            "site": "mangasee",
            "cover_ext": "jpg",
            "is_webtoon": False,
            "chapters": chapters,
            "alt_names": [],
            "descriptions": [desc],
            "descriptions_format": "text",
        }

    def get_chapter(self, title_id, chapter_id):
        numbers = _parse_chapter_number(chapter_id)
        index = chapter_id[0]
        suffix = "" if index == "1" else f"-index-{index}"
        url = f"https://mangasee123.com/read-online/{title_id}-chapter-{numbers['number']}{suffix}.html"
        resp = self.http_get(url)
        html = resp.text

        title_id = regexes["chapter_title_name"].search(html).group(1)
        chapter_data = json.loads(regexes["chapter_data"].search(html).group(1))
        num_pages = int(chapter_data["Page"])
        directory = chapter_data["Directory"]
        img_server = regexes["chapter_img_server"].search(html).group(1)
        img_server = regexes["chapter_img_server"].search(html).group(1)

        result = {
            "id": chapter_id,
            "title_id": title_id,
            "site": "mangasee",
            "name": chapter_data["ChapterName"] or "",
            "pages": [
                _generate_img_src(
                    img_server, title_id, chapter_data["Chapter"], directory, p
                )
                for p in range(1, num_pages + 1)
            ],
            "pages_alt": [],
            "groups": [],
            **numbers,
        }
        return result

    def search_title(self, query):
        """
        Json blob of all mangasee titles: https://mangasee123.com/_search.php
        The structure is something like this:
            [
                {
                'i': '',
                's': '',
                'a': ['', '']
                }
            ]
        Where `i` is id, `s` is name, and `a` is a list of alternative names.
        So we can just read that once and build an in-memory sqlite db for offline full
        text search.

        Additionally, if we have a local key-value store, try that before actually
        sending an http request to mangasee.
        """
        if not self.search_table:
            titles = None
            if self.keyval_store:
                titles = json.loads(
                    self.keyval_store.get("mangasee_titles", "null", since="-20 day")
                )
            if not titles:
                print("Fetching mangasee title list...", end="")
                resp = self.http_get("https://mangasee123.com/_search.php")
                print(" done")
                titles = resp.json()
                self.keyval_store.set("mangasee_titles", resp.text)
            self.search_table = SearchTable(titles)

        return [
            {
                "id": row[0],
                "name": row[1],
                "site": "mangasee",
                "thumbnail": f"https://cover.nep.li/cover/{row[0]}.jpg",
            }
            for row in self.search_table.search(query)
        ]

    def title_cover(self, title_id, cover_ext):
        return self.title_thumbnail(title_id, cover_ext)

    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):
        return f"https://mangasee123.com/manga/{title_id}"


class SearchTable:
    def __init__(self, titles: list):
        self.db = apsw.Connection(":memory:")
        cursor = self.db.cursor()
        cursor.execute(
            "CREATE VIRTUAL TABLE titles USING FTS5(id UNINDEXED, name, alt_names);"
        )

        rows = []
        for t in titles:
            id = t["i"]
            name = t["s"]
            alt_names = t["a"]
            rows.append((id, name, " ".join(alt_names)))

        cursor.executemany(
            "INSERT INTO titles(id, name, alt_names) VALUES(?,?,?);", rows
        )

    def search(self, query):
        query = '"' + query.replace('"', '""') + '"'
        return self.db.cursor().execute(
            "SELECT id, name FROM titles WHERE titles MATCH ? ORDER BY rank;", (query,)
        )


def _parse_chapter_number(e):
    """
    Mangasee author tries to be clever with obtuse chapter numbers that must be decoded
    via javascript:

        (vm.ChapterDisplay = function (e) {
        var t = parseInt(e.slice(1, -1)),
            n = e[e.length - 1];
        return 0 == n ? t : t + "." + n;
        })

        Example: vm.ChapterDisplay('100625') === '62.5'

    No idea why tbh.
    """
    major = int(e[1:-1])
    minor = int(e[-1])
    result = {
        "num_major": major,
        "number": str(major) if not minor else f"{major}.{minor}",
        "raw_id": e,
    }
    if minor:
        result["num_minor"] = minor
    return result


def _chapter_url(e):
    """
    Yet another bright idea:

        (vm.ChapterURLEncode = function (e) {
            Index = "";
            var t = e.substring(0, 1);
            1 != t && (Index = "-index-" + t);
            var n = parseInt(e.slice(1, -1)),
            m = "",
            a = e[e.length - 1];
            return (
            0 != a && (m = "." + a),
            "-chapter-" + n + m + Index + vm.PageOne + ".html"
            );
        }),

    e.g. vm.ChapterURLEncode("201420") === "-chapter-142-index-2-page-1.html"
    """


def _generate_img_src(img_srv, title_id, chapter_id, directory, page):
    """
    Chapter ID padding logic:

        vm.ChapterImage = function (ChapterString) {
          var Chapter = ChapterString.slice(1, -1);
          var Odd = ChapterString[ChapterString.length - 1];
          if (Odd == 0) {
            return Chapter;
          } else {
            return Chapter + "." + Odd;
          }
        };
    """
    chapter = chapter_id[1:-1]
    odd = chapter_id[len(chapter_id) - 1]
    if odd == "0":
        padded_chapter = chapter
    else:
        padded_chapter = f"{chapter}.{odd}"

    directory = f"{directory}/" if directory else ""
    return (
        f"https://{img_srv}/manga/{title_id}/{directory}{padded_chapter}-{page:03d}.png"
    )