Some styles and fixes.

This commit is contained in:
2025-12-23 09:30:23 +01:00
parent 644419a0e8
commit 636abadc1f
4 changed files with 129 additions and 11 deletions

67
main.py
View File

@ -11,9 +11,14 @@ import re
from requests_tor import RequestsTor
import requests
import hashlib
from PIL import Image
from io import BytesIO
IMAGE_REMOVE_REGEX = re.compile(r"""<img\s+.*src\s*=\s*\"(?P<src>https?.*)\".*/>""")
KINDLE_MAX_HEIGHT = 1024
KINDLE_MAX_WIDTH = 768
class Author(BaseModel):
id: int
@ -49,11 +54,23 @@ def process_text(text_obj: dict):
content = f"<i>{content}</i>"
elif mark["type"] == "bold":
content = f"<b>{content}</b>"
elif mark["type"] == "underline":
content = f"<u>{content}</u>"
else:
raise ValueError(f"unknown mark for text {text_obj}")
return content
def fit_image(data: bytes) -> bytes:
buff = BytesIO(data)
img = Image.open(buff)
out_buff = BytesIO()
img = img.convert("RGB")
img.thumbnail((KINDLE_MAX_WIDTH, KINDLE_MAX_HEIGHT))
img.save(out_buff, format="JPEG")
return out_buff.getvalue()
class Chapter(BaseModel):
id: int
volume: int
@ -148,12 +165,14 @@ class Chapter(BaseModel):
image_name = image["image"]
image_path, image_bytes = self.get_image(image_name, zip)
image_item = epub.EpubImage(
uid=image_name,
file_name=image_path,
content=image_bytes,
media_type="image/jpeg",
content=fit_image(image_bytes),
)
extras.append(image_item)
output.append(f'<img src="../{image_path}" />')
output.append(
f'<div class="img-wrap"><img src="../{image_path}" /></div>'
)
elif item["type"] == "horizontalRule":
output.append("<hr/>")
else:
@ -224,12 +243,13 @@ class Chapter(BaseModel):
if final_path is None:
img = epub.EpubImage(
file_name=strip_path,
content=resp,
media_type="image/jpeg",
content=fit_image(resp),
)
replaces.append(img)
final_path = strip_path
newiimage = f'<img loading="lazy" src="../{final_path}" />'
newiimage = f'<div class="img-wrap"><img src="../{final_path}" /></div>'
print(target_str, newiimage)
new_content = new_content.replace(target_str, newiimage) # type: ignore
@ -243,7 +263,7 @@ class Chapter(BaseModel):
fetch_images: bool,
client: requests.Session | RequestsTor,
cache: Path,
):
) -> epub.EpubHtml:
(item, extras) = self.load(base_dir)
for extra in extras:
book.add_item(extra)
@ -255,8 +275,8 @@ class Chapter(BaseModel):
book.add_item(image)
book.add_item(item)
book.spine.append(item)
book.toc.append(item)
return item
def parse_args():
@ -356,12 +376,35 @@ def main():
lang="ru",
)
book.add_item(translators_page)
book.toc.append(translators_page)
book.spine.append(translators_page) # type: ignore
css = epub.EpubItem(
uid="doc_style",
file_name="styles/default.css",
media_type="text/css",
content=Path("./style.css").read_text(),
)
css_ref = epub.EpubItem(
uid="doc_style",
file_name="../styles/default.css",
media_type="text/css",
content="",
)
book.add_item(css)
translators = set()
for chapter in chapters:
chapter.add_to_book(book, args.input, args.fetch_images, client, args.cache)
item = chapter.add_to_book(
book,
args.input,
args.fetch_images,
client,
args.cache,
)
item.add_item(css_ref)
book.spine.append(item) # type: ignore
book.toc.append(item)
translators.update(chapter.get_translators(args.input))
translators_content = '<h2 align="center">Переводчики:</h2>\n<ul>\n'
@ -372,7 +415,9 @@ def main():
book.add_item(epub.EpubNcx())
output_path: Path = args.output / f"{info.rusName}-{args.volume or 'full'}.epub"
suffix = f"-{args.volume}" if args.volume else ""
output_path: Path = args.output / f"{info.rusName}{suffix}.epub"
output_path.parent.mkdir(parents=True, exist_ok=True)
epub.write_epub(str(output_path), book)