~nhanb/pytaku

7d6b58d3258aa0321f6e56dfff49a81791c16151 — Nhân 10 months ago e658918
bring back mangadex chapter names

Turns out there's another API that lists chapters. Oh well.
2 files changed, 38 insertions(+), 33 deletions(-)

M src/mangoapi/mangadex.py
M tests/mangoapi/test_mangadex.py
M src/mangoapi/mangadex.py => src/mangoapi/mangadex.py +28 -31
@@ 59,39 59,36 @@ class Mangadex(Site):
        return title

    def get_chapters_list(self, title_id):
        resp = self.http_get(
            f"https://api.mangadex.org/manga/{title_id}/aggregate",
            params={"translatedLanguage[]": "en"},
        )
        assert resp.status_code == 200
        volumes: dict = resp.json()["volumes"]
        chapters = []

        # 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():
            # 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"]
                        if vol["volume"] not in (None, "none")
                        else "",
                        **_parse_chapter_number(chap["chapter"]),
                    }
                    for chap in vol["chapters"].values()  # again, fucking yikes
                ]
                if type(vol["chapters"]) is dict
                else []
        offset = 0
        limit = 100  # max allowed by mangadex api; will be 400 if we try any higher
        while True:
            resp = self.http_get(
                "https://api.mangadex.org/chapter",
                params={
                    "manga": title_id,
                    "translatedLanguage[]": "en",
                    "order[chapter]": "desc",
                    "offset": offset,
                    "limit": limit,
                },
            )
            assert resp.status_code == 200
            body = resp.json()
            chapters += [
                {
                    "id": chap["id"],
                    "name": chap["attributes"]["title"],
                    "groups": [],  # TODO
                    "volume": chap["attributes"]["volume"],
                    **_parse_chapter_number(chap["attributes"]["chapter"]),
                }
                for chap in body["data"]
            ]

            offset += limit
            if len(chapters) < limit or offset >= body["total"]:
                break

        return chapters


M tests/mangoapi/test_mangadex.py => tests/mangoapi/test_mangadex.py +10 -2
@@ 25,15 25,23 @@ def test_get_title():
        "is_webtoon": False,
    }

    assert len(chapters) == 252
    assert len(chapters) == 251
    assert chapters[1] == {
        "id": "dfd3fd5a-a20f-460f-b726-e9cb168bfe3d",
        "name": "",
        "name": "Love Blooms in The Season of Partings",
        "volume": "28",
        "groups": [],
        "number": "245",
        "num_major": 245,
    }
    assert chapters[-1] == {
        "groups": [],
        "id": "91ee8504-db4e-4b67-ac89-8fb291f67f7e",
        "name": "I Picked up the Devil King",
        "num_major": 1,
        "number": "1",
        "volume": "1",
    }


def test_get_title_webtoon():