feat: 历史同步优化与消息已读属性
- 历史同步: 本地缓存优先、断点续传、按月批量同步 - 新增 sync_monthly.py: 支持逐月往前推进同步 - 消息表新增 is_read 字段,默认 false - Obsidian 文档 frontmatter 自动补充 is_read: false - 新增 migrate_add_is_read.py / migrate_obsidian_is_read.py 迁移脚本 - 总结 prompt 补充: 不记录腾讯会议号 - 历史同步失败时记录日志到 ~/.larkx/logs/history-failures.jsonl
This commit is contained in:
79
migrate_obsidian_is_read.py
Normal file
79
migrate_obsidian_is_read.py
Normal file
@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""批量给 Obsidian 总结文档补上 is_read: false frontmatter 属性。"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
def has_frontmatter(content: str) -> bool:
|
||||
return content.startswith("---\n")
|
||||
|
||||
|
||||
def add_is_read_to_frontmatter(content: str) -> tuple[str, bool]:
|
||||
"""返回 (新内容, 是否修改)。"""
|
||||
if not has_frontmatter(content):
|
||||
return f"---\nis_read: false\n---\n\n{content}", True
|
||||
|
||||
# 提取 frontmatter
|
||||
match = re.match(r"^---\n(.*?)\n---\n", content, re.DOTALL)
|
||||
if not match:
|
||||
return content, False
|
||||
|
||||
frontmatter_text = match.group(1)
|
||||
rest = content[match.end():]
|
||||
|
||||
try:
|
||||
props = yaml.safe_load(frontmatter_text) or {}
|
||||
except yaml.YAMLError:
|
||||
return content, False
|
||||
|
||||
if "is_read" in props:
|
||||
return content, False
|
||||
|
||||
props["is_read"] = False
|
||||
new_frontmatter = yaml.safe_dump(props, allow_unicode=True, sort_keys=False).strip()
|
||||
return f"---\n{new_frontmatter}\n---\n{rest}", True
|
||||
|
||||
|
||||
def process_directory(directory: Path) -> tuple[int, int]:
|
||||
"""返回 (处理文件数, 修改文件数)。"""
|
||||
if not directory.exists():
|
||||
return 0, 0
|
||||
processed = 0
|
||||
modified = 0
|
||||
for path in sorted(directory.glob("*.md")):
|
||||
content = path.read_text(encoding="utf-8")
|
||||
new_content, changed = add_is_read_to_frontmatter(content)
|
||||
processed += 1
|
||||
if changed:
|
||||
path.write_text(new_content, encoding="utf-8")
|
||||
modified += 1
|
||||
print(f" 已更新: {path.name}")
|
||||
return processed, modified
|
||||
|
||||
|
||||
def main():
|
||||
directories = [
|
||||
Path.home() / ".larkx" / "summaries",
|
||||
Path(
|
||||
"/Users/benature/Library/Mobile Documents/iCloud~md~obsidian/Documents/Benature/Work/Business/Internal/LarkMsg"
|
||||
),
|
||||
]
|
||||
|
||||
total_processed = 0
|
||||
total_modified = 0
|
||||
for directory in directories:
|
||||
print(f"处理目录: {directory}")
|
||||
processed, modified = process_directory(directory)
|
||||
total_processed += processed
|
||||
total_modified += modified
|
||||
print(f" 共 {processed} 个文件,修改 {modified} 个\n")
|
||||
|
||||
print(f"总计: 处理 {total_processed} 个文件,修改 {total_modified} 个")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user