From ca76bdd564d12d2de27d2d1e48d473de67d8e9d4 Mon Sep 17 00:00:00 2001 From: Michael Lehmann Date: Sun, 5 Oct 2025 15:58:46 +0200 Subject: [PATCH] Add generate epub function. --- main.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 9635482..0c5dae4 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,10 @@ import re +import secrets import tempfile from pathlib import Path -from requests import HTTPError, request +from ebooklib import epub # type: ignore +from requests import HTTPError, auth, request def parse_issuu_url(url: str) -> tuple[str, str]: @@ -66,6 +68,67 @@ def download_pages(page_urls: list[str], working_dir: Path) -> list[Path]: return page_paths +def generate_epub( + pages: list[Path], output_file: Path, title: str, author: str +) -> None: + """generate epub file.""" + book = epub.EpubBook() + book.set_identifier(secrets.token_urlsafe(10)) + book.set_title(title=title) + book.set_language("de") + book.add_author(author=author) + + chapters = [] + + for i, page in enumerate(pages, start=1): + page_title = f"Page {i}" + + image_item = epub.EpubImage() + image_item.file_name = page.as_posix() + image_item.media_type = "image/png" + book.add_item(image_item) + + chapter = epub.EpubHtml( + title=page_title, file_name=f"page_{i}.xhtml", lang="de" + ) + + chapter.content = f""" + + + {page_title} + + + + Page {i} + + + """ + + book.add_item(chapter) + chapters.append(chapter) + + book.toc = chapters + book.spine = ["nav"] + chapters + + book.add_item(epub.EpubNcx()) + book.add_item(epub.EpubNav()) + + epub.write_epub(output_file, book, {}) + + print(f"EPUB erfolgreich erstellt: {output_file}") + + def main() -> None: """main function.""" cwd = create_working_dir() @@ -75,7 +138,14 @@ def main() -> None: urls = get_page_urls(username, document_id) - print(download_pages(urls, cwd)) + pages = download_pages(urls, cwd) + + generate_epub( + pages=pages, + output_file=Path("/home/michael/Downloads/YBMag-2025-01.epub"), + title="YB Mag 2025 01", + author="BSC YB", + ) if __name__ == "__main__":