- 新增 download_images.py: 批量下载消息图片到 downloads/images/ - 数据库移至 downloads/messages.db,SnifferStorage 默认使用项目目录 - 图片默认保存到 downloads/images/,支持 --out-dir 自定义 - .gitignore 排除 downloads/ 避免提交二进制文件 - 补充腾讯会议号过滤 prompt - 补充 Obsidian 文档 is_read frontmatter
121 lines
4.5 KiB
Python
121 lines
4.5 KiB
Python
"""Small upstream client extension for visible, date-bounded history sync."""
|
|
|
|
import json
|
|
|
|
from larkx.client import LarkClient
|
|
from larkx.proto import decoders
|
|
|
|
|
|
def _json_text_fields(value, parts: list[str]) -> None:
|
|
"""Collect human-readable card labels, values and links without style metadata."""
|
|
if isinstance(value, dict):
|
|
for key, item in value.items():
|
|
if key in {"style", "styleKeys", "property", "childIds", "elementIds"}:
|
|
continue
|
|
if key in {"title", "text", "content", "label", "value", "description", "summary", "name", "url", "link"}:
|
|
if isinstance(item, str) and item.strip():
|
|
parts.append(item.strip())
|
|
else:
|
|
_json_text_fields(item, parts)
|
|
elif isinstance(value, list):
|
|
for item in value:
|
|
_json_text_fields(item, parts)
|
|
|
|
|
|
def extract_card_text(data: dict) -> str:
|
|
"""Extract readable text from JSON and rich-text cards ignored upstream."""
|
|
parts = []
|
|
json_card = data.get("jsonCard")
|
|
if json_card:
|
|
try:
|
|
_json_text_fields(json.loads(json_card), parts)
|
|
except (TypeError, ValueError):
|
|
pass
|
|
rich_text = decoders.extract_rich_text(data.get("richtext") or {})
|
|
if rich_text and set(rich_text) != {"@"}:
|
|
parts.append(rich_text)
|
|
seen = set()
|
|
unique_parts = []
|
|
for part in parts:
|
|
if part and part not in seen:
|
|
seen.add(part)
|
|
unique_parts.append(part)
|
|
return "\n".join(unique_parts)[:8000]
|
|
|
|
|
|
class SnifferLarkClient(LarkClient):
|
|
def pull_history(
|
|
self,
|
|
chat_id: str,
|
|
positions,
|
|
save_storage=None,
|
|
visible_only=False,
|
|
thread_chat=None,
|
|
):
|
|
request = {"chatId": str(chat_id), "positions": [int(p) for p in positions]}
|
|
if visible_only:
|
|
request.update(
|
|
{
|
|
"type": "CHAT",
|
|
"onlyVisibleMessages": True,
|
|
"needNextPosition": True,
|
|
"direction": "UP",
|
|
}
|
|
)
|
|
response = self.api("messages.PullMessagesByPositionsRequest", request)
|
|
messages = (response or {}).get("messages") or {}
|
|
if thread_chat is None:
|
|
thread_chat = self.is_thread_chat(chat_id)
|
|
result = []
|
|
for position_key, value in sorted(messages.items(), key=lambda item: int(item[0])):
|
|
message_type = decoders.enum_to_int(value.get("type", 0))
|
|
chat_type = decoders.enum_to_int(value.get("chatType", 0))
|
|
summary, data = decoders.decode_message_content(
|
|
message_type, value.get("content", b"")
|
|
)
|
|
if message_type == 14 and data:
|
|
card_text = extract_card_text(data)
|
|
if card_text:
|
|
summary = f"{summary}\n{card_text}"
|
|
root_id = value.get("rootId") or ""
|
|
scope, anchor = decoders.resolve_scope(
|
|
value.get("id"),
|
|
message_type,
|
|
chat_type,
|
|
root_id,
|
|
value.get("threadId") or "",
|
|
thread_chat,
|
|
)
|
|
message = {
|
|
"msg_id": value.get("id"),
|
|
"msg_type": message_type,
|
|
"msg_type_name": decoders.MSG_TYPE_NAMES.get(
|
|
message_type, str(message_type)
|
|
),
|
|
"from_id": value.get("fromId"),
|
|
"chat_id": value.get("chatId") or value.get("channelId") or str(chat_id),
|
|
"chat_type": chat_type,
|
|
"chat_type_name": decoders.CHAT_TYPE_NAMES.get(
|
|
chat_type, str(chat_type)
|
|
),
|
|
"scope": scope,
|
|
"anchor": anchor,
|
|
"at_me": False,
|
|
"root_id": root_id,
|
|
"parent_id": value.get("parentId") or "",
|
|
"cid": value.get("cid") or "",
|
|
"position": value.get("position") or int(position_key),
|
|
"create_time": value.get("createTime"),
|
|
"content": summary,
|
|
"content_data": data,
|
|
}
|
|
result.append(message)
|
|
if save_storage is not None:
|
|
direction = (
|
|
"out"
|
|
if str(value.get("fromId") or "") == str(self.auth.user_id or "")
|
|
else "in"
|
|
)
|
|
save_storage.save_message(message, direction=direction)
|
|
return result
|