From be6bfb10cd798780c53d60ba8f84f4583818d7cb Mon Sep 17 00:00:00 2001 From: Michael Lehmann Date: Mon, 6 Oct 2025 23:14:56 +0200 Subject: [PATCH] Update --- README.md | 51 ++++++++++++++++++--- devenv.nix | 1 + main.py => issuu2epub.py | 95 +++++++++++++++++++++++++++++++++------- 3 files changed, 126 insertions(+), 21 deletions(-) rename main.py => issuu2epub.py (59%) diff --git a/README.md b/README.md index 5e62b69..20c0ed8 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,54 @@ -# issuu2epub +# Issuu to EPUB Converter -Download Documents from Issuu as EPUB Files. +Ein Python-Skript zum Konvertieren von Issuu-Dokumenten in EPUB-Dateien, +optimiert für Kindle E-Reader. + +## 📋 Übersicht + +Dieses Tool lädt Dokumente von Issuu herunter und konvertiert sie in das +EPUB-Format. Das erste Bild wird als Cover verwendet, und alle Bilder werden für +optimale Darstellung auf E-Readern komprimiert. + +## ✨ Features + +- **Einfache Befehlszeilenschnittstelle** mit Click +- **Automatisches Cover** aus der ersten Seite +- **Kindle-optimierte** Bildkompression +- **Kein Inhaltsverzeichnis** für direkten Zugriff auf den Inhalt +- **Temporäre Verzeichnisse** für saubere Verarbeitung +- **Unterstützt große Dokumente** mit Stream-Download + +## 🚀 Verwendung + +### Einfache Verwendung + +```bash +python issuu2epub.py +``` + +Das Skript fragt interaktiv nach allen benötigten Informationen: + +1. Issuu URL: Die URL des Issuu-Dokuments +2. Document Title: Titel des Dokuments +3. Document Author: Autor des Dokuments +4. EPUB Output Filename: Name der Ausgabedatei (z.B. mein_dokument.epub) + +### Direkte Befehlszeilenverwendung + +```bash +python issuu2epub.py --url "https://issuu.com/bscyb1898/docs/yb_mag_nr._1_saison_2025_26" --title "YB Mag 2025 01" --author "BSC YB" --output "YB_Mag_2025_01.epub" +``` ## Notes -- Educational Use Only: These tools are intended for educational purposes. - Always respect the terms of service of Issuu and other websites. -- Responsibility: The repository author is not responsible for any misuse of - these tools. +Dieses Tool ist für den persönlichen Gebrauch bestimmt. Stellen Sie sicher, dass +Sie die Urheberrechte der Dokumente respektieren und nur Inhalte verwenden, für +die Sie die entsprechenden Rechte besitzen oder die unter einer freien Lizenz +stehen. ## Credits - AhmedOsamaMath/issuu-downloader +``` +``` diff --git a/devenv.nix b/devenv.nix index 329baaf..151e0f3 100644 --- a/devenv.nix +++ b/devenv.nix @@ -11,6 +11,7 @@ pkgs.python313Packages.ebooklib pkgs.python313Packages.requests pkgs.python313Packages.pip + pkgs.python313Packages.pillow pkgs.python313Packages.types-requests ]; diff --git a/main.py b/issuu2epub.py similarity index 59% rename from main.py rename to issuu2epub.py index 0c5dae4..a5ac0cc 100644 --- a/main.py +++ b/issuu2epub.py @@ -1,9 +1,14 @@ +import io +import os import re import secrets import tempfile +from fileinput import filename from pathlib import Path +import click from ebooklib import epub # type: ignore +from PIL import Image from requests import HTTPError, auth, request @@ -68,6 +73,24 @@ def download_pages(page_urls: list[str], working_dir: Path) -> list[Path]: return page_paths +def convert_image(image_path: Path) -> io.BytesIO: + """convert image and return bytes array.""" + max_image_size = (1000, 1400) + target_quality = 50 + + with Image.open(image_path.as_posix()) as img: + + if img.mode in ("RGBA", "P"): + img = img.convert("RGB") + + img.thumbnail(max_image_size, Image.Resampling.LANCZOS) + img_byte_arr = io.BytesIO() + img.save(img_byte_arr, format="JPEG", optimize=True, quality=target_quality) + img_byte_arr.seek(0) + + return img_byte_arr + + def generate_epub( pages: list[Path], output_file: Path, title: str, author: str ) -> None: @@ -80,12 +103,51 @@ def generate_epub( chapters = [] - for i, page in enumerate(pages, start=1): + # Use first image as Cover + title_page = epub.EpubHtml(title=title, file_name="title_page.xhtml", lang="de") + + cover_image = epub.EpubImage() + cover_image.file_name = f"images/cover.jpg" + cover_image.media_type = "image/jpeg" + cover_image.content = convert_image(pages[0]) + + book.add_item(cover_image) + + title_page.content = f""" + + + {title} + + + + Cover + + + """ + + book.add_item(title_page) + chapters.append(title_page) + + # Add Pages. + for i, page in enumerate(pages[1:], start=1): page_title = f"Page {i}" image_item = epub.EpubImage() - image_item.file_name = page.as_posix() - image_item.media_type = "image/png" + image_item.file_name = f"images/page_{i:03d}.jpg" + image_item.media_type = "image/jpeg" + image_item.content = convert_image(page) + book.add_item(image_item) chapter = epub.EpubHtml( @@ -118,23 +180,26 @@ def generate_epub( 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}") + print(f"✅ Kindle-optimiertes EPUB erstellt: {output_file}") -def main() -> None: +@click.command() +@click.option("--url", prompt="Issuu URL", help="Issuu URL to convert to EPUB") +@click.option("--title", prompt="Document Title", help="Document Title") +@click.option("--author", prompt="Document Author", help="Document Author") +@click.option( + "--output", + prompt="EPUB Output Filename", + help="EPUB Output File", +) +def main(url: str, title: str, author: str, output: str) -> None: """main function.""" cwd = create_working_dir() - username, document_id = parse_issuu_url( - "https://issuu.com/bscyb1898/docs/yb_mag_nr._1_saison_2025_26" - ) + username, document_id = parse_issuu_url(url=url) urls = get_page_urls(username, document_id) @@ -142,9 +207,9 @@ def main() -> None: generate_epub( pages=pages, - output_file=Path("/home/michael/Downloads/YBMag-2025-01.epub"), - title="YB Mag 2025 01", - author="BSC YB", + output_file=Path(output), + title=title, + author=author, )