From 02976a2d952badc0429dc965e7b67086c7a8a5e1 Mon Sep 17 00:00:00 2001 From: Orionis Date: Mon, 16 Nov 2026 23:34:04 +0100 Subject: [PATCH] =?UTF-8?q?:bug:=20Correction=20de=20la=20r=C3=A9cup=C3=A9?= =?UTF-8?q?ration=20chapitre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/scalper/sources/mangadex.py | 27 +++++++++++++--------- tests/test_mangadex.py | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/src/scalper/sources/mangadex.py b/src/scalper/sources/mangadex.py index 5563e26..b7beb38 100644 --- a/src/scalper/sources/mangadex.py +++ b/src/scalper/sources/mangadex.py @@ -91,18 +91,23 @@ class MangaDexAdapter: dest_dir.mkdir(parents=True, exist_ok=True) at_home = self._get_json(f"/at-home/server/{chapter.id}") base = at_home["baseUrl"] - data = self._get_json(f"/chapter/{chapter.id}") - attrs = data["data"]["attributes"] - chapter_hash = attrs["hash"] - pages = attrs.get("pages", 0) - files = attrs.get("data", []) + # Le payload at-home embarque hash/data/dataSaver ; repli sur /chapter/{id} + # (qui peut ne pas encore exposer hash/data juste après une publication). + payload = at_home.get("chapter") or {} + attrs = self._get_json(f"/chapter/{chapter.id}")["data"]["attributes"] + chapter_hash = payload.get("hash") or attrs.get("hash") + pages = attrs.get("pages") or payload.get("pages") or 0 + files = payload.get("data") or attrs.get("data") or [] lines = [f"# raw chapter {chapter.id} ({chapter.title})", ""] - img_dir = dest_dir / "images" - img_dir.mkdir(exist_ok=True) - for i, fname in enumerate(files, start=1): - url = f"{base}/data/{chapter_hash}/{fname}" - (img_dir / fname).write_bytes(self.fetcher.get(url)) - lines.append(f"![page {i}](images/{fname})") + if chapter_hash and files: + img_dir = dest_dir / "images" + img_dir.mkdir(exist_ok=True) + for i, fname in enumerate(files, start=1): + url = f"{base}/data/{chapter_hash}/{fname}" + (img_dir / fname).write_bytes(self.fetcher.get(url)) + lines.append(f"![page {i}](images/{fname})") + else: + lines.append("(images non disponibles pour ce chapitre pour le moment)") (dest_dir / "raw.html").write_text("\n".join(lines), encoding="utf-8") (dest_dir / "meta.json").write_text( json.dumps( diff --git a/tests/test_mangadex.py b/tests/test_mangadex.py index f80c5fc..1aeea5c 100644 --- a/tests/test_mangadex.py +++ b/tests/test_mangadex.py @@ -1,7 +1,10 @@ import json +import tempfile import unittest +from pathlib import Path from urllib.parse import urlparse, parse_qs +from scalper.models import ChapterRef from scalper.sources.mangadex import MangaDexAdapter GROUP_UUID = "123e4567-e89b-42d3-a456-426614174000" @@ -63,5 +66,42 @@ class TestMangaDexGroupFilter(unittest.TestCase): self.assertNotIn("translatedLanguage[]", qs) +class TestFetchChapter(unittest.TestCase): + def _adapter(self, at_home_chapter, chapter_attrs) -> tuple[MangaDexAdapter, StubFetcher]: + stub = StubFetcher( + { + "/at-home/server/": {"result": "ok", "baseUrl": "https://img.example", "chapter": at_home_chapter}, + "/chapter/ch-1": {"data": {"attributes": chapter_attrs}}, + "img.example": {"fake": "image-bytes"}, + } + ) + return MangaDexAdapter(fetcher=stub), stub + + def test_fetch_chapter_uses_at_home_payload(self): + adapter, _ = self._adapter( + {"hash": "abc123", "data": ["p1.png", "p2.png"], "dataSaver": []}, + {"pages": 2}, + ) + with tempfile.TemporaryDirectory() as tmp: + ref = ChapterRef(id="ch-1", number="1", title="Start", url="https://mangadex.org/chapter/ch-1") + adapter.fetch_chapter(ref, Path(tmp)) + raw = (Path(tmp) / "raw.html").read_text(encoding="utf-8") + self.assertIn("![page 1](images/p1.png)", raw) + self.assertIn("![page 2](images/p2.png)", raw) + self.assertTrue((Path(tmp) / "images" / "p1.png").exists()) + meta = json.loads((Path(tmp) / "meta.json").read_text(encoding="utf-8")) + self.assertEqual(meta["pages"], 2) + self.assertEqual(meta["source"], "mangadex") + + def test_fetch_chapter_without_hash_does_not_crash(self): + adapter, _ = self._adapter({}, {"pages": 0}) + with tempfile.TemporaryDirectory() as tmp: + ref = ChapterRef(id="ch-1", number="1", title="Start", url="https://mangadex.org/chapter/ch-1") + adapter.fetch_chapter(ref, Path(tmp)) + raw = (Path(tmp) / "raw.html").read_text(encoding="utf-8") + self.assertIn("images non disponibles", raw) + self.assertFalse((Path(tmp) / "images").exists()) + + if __name__ == "__main__": unittest.main()