From 70d8d12e647dae1e490f50d5be32a330df31c92d Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sun, 10 Mar 2024 08:29:42 +0800 Subject: [PATCH] Add export all books --- export.py | 25 +++++++++++++++++++++++-- main.py | 13 ++++++++++++- novelCiwei.py | 4 ++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/export.py b/export.py index b2f2eaa..f0ba743 100644 --- a/export.py +++ b/export.py @@ -121,9 +121,23 @@ def export_book(ncw: NovelCiwei, db: CwmDb, cfg: Config, bn: BooksNew, epub.save_epub_file() +def export_all(ncw: NovelCiwei, db: CwmDb, cfg: Config, bn: BooksNew): + books = ncw.get_all_books() + for book in books: + book_id = int(book[0]) + try: + export_book(ncw, db, cfg, bn, book_id) + except Exception as e: + print(f'Failed to export book {book_id}: {e}') + + def export(ncw: NovelCiwei, db: CwmDb, cfg: Config, bn: BooksNew): action = ask_choice(cfg, [], '请选择要导出的类型:', extra=[ - ('b', '整本书', 'book'), ('c', '单章', 'chapter')]) + ('b', '整本书', 'book'), ('c', '单章', 'chapter'), + ('a', '所有书', 'all')]) + if action == 'all': + export_all(ncw, db, cfg, bn) + return books = ncw.get_books() shelfs = {} for book in books: @@ -139,9 +153,16 @@ def export(ncw: NovelCiwei, db: CwmDb, cfg: Config, bn: BooksNew): data += f"\n书架内有 {book['book_name']} - {book['author_name']}" return data shelf = ask_choice(cfg, [i for i in shelfs.keys()], '请选择书架:', show_shelf, - [('r', '阅读历史', 'readhistory')]) + [('r', '阅读历史', 'readhistory'), ('a', '所有书', 'all')]) if shelf == 'readhistory': books = [json.loads(b['book_info']) for b in ncw.get_read_history()] + elif shelf == 'all': + bookids = [int(b[0]) for b in ncw.get_all_books()] + books = [] + for bid in bookids: + book = ncw.get_book_in_shelf(bid) or ncw.get_book_in_readhistory(bid) # noqa: E501 + if book: + books.append(book) else: books = [json.loads(b['book_info']) for b in shelfs[shelf]] book = ask_choice(cfg, books, '请选择书:', lambda b: f"{b['book_name']} - {b['author_name']}") # noqa: E501 diff --git a/main.py b/main.py index 3c1e5b7..8e67192 100644 --- a/main.py +++ b/main.py @@ -21,7 +21,7 @@ parser.add_argument('--ebt', '--export-book-template', help='The template of the 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 +parser.add_argument('action', help='The action to do.', choices=['importkey', 'exportchapter', 'exportbook', 'export', 'exportall', 'ik', 'ec', 'eb', 'e', 'ea'], nargs='?', default='export') # noqa: E501 def main(args=None): @@ -75,6 +75,17 @@ def main(args=None): raise ValueError('At least one export type should be specified.') # noqa: E501 from export import export export(ncw, db, cfg, bn) + elif arg.action == "exportall" or arg.action == "ea": + if cfg.cwmdb is None: + raise ValueError('The cwmdb is not specified.') + ncw = NovelCiwei(cfg.cwmdb) + if cfg.booksnew is None: + raise ValueError('The booksnew is not specified.') + bn = BooksNew(cfg.booksnew) + if not cfg.export_epub and not cfg.export_txt: + raise ValueError('At least one export type should be specified.') # noqa: E501 + from export import export_all + export_all(ncw, db, cfg, bn) finally: cfg.save() diff --git a/novelCiwei.py b/novelCiwei.py index fd23b7e..60629d7 100644 --- a/novelCiwei.py +++ b/novelCiwei.py @@ -20,6 +20,10 @@ class NovelCiwei: for i in cur: return json.loads(i[0]) + def get_all_books(self): + cur = self._db.execute('SELECT book_id, COUNT(*) FROM catalog1 WHERE is_download = 1 GROUP BY book_id;') # noqa: E501 + return cur.fetchall() + def get_books(self): cur = self._db.execute('SELECT * FROM shelf_book_info;') cur.row_factory = sqlite3.Row