2022-01-18 21:05:00 +00:00
|
|
|
import logging
|
2022-01-18 20:43:45 +00:00
|
|
|
|
2022-09-18 23:00:12 +00:00
|
|
|
import bs4
|
2022-09-18 23:07:52 +00:00
|
|
|
from ebooklib import epub # type: ignore
|
2022-09-18 23:00:12 +00:00
|
|
|
import requests
|
|
|
|
import utils
|
2022-01-18 20:43:45 +00:00
|
|
|
|
2022-08-18 10:59:02 +00:00
|
|
|
|
|
|
|
@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)
|
2022-01-18 20:43:45 +00:00
|
|
|
|
|
|
|
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")
|
2022-01-19 15:04:48 +00:00
|
|
|
content_as = entry_content.find_all("a")
|
2022-01-18 20:43:45 +00:00
|
|
|
|
2022-01-19 15:04:48 +00:00
|
|
|
for content_a in content_as:
|
|
|
|
content_a.decompose()
|
2022-01-18 20:43:45 +00:00
|
|
|
|
|
|
|
chapter_title = "Introduction"
|
|
|
|
chapter_content = ""
|
2022-08-18 11:43:30 +00:00
|
|
|
chapter_index = 1
|
2022-01-18 20:43:45 +00:00
|
|
|
|
|
|
|
for paragraph in entry_content.children:
|
|
|
|
if paragraph.name == "h1":
|
2022-01-18 21:05:00 +00:00
|
|
|
logging.log(logging.INFO, f"Gravesong - {chapter_title}")
|
2022-08-18 10:59:02 +00:00
|
|
|
book_items.append(
|
|
|
|
epub.EpubHtml(
|
|
|
|
title=chapter_title,
|
|
|
|
content=utils.generate_title_html(chapter_title, chapter_content),
|
2022-08-18 11:43:30 +00:00
|
|
|
file_name=f"{chapter_index}.xhtml",
|
2022-08-18 10:59:02 +00:00
|
|
|
lang=metadatas["language"],
|
|
|
|
)
|
2022-01-18 20:43:45 +00:00
|
|
|
)
|
2022-01-18 21:53:50 +00:00
|
|
|
chapter_title = paragraph.get_text().strip()
|
2022-08-18 11:43:30 +00:00
|
|
|
chapter_index += 1
|
2022-01-18 20:43:45 +00:00
|
|
|
elif paragraph.name == "p":
|
|
|
|
chapter_content += paragraph.prettify()
|
|
|
|
|
2022-01-18 21:05:00 +00:00
|
|
|
logging.log(logging.INFO, f"Gravesong - {chapter_title}")
|
2022-08-18 10:59:02 +00:00
|
|
|
book_items.append(
|
|
|
|
epub.EpubHtml(
|
|
|
|
title=chapter_title,
|
|
|
|
content=utils.generate_title_html(chapter_title, chapter_content),
|
2022-08-18 11:43:30 +00:00
|
|
|
file_name=f"{chapter_index}.xhtml",
|
2022-08-18 10:59:02 +00:00
|
|
|
lang=metadatas["language"],
|
|
|
|
)
|
|
|
|
)
|
2022-01-18 20:43:45 +00:00
|
|
|
|
2022-01-18 21:05:00 +00:00
|
|
|
logging.log(logging.INFO, "Gravesong - Book")
|
2022-08-18 10:59:02 +00:00
|
|
|
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)
|