def generate_epub_file(chapters, epub_name="epub_name", output_directory="OUTPUT_DIRECTORY"): if not chapters: return epub = html2epub.Epub(epub_name) def set_chapter(type, chapter): if type in 'content': epub.add_chapter( html2epub.create_chapter_from_string(chapter, title=chapter)) elif type in 'url': epub.add_chapter(html2epub.create_chapter_from_url(chapter)) elif type in 'file': epub.add_chapter(html2epub.create_chapter_from_file(chapter)) for type_chapter in chapters: cur_chapter = chapters[type_chapter] if not cur_chapter: continue if isinstance(cur_chapter, str): set_chapter(type_chapter, cur_chapter) elif isinstance(chapters[type_chapter], list): for chapter2 in cur_chapter: if not chapter2: continue set_chapter(type_chapter, chapter2) epub.create_epub(output_directory, epub_name)
def saveEpub(infoList, bookName): epub = html2epub.Epub(bookName) for eachHtml, title in getbook(infoList): chapter = html2epub.create_chapter_from_string(eachHtml, title=title) epub.add_chapter(chapter) epub.create_epub(DIR) print("下载成功, 已保存在: ", DIR + bookName + ".epub")
def saveEpub(url): bookName, htmls = getbook(url) epub = html2epub.Epub(bookName) i = 0 print("开始整合epub...") for eachHtml in htmls: i += 1 chapter = html2epub.create_chapter_from_string(eachHtml, title="章节" + str(i)) epub.add_chapter(chapter) print("已整合{:.2f}%".format( (htmls.index(eachHtml) + 1) / len(htmls) * 100)) epub.create_epub(DIR) print("整合完毕.")
os.chdir(title) # Init Hive hive = [args.url] downloaded = set() while hive: current_url = hive.pop() if (current_url in downloaded): print("-%s " % current_url) else: print("~[%d] %s" % (len(hive), current_url)) downloaded.add(current_url) download(current_url) if config['waitPackage'] == 'yes': input('>Press Enter when ready...') print(">Download completed, now packaging epub...") epub = html2epub.Epub(title, language="zh-cn", creator="cool18", publisher="cool18") for file in os.listdir("."): chap = html2epub.create_chapter_from_file(file) epub.add_chapter(chap) epubpath = epub.create_epub(pydir) print(">OK, epub generated at: %s" % epubpath) if config['autoDelete'] == 'yes': os.chdir("..") print(">Deleting Directory: %s" % title) shutil.rmtree(title)