Add generate epub function.
This commit is contained in:
74
main.py
74
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"""
|
||||
<html>
|
||||
<head>
|
||||
<title>{page_title}</title>
|
||||
<style>
|
||||
body {{
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
text-align: center;
|
||||
}}
|
||||
img {{
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
max-height: 90vh;
|
||||
}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<img src="{image_item.file_name}" alt="Page {i}"/>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
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__":
|
||||
|
||||
Reference in New Issue
Block a user