This commit is contained in:
douboer
2025-08-15 13:49:02 +08:00
parent 8873c70a60
commit 0bc6844209
76 changed files with 726 additions and 11197 deletions

View File

@@ -5,6 +5,8 @@ booklist_parse.py
- 解析iBooks的Books.plist提取所有书籍元数据书名、作者、路径、时间等
- 解析BKLibrary.sqlite获取每本书的最近打开时间苹果时间戳基准2001-01-01
依赖config.py 统一管理路径和配置项。
主要接口:
- parse_books_plist(plist_path):返回所有书籍元数据,结构为{bk_id: {...}}
- get_books_last_open(db_path):返回所有书籍最近打开时间,结构为{bk_id: {'last_open': 时间戳}}
@@ -12,13 +14,14 @@ booklist_parse.py
依赖plistlib, collections, sqlite3, os, datetime
典型用法:
booksinfo = parse_books_plist('./data/Books.plist')
books_open = get_books_last_open('data/BKLibrary.sqlite')
booksinfo = parse_books_plist(config.LOCAL_BOOKS_PLIST)
books_open = get_books_last_open(config.LOCAL_LIBRARY_DB)
"""
import config
import plistlib
from collections import defaultdict
def parse_books_plist(plist_path):
def parse_books_plist(plist_path=config.LOCAL_BOOKS_PLIST):
booksinfo = defaultdict(dict)
with open(plist_path, 'rb') as f: plist_data = plistlib.load(f)
for book in plist_data.get('Books', []):
@@ -38,7 +41,7 @@ def parse_books_plist(plist_path):
import sqlite3
import os
def get_books_last_open(db_path='data/BKLibrary.sqlite'):
def get_books_last_open(db_path=config.LOCAL_LIBRARY_DB):
"""
从BKLibrary.sqlite获取书籍最近打开时间
返回defaultdict(dict)bk_id为索引包含最近打开时间
@@ -51,8 +54,7 @@ def get_books_last_open(db_path='data/BKLibrary.sqlite'):
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# ZBKLIBRARYASSET表包含书籍信息
cursor.execute(''' SELECT ZASSETID, zlastopendate FROM ZBKLIBRARYASSET WHERE zlastopendate IS NOT NULL
''')
cursor.execute(''' SELECT ZASSETID, zlastopendate FROM ZBKLIBRARYASSET WHERE zlastopendate IS NOT NULL ''')
rows = cursor.fetchall()
for row in rows:
asset_id, last_open = row
@@ -67,7 +69,7 @@ def get_books_last_open(db_path='data/BKLibrary.sqlite'):
return books_open
if __name__ == '__main__':
booksinfo = parse_books_plist('./data/Books.plist')
booksinfo = parse_books_plist(config.LOCAL_BOOKS_PLIST)
from pprint import pprint
print("\n【前三条示例】")
for k, v in list(booksinfo.items())[:3]: