NovelDownloader/ebook.py
Juraj Oravec d8a8122bdc
Rewrite to a more modular style
- Only one module is available for now: boxnovel.com
- There a re still some problems but it is getting better

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
2020-12-21 03:03:32 +01:00

127 lines
3.9 KiB
Python

import os
from typing import List
from ebooklib import epub
from chapter import Chapter
class Ebook:
def __init__(self, title : str, chapters : List[Chapter], author : str = "",
coverArt : str = ""):
self.title = title
self.chapters = chapters
self.author = author
self.coverArt = coverArt
self.ebookChapters = list()
def create(self):
self.ebook = epub.EpubBook()
self.ebook.set_identifier("ebook-%s" % self.title)
self.ebook.set_title(self.title)
self.ebook.set_language('en')
if self.author:
self.ebook.add_author(self.author)
self.docStyle = epub.EpubItem(
uid="doc_style",
file_name="style/main.css",
media_type="text/css",
content=open("template/style.css").read()
)
self.ebook.add_item(self.docStyle)
self.cover()
self.addChapters()
filename = "{basename}.epub".format(basename=self.title)
print("Saving to '%s'" % filename)
if os.path.exists(filename):
os.remove(filename)
epub.write_epub(filename, self.ebook, {})
def cover(self):
if not self.ebook:
return
content = "<h1>{title}</h1>".format(title=self.title)
if self.author:
content += "Written by {author}".format(author=self.author)
self.intro_ch = epub.EpubHtml(title="Cover", file_name='cover.xhtml')
self.intro_ch.add_item(self.docStyle)
self.intro_ch.content = """<html>
<head>
<title>Cover</title>
<link rel="stylesheet" href="style/main.css" type="text/css" />
</head>
<body>
{content}
</body>
</html>
""".format(content=content)
self.ebook.add_item(self.intro_ch)
if self.coverArt:
self.book_artwork = epub.EpubItem(
uid="book_artwork",
file_name="media/artwork.jpg",
media_type="image/jpeg",
content=open(self.coverArt).read()
)
self.ebook.add_item(self.book_artwork)
art_ch = epub.EpubHtml(title="Artwork", file_name='artwork.xhtml')
art_ch.add_item(self.docStyle)
art_ch.add_item(self.book_artwork)
art_ch.content = """<html>
<head>
<title>Artwork</title>
<link rel="stylesheet" href="style/main.css" type="text/css" />
</head>
<body>
<img class="intro_artwork" src="media/artwork.jpg" alt="Artwork" />
</body>
</html>
"""
self.ebook.add_item(art_ch)
def addChapters(self):
if not self.ebook:
return
if not self.chapters:
return
for chapterData in self.chapters:
chapter = epub.EpubHtml(
title=chapterData.title,
file_name='Chapter-{number}.xhtml'.format(number=chapterData.number)
)
chapter.add_item(self.docStyle)
chapter.content = chapterData.getHtml()
self.ebook.add_item(chapter)
self.ebookChapters.append(chapter)
self.tableOfContent()
def tableOfContent(self):
toc = list()
if self.coverArt:
toc.append(epub.Link('artwork.xhtml', 'Artvork', 'artwork'))
if self.chapters:
toc.append(epub.Link('cover.xhtml', 'Cover', 'cover'))
toc.append((epub.Section('Chapters'), self.ebookChapters))
# Set the TOC
self.ebook.toc = tuple(toc)
# add navigation files
self.ebook.add_item(epub.EpubNcx())
self.ebook.add_item(epub.EpubNav())
# Create spine
nav_page = epub.EpubNav(uid='book_toc', file_name='toc.xhtml')
nav_page.add_item(self.docStyle)
self.ebook.add_item(nav_page)
self.ebook.spine = [self.intro_ch, nav_page] + self.ebookChapters