From c0568543a50b4fd2722c92c5130bad9c035a79ba Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 8 Mar 2024 22:38:18 +0800 Subject: [PATCH] Add new settings --- config.py | 6 ++++++ epub.py | 22 ++++++++++++++++++++++ export.py | 4 +++- main.py | 2 ++ utils.py | 10 ++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/config.py b/config.py index 58624b9..90eda65 100644 --- a/config.py +++ b/config.py @@ -21,6 +21,8 @@ class Config: self._data['type'] = 'epub,txt' self._data['export_book_template'] = 'exported/ - .' # noqa: E501 self._data['export_chapter_template'] = 'exported//.txt' # noqa: E501 + self._data['export_nodownload'] = True + self._data['img_cache_dir'] = 'img_cache' self._data['page_size'] = 10 self.save() @@ -72,6 +74,10 @@ class Config: def export_epub(self): return self.export_type.find('epub') >= 0 + @cached_property + def export_nodownload(self): + return self.get_arg('export_nodownload', True) + @cached_property def export_txt(self): return self.export_type.find('txt') >= 0 diff --git a/epub.py b/epub.py index 8c7453a..274ba26 100644 --- a/epub.py +++ b/epub.py @@ -360,6 +360,28 @@ class EpubFile: self.EpubList.append(ch) self.epub.spine.append(ch) + def add_nodownload_chapter(self, chapter, division_name: str): + chapter_title = chapter['chapter_title'] + chapter_id = chapter['chapter_id'] + ch = epub.EpubHtml( + title=f"{chapter_title} (未下载)", + file_name=f'ch{chapter_id}.xhtml', + lang='zh-CN', + uid=f'ch{chapter_id}', + ) + if division_name == '作品相关': + ch.is_linear = False + ch.content = f'

{chapter_title}

\n

本章未下载

' # noqa: E501 + self.epub.add_item(ch) + if self.last_division_name != division_name: + self.EpubList.append([epub.Link(ch.file_name, division_name), []]) + self.last_division_name = division_name + if isinstance(self.EpubList[-1], list): + self.EpubList[-1][-1].append(ch) + else: + self.EpubList.append(ch) + self.epub.spine.append(ch) + def save_epub_file(self): # save epub file to local # the path to save epub file to local self.epub.toc = self.EpubList diff --git a/export.py b/export.py index 5c9e154..c3476ef 100644 --- a/export.py +++ b/export.py @@ -100,9 +100,11 @@ def export_book(ncw: NovelCiwei, db: CwmDb, cfg: Config, bn: BooksNew, if cfg.export_epub: epub.add_chapter(chapter, content, division_name) count += 1 - else: + elif cfg.export_nodownload: if cfg.export_txt: txt.write(f"第{chapter_index}章 {chapter_title} (未下载)\n\n") # noqa: E501 + if cfg.export_epub: + epub.add_nodownload_chapter(chapter, division_name) chapter_index += 1 print(f'Exported {count} chapters.') finally: diff --git a/main.py b/main.py index 68ba141..3c1e5b7 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from config import Config from db import CwmDb from novelCiwei import NovelCiwei from booksnew import BooksNew +from utils import parse_bool parser = ArgumentParser(description='A tool to export CiWeiMao novel cache.') @@ -19,6 +20,7 @@ parser.add_argument('-t', '--type', help='Export type. Available types: epub, tx parser.add_argument('--ebt', '--export-book-template', help='The template of the exported book. Available key: , , , eta.') # noqa: E501 parser.add_argument('--icd', '--image-cache-dir', help='Path to image cache directory.') # noqa: E501 parser.add_argument('-s', '--page-size', help='Maximum size of a page when asking for choices.', type=int) # noqa: E501 +parser.add_argument('-a', '--export-nodownload', help='export not downloaded chapter when exporting book.', type=parse_bool) # noqa: E501 parser.add_argument('action', help='The action to do.', choices=['importkey', 'exportchapter', 'exportbook', 'export', 'ik', 'ec', 'eb', 'e'], nargs='?', default='export') # noqa: E501 diff --git a/utils.py b/utils.py index 5c9a3d0..9e092d4 100644 --- a/utils.py +++ b/utils.py @@ -53,3 +53,13 @@ def ask_choice(cfg: Config, choices: list, prompt='请选择:', fn=None, extra if index < 0 or index >= count: continue return choices[index] + + +def parse_bool(s: str): + if s.lower() in ['y', 'yes', 'true', 't']: + return True + if s.lower() in ['n', 'no', 'false', 'f']: + return False + if s.isnumeric(): + return bool(int(s)) + raise ValueError(f"Unexpected bool value: {s}")