优化效率
This commit is contained in:
@@ -33,8 +33,9 @@ def get_toc_tree(toc_path):
|
||||
nav_map = soup.find('navMap')
|
||||
return parse_navpoints(nav_map.find_all('navPoint', recursive=False))
|
||||
|
||||
def build_booksnote(annotation_db='data/AEAnnotation.sqlite', books_plist='data/Books.plist'):
|
||||
annotations = get_annotations(annotation_db)
|
||||
def build_booksnote(annotation_db='data/AEAnnotation.sqlite', books_plist='data/Books.plist', bookid=None):
|
||||
# 支持只处理特定 assetid 的笔记
|
||||
annotations = get_annotations(annotation_db, bookid=bookid)
|
||||
booksinfo = parse_books_plist(books_plist)
|
||||
booksnote = defaultdict(lambda: defaultdict(dict))
|
||||
for assetid, notes in annotations.items():
|
||||
@@ -132,29 +133,31 @@ if __name__ == '__main__':
|
||||
|
||||
from booklist_parse import parse_books_plist
|
||||
from InquirerPy import inquirer
|
||||
booksnote = build_booksnote()
|
||||
|
||||
# 先获取所有书籍元数据
|
||||
booksinfo = parse_books_plist('data/Books.plist')
|
||||
# 构建书名列表(优先displayname, 其次itemname, 否则assetid),按最新笔记时间排序
|
||||
|
||||
# 构建书名列表(优先displayname, 其次itemname, 否则assetid),按parse_books_plist中的date字段排序
|
||||
assetid2name = {}
|
||||
assetid2latest = {}
|
||||
for assetid in booksnote:
|
||||
info = booksinfo.get(assetid, {})
|
||||
assetid2lastopen = {}
|
||||
from booklist_parse import get_books_last_open
|
||||
|
||||
# 获取所有书籍的最后打开时间(字典,值为{'last_open': 时间戳})
|
||||
last_open_times = get_books_last_open('data/BKLibrary.sqlite')
|
||||
|
||||
for assetid, info in booksinfo.items():
|
||||
name = info.get('displayname') or info.get('itemname') or assetid
|
||||
# 如果书名中包含“-”,只取“-”前面的部分
|
||||
if '-' in name: name = name.split('-', 1)[0].strip()
|
||||
if '-' in name:
|
||||
name = name.split('-', 1)[0].strip()
|
||||
assetid2name[assetid] = name
|
||||
# 获取该书所有笔记的最新creationdate
|
||||
latest = None
|
||||
for chapter in booksnote[assetid].values():
|
||||
for ann in chapter.values():
|
||||
dt = ann.get('creationdate')
|
||||
if dt:
|
||||
if latest is None or dt > latest:
|
||||
latest = dt
|
||||
assetid2latest[assetid] = latest or ''
|
||||
# 按最新时间降序排列
|
||||
sorted_assetids = sorted(assetid2name.keys(), key=lambda aid: assetid2latest[aid], reverse=True)
|
||||
choices = [f"{assetid2name[aid]} [{aid}]" for aid in sorted_assetids]
|
||||
# 用 get_books_last_open 返回的时间戳排序,如无则为0
|
||||
ts = last_open_times.get(assetid, {}).get('last_open', 0)
|
||||
assetid2lastopen[assetid] = ts
|
||||
|
||||
# 按last_open时间戳降序排列
|
||||
sorted_assetids = sorted(assetid2name.keys(), key=lambda aid: assetid2lastopen[aid], reverse=True)
|
||||
choices = [f"{assetid2name[aid]} [{assetid2lastopen[aid]}]" for aid in sorted_assetids]
|
||||
if not choices:
|
||||
print("无可导出的笔记")
|
||||
exit(0)
|
||||
@@ -164,6 +167,7 @@ if __name__ == '__main__':
|
||||
multiselect=False,
|
||||
instruction="上下键选择,输入可模糊筛选,回车确定"
|
||||
).execute()
|
||||
|
||||
# 解析选中assetid
|
||||
for aid, name in assetid2name.items():
|
||||
if answer.startswith(name):
|
||||
@@ -172,8 +176,9 @@ if __name__ == '__main__':
|
||||
else:
|
||||
print("未找到选中书籍")
|
||||
exit(1)
|
||||
|
||||
# 只导出选中书的笔记
|
||||
selected_booksnote = {selected_assetid: booksnote[selected_assetid]}
|
||||
selected_booksnote = build_booksnote(bookid=selected_assetid)
|
||||
selected_booksinfo = {selected_assetid: booksinfo.get(selected_assetid, {})}
|
||||
out_path = f'export_notes/notes_export_{selected_assetid}.md'
|
||||
export_booksnote_to_md(selected_booksnote, selected_booksinfo, out_path)
|
||||
|
||||
Reference in New Issue
Block a user