From fdd9f3c17f6e8b91b47e3704e081c2b51c474be2 Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Tue, 10 Jun 2025 14:18:42 +0200 Subject: [PATCH] feat: fetching assets for chapter --- scribble_to_epub/__main__.py | 3 ++- scribble_to_epub/scribblehub.py | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/scribble_to_epub/__main__.py b/scribble_to_epub/__main__.py index 2683fc6..f6b8330 100644 --- a/scribble_to_epub/__main__.py +++ b/scribble_to_epub/__main__.py @@ -18,7 +18,8 @@ def cli(): print(f"Running scribble_to_epub for URL: {args.url}") - ScribbleBook(args.url) + scribble_book = ScribbleBook(args.url) + scribble_book.load(limit_chapters=1) if __name__ == "__main__": diff --git a/scribble_to_epub/scribblehub.py b/scribble_to_epub/scribblehub.py index f1e17ec..dfc7695 100644 --- a/scribble_to_epub/scribblehub.py +++ b/scribble_to_epub/scribblehub.py @@ -241,16 +241,7 @@ class ScribbleBook: self.tags = [] self.chapters: List[ScribbleChapter] = [] - - # fetching metadata self.session = cloudscraper.create_scraper() - self.load_metadata() - print(str(self)) - - self.get_chapters() - c = self.chapters[0] - c.load() - print(c.text) def add_asset(self, url: str): if url is None: @@ -264,6 +255,16 @@ class ScribbleBook: else: log.warning(f"couldn't fetch asset {url}") + def load(self, limit_chapters: Optional[int] = None): + self.load_metadata() + print(str(self)) + + self.fetch_chapters(limit=limit_chapters) + for chapter in self.chapters: + print(str(chapter)) + chapter.load() + + def load_metadata(self) -> None: """ Load the metadata for this object @@ -315,7 +316,7 @@ class ScribbleBook: continue self.rights = ftfy.fix_text(img.next.string) - def get_chapters(self) -> None: + def fetch_chapters(self, limit: Optional[int] = None) -> None: """ Fetch the chapters for the work, based on the TOC API """ @@ -324,6 +325,9 @@ class ScribbleBook: f"Expecting {self.chapter_count} chapters, page_count={page_count}" ) + if limit is not None: + page_count = min(page_count, limit) + for page in range(1, page_count + 1): chapter_resp = self.session.post( "https://www.scribblehub.com/wp-admin/admin-ajax.php", @@ -346,6 +350,3 @@ class ScribbleBook: self.chapters.append(chapter) self.chapters.sort(key=lambda x: x.index) - - for c in self.chapters: - print(str(c))