🐛 Correction de la récupération chapitre
This commit is contained in:
1 parent
902529a4ea
commit
02976a2d95
2 files changed
+56
-11
No files matched your search
@@ -91,18 +91,23 @@ class MangaDexAdapter:
|
|||||||
dest_dir.mkdir(parents=True, exist_ok=True)
|
dest_dir.mkdir(parents=True, exist_ok=True)
|
||||||
at_home = self._get_json(f"/at-home/server/{chapter.id}")
|
at_home = self._get_json(f"/at-home/server/{chapter.id}")
|
||||||
base = at_home["baseUrl"]
|
base = at_home["baseUrl"]
|
||||||
data = self._get_json(f"/chapter/{chapter.id}")
|
# Le payload at-home embarque hash/data/dataSaver ; repli sur /chapter/{id}
|
||||||
attrs = data["data"]["attributes"]
|
# (qui peut ne pas encore exposer hash/data juste après une publication).
|
||||||
chapter_hash = attrs["hash"]
|
payload = at_home.get("chapter") or {}
|
||||||
pages = attrs.get("pages", 0)
|
attrs = self._get_json(f"/chapter/{chapter.id}")["data"]["attributes"]
|
||||||
files = attrs.get("data", [])
|
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})", ""]
|
lines = [f"# raw chapter {chapter.id} ({chapter.title})", ""]
|
||||||
img_dir = dest_dir / "images"
|
if chapter_hash and files:
|
||||||
img_dir.mkdir(exist_ok=True)
|
img_dir = dest_dir / "images"
|
||||||
for i, fname in enumerate(files, start=1):
|
img_dir.mkdir(exist_ok=True)
|
||||||
url = f"{base}/data/{chapter_hash}/{fname}"
|
for i, fname in enumerate(files, start=1):
|
||||||
(img_dir / fname).write_bytes(self.fetcher.get(url))
|
url = f"{base}/data/{chapter_hash}/{fname}"
|
||||||
lines.append(f"")
|
(img_dir / fname).write_bytes(self.fetcher.get(url))
|
||||||
|
lines.append(f"")
|
||||||
|
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 / "raw.html").write_text("\n".join(lines), encoding="utf-8")
|
||||||
(dest_dir / "meta.json").write_text(
|
(dest_dir / "meta.json").write_text(
|
||||||
json.dumps(
|
json.dumps(
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
import json
|
import json
|
||||||
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
from pathlib import Path
|
||||||
from urllib.parse import urlparse, parse_qs
|
from urllib.parse import urlparse, parse_qs
|
||||||
|
|
||||||
|
from scalper.models import ChapterRef
|
||||||
from scalper.sources.mangadex import MangaDexAdapter
|
from scalper.sources.mangadex import MangaDexAdapter
|
||||||
|
|
||||||
GROUP_UUID = "123e4567-e89b-42d3-a456-426614174000"
|
GROUP_UUID = "123e4567-e89b-42d3-a456-426614174000"
|
||||||
@@ -63,5 +66,42 @@ class TestMangaDexGroupFilter(unittest.TestCase):
|
|||||||
self.assertNotIn("translatedLanguage[]", qs)
|
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("", raw)
|
||||||
|
self.assertIn("", 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__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
Reference in new issue
Block a user