Update code

This commit is contained in:
2024-03-08 15:28:14 +08:00
parent a17e8705de
commit f61a2af57d
4 changed files with 107 additions and 5 deletions

View File

@@ -18,6 +18,8 @@ class Config:
else:
self._data['db'] = 'cwm.db'
self._data['save_to_config'] = True
self._data['type'] = 'epub,txt'
self._data['export_book_template'] = 'exported/<book_name> - <author_name>.<ext>' # noqa: E501
self._data['export_chapter_template'] = 'exported/<book_id>/<chapter_id>.txt' # noqa: E501
self.save()
@@ -35,6 +37,10 @@ class Config:
else:
return default
@cached_property
def book_id(self):
return getattr(self._args, 'bid', None)
@cached_property
def booksnew(self):
return self.get_arg('booksnew', None)
@@ -51,10 +57,33 @@ class Config:
def db(self):
return self.get_arg('db', 'cwm.db')
@cached_property
def export_book_template(self):
return self.get_arg('export_book_template', 'exported/<book_name> - <author_name>.<ext>') # noqa: E501
@cached_property
def export_chapter_template(self):
return self.get_arg('export_chapter_template', 'exported/<book_id>/<chapter_id>.txt') # noqa: E501
@cached_property
def export_epub(self):
return self.export_type.find('epub') >= 0
@cached_property
def export_txt(self):
return self.export_type.find('txt') >= 0
@cached_property
def export_type(self):
return self.get_arg('type', 'epub,txt')
def get_export_book(self, book, ext):
temp = self.export_book_template
for k in book.keys():
temp = temp.replace(f'<{k}>', str(book[k]))
temp = temp.replace('<ext>', ext)
return temp
def get_export_chapter(self, chapter):
temp = self.export_chapter_template
for k in chapter.keys():

View File

@@ -46,3 +46,48 @@ def export_chapter(ncw: NovelCiwei, db: CwmDb, cfg: Config, bn: BooksNew,
with open(filename, 'w', encoding='UTF-8') as f:
f.write(chapter['chapter_title'] + '\n')
f.write(content)
def export_book(ncw: NovelCiwei, db: CwmDb, cfg: Config, bn: BooksNew,
book_id: int):
book = ncw.get_book_in_shelf(book_id)
if cfg.export_txt:
txt_filename = cfg.get_export_book(book, 'txt')
d = dirname(txt_filename)
makedirs(d, exist_ok=True)
txt = open(txt_filename, 'w', encoding='UTF-8')
try:
chapters = ncw.get_chapter_with_bookid(book_id)
divisions = ncw.get_divisions_with_bookid(book_id)
maps = {}
count = 0
for chapter in chapters:
division_id = chapter['division_id']
if division_id in maps:
maps[division_id].append(chapter)
else:
maps[division_id] = [chapter]
for division in divisions:
if cfg.export_txt:
txt.write(f"{division['division_index']}{division['division_name']}\n") # noqa: E501
if division['description']:
txt.write(division['description'] + '\n\n')
chapter_index = 1
for chapter in maps[division['division_id']]:
if chapter['is_download']:
chapter_id = chapter['chapter_id']
chapter_title = chapter['chapter_title']
raw_content = bn.get_chapter(book_id, chapter_id)
content = try_decrypt(db, cfg, raw_content, chapter_id)
if cfg.export_txt:
txt.write(f"{chapter_index}{chapter_title}\n")
txt.write(content + '\n\n')
count += 1
else:
if cfg.export_txt:
txt.write(f"{chapter_index}{chapter_title} (未下载)\n\n") # noqa: E501
chapter_index += 1
print(f'Exported {count} chapters.')
finally:
if cfg.export_txt:
txt.close()

24
main.py
View File

@@ -14,7 +14,10 @@ parser.add_argument('-b', '--booksnew', help='The path to booksnew directory or
parser.add_argument('-C', '--cid', '--chapter-id', help='The chapter id.', type=int) # noqa: E501
parser.add_argument('--ect', '--export-chapter-template', help='The template of the exported chapter. Available key: <book_id>, <chapter_id> eta.') # noqa: E501
parser.add_argument('-r', '--real', help='Use default locations. Needed running on Android machine. Root is required.', action='store_true') # noqa: E501
parser.add_argument('action', help='The action to do.', choices=['importkey', 'exportchapter']) # noqa: E501
parser.add_argument('-B', '--bid', '--book-id', help='The book id.', type=int)
parser.add_argument('-t', '--type', help='Export type. Available types: epub, txt. Default: epub,txt') # noqa: E501
parser.add_argument('--ebt', '--export-book-template', help='The template of the exported book. Available key: <ext>, <book_id>, <book_name>, <author_name> eta.') # noqa: E501
parser.add_argument('action', help='The action to do.', choices=['importkey', 'exportchapter', 'exportbook']) # noqa: E501
def main(args=None):
@@ -22,9 +25,9 @@ def main(args=None):
cfg = Config(arg.config)
if arg.real:
base_dir = '/data/data/com.kuangxiangciweimao.novel/'
arg.cwmdb = f'{base_dir}/databases/novelCiwei'
arg.key = f'{base_dir}/files/Y2hlcy8'
arg.booksnew = f'{base_dir}/files/novelCiwei/reader/booksnew'
arg.cwmdb = f'{base_dir}databases/novelCiwei'
arg.key = f'{base_dir}files/Y2hlcy8'
arg.booksnew = f'{base_dir}files/novelCiwei/reader/booksnew'
cfg.add_args(arg)
try:
db = CwmDb(cfg.db)
@@ -44,6 +47,19 @@ def main(args=None):
raise ValueError('The chapter id is not specified.')
from export import export_chapter
export_chapter(ncw, db, cfg, bn, cfg.chapter_id)
elif arg.action == 'exportbook':
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 cfg.book_id is None:
raise ValueError('The book id is not specified.')
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_book
export_book(ncw, db, cfg, bn, cfg.book_id)
finally:
cfg.save()

View File

@@ -11,7 +11,19 @@ class NovelCiwei:
'SELECT book_info FROM shelf_book_info WHERE book_id = ?;',
[str(book_id)])
for i in cur:
return json.loads(i)
return json.loads(i[0])
def get_chapter_with_bookid(self, book_id: int):
cur = self._db.execute(
'SELECT * FROM catalog1 WHERE book_id = ? ORDER BY chapter_index;',
[str(book_id)])
cur.row_factory = sqlite3.Row
return cur.fetchall()
def get_divisions_with_bookid(self, book_id: int):
cur = self._db.execute('SELECT * FROM division WHERE book_id = ? ORDER BY division_index;', [str(book_id)]) # noqa: E501
cur.row_factory = sqlite3.Row
return cur.fetchall()
def get_chapter(self, chapter_id: int):
cur = self._db.execute(