Add export all books

This commit is contained in:
2024-03-10 08:29:42 +08:00
parent 3888030eec
commit 70d8d12e64
3 changed files with 39 additions and 3 deletions

View File

@@ -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

13
main.py
View File

@@ -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()

View File

@@ -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