Michel Roux fff3b3e886
All checks were successful
books / lint (push) Successful in 1m21s
fix: ⚰️ remove non working cookies
2025-02-11 23:37:03 +01:00

80 lines
2.5 KiB
Python

from argparse import ArgumentParser
from json import load
from os import path
from pathlib import Path
from random import randint
from tempfile import NamedTemporaryFile
from time import sleep
from typing import cast
from bs4 import BeautifulSoup
from curl_cffi import requests
from pypub import Chapter, Epub, SimpleChapterFactory # type: ignore
from pyxml.html import HtmlElement, fromstring # type: ignore
parser = ArgumentParser()
parser.add_argument("-l", "--lang", choices=["fr", "en"], required=True)
parser.add_argument("-v", "--volume", type=int, choices=range(1, 10), required=True)
parser.add_argument("output", type=Path)
args = parser.parse_args()
class MyChapterFactory(SimpleChapterFactory): # type: ignore
def cleanup_html(self, content: bytes) -> HtmlElement:
"""
cleanup html content to only include supported tags
"""
# check if we can minimalize the scope
etree = fromstring(content)
# fix and remove invalid images
for img in etree.xpath(".//img"):
# ensure all images with no src are removed
if "src" not in img.attrib:
cast(HtmlElement, img.getparent()).remove(img)
# ensure they also have an alt
elif "alt" not in img.attrib:
img.attrib["alt"] = img.attrib["src"]
# return new element-tree
return etree
with open(f"{path.dirname(__file__)}/{args.lang}.json") as f:
manifest = load(f)
book = manifest[args.volume - 1]
epub = Epub(
title=book["title"],
creator=book["creator"],
language=args.lang,
publisher="Nanamazon+",
factory=MyChapterFactory(),
)
for url in book["chapters"]:
markup = requests.get(
url,
impersonate=requests.BrowserType.firefox133.value,
thread=None,
curl_options=None,
debug=False,
)
etree = BeautifulSoup(markup.text, "html.parser")
title = etree.select_one("h1.entry-title").text # type: ignore
content = etree.select("div.entry-content p")
print(f"Chapter {title}...")
chapter = Chapter(title, "")
for elem in content:
if elem.a:
continue
chapter.content += elem.prettify()
epub.add_chapter(chapter)
timeout = randint(10, 90)
print(f"Wait {timeout} seconds...")
sleep(timeout)
with NamedTemporaryFile() as cover:
response = requests.get(book["cover"], thread=None, curl_options=None, debug=False)
cover.write(response.content)
epub.cover = cover.name
epub.create(args.output)