import logging import bs4 from ebooklib import epub import requests import utils @utils.executor def process(metadatas, output_dir): book = epub.EpubBook() book_items = [] book.set_identifier("gravesong") book.set_title("Gravesong") book.set_language(metadatas["language"]) book.set_cover("cover.jpg", open(metadatas["cover"], "rb").read()) book.add_metadata("DC", "description", "Advance Patreon Edition") for author in metadatas["authors"]: book.add_author(author) response = requests.post( "https://wanderinginn.com/wp-pass.php", data={"post_password": "Iwalkedameadowweary", "Submit": "Enter"}, headers={"Referer": "https://wanderinginn.com/2022/01/11/gravesong/"}, ) html = bs4.BeautifulSoup(response.content, "html.parser") entry_content = html.select_one("div.entry-content") content_as = entry_content.find_all("a") for content_a in content_as: content_a.decompose() chapter_title = "Introduction" chapter_content = "" chapter_index = 1 for paragraph in entry_content.children: if paragraph.name == "h1": logging.log(logging.INFO, f"Gravesong - {chapter_title}") book_items.append( epub.EpubHtml( title=chapter_title, content=utils.generate_title_html(chapter_title, chapter_content), file_name=f"{chapter_index}.xhtml", lang=metadatas["language"], ) ) chapter_title = paragraph.get_text().strip() chapter_index += 1 elif paragraph.name == "p": chapter_content += paragraph.prettify() logging.log(logging.INFO, f"Gravesong - {chapter_title}") book_items.append( epub.EpubHtml( title=chapter_title, content=utils.generate_title_html(chapter_title, chapter_content), file_name=f"{chapter_index}.xhtml", lang=metadatas["language"], ) ) logging.log(logging.INFO, "Gravesong - Book") for book_item in book_items: book.add_item(book_item) book.add_item(epub.EpubNcx()) book.add_item(epub.EpubNav()) book.toc = book_items book.spine = ["cover", "nav", *book_items] epub.write_epub(f"{output_dir}/Gravesong.epub", book)