🐛 Correction de la récupération chapitre

This commit is contained in:
Orionis committed 2026-11-16 23:34:04 +01:00
1 parent 902529a4ea
commit 02976a2d95
2 files changed
+50 -5

No files matched your search

+10 -5
View File
@@ -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})", ""]
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(
+40
View File
@@ -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()