import config import plistlib import sqlite3 import os from collections import defaultdict class BookListManager: def __init__(self, plist_path=None, db_path=None): self.plist_path = plist_path or config.LOCAL_BOOKS_PLIST self.db_path = db_path or config.LOCAL_LIBRARY_DB self._booksinfo = None self._books_open = None def get_books_info(self): if self._booksinfo is not None: return self._booksinfo booksinfo = defaultdict(dict) with open(self.plist_path, 'rb') as f: plist_data = plistlib.load(f) for book in plist_data.get('Books', []): bk_id = book.get('BKGeneratedItemId') if not bk_id: continue booksinfo[bk_id] = { 'displayname': book.get('BKDisplayName', ''), 'author': book.get('artistName', ''), 'type': book.get('BKBookType', ''), 'bookid': bk_id, 'itemname': book.get('itemName', ''), 'path': book.get('path', ''), 'date': book.get('BKInsertionDate',''), 'updatedate': book.get('updateDate','') } self._booksinfo = booksinfo return booksinfo def get_books_last_open(self): if self._books_open is not None: return self._books_open books_open = defaultdict(dict) if not os.path.exists(self.db_path): return books_open try: conn = sqlite3.connect(self.db_path) cursor = conn.cursor() cursor.execute(''' SELECT ZASSETID, zlastopendate FROM ZBKLIBRARYASSET WHERE zlastopendate IS NOT NULL ''') rows = cursor.fetchall() for row in rows: asset_id, last_open = row if asset_id: books_open[asset_id] = { 'last_open': last_open } conn.close() except Exception as e: print(f'警告: 读取BKLibrary.sqlite失败: {e}') self._books_open = books_open return books_open if __name__ == '__main__': manager = BookListManager() booksinfo = manager.get_books_info() from pprint import pprint print("\n【前三条示例】") for k, v in list(booksinfo.items())[:3]: print(f"{k}:") pprint(v, sort_dicts=False, indent=2) print('-' * 60) print("\n【最近打开时间示例】") books_open = manager.get_books_last_open() import datetime for k, v in list(books_open.items())[:3]: ts = v['last_open'] dt = datetime.datetime(2001, 1, 1) + datetime.timedelta(seconds=ts) print(f"{k}: {dt} (timestamp: {ts})")