From 313605f01ba2bffcb6a4596012181f6e293ee71f Mon Sep 17 00:00:00 2001 From: Benature Date: Sun, 29 Nov 2026 00:13:59 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BB=8E=E5=8D=A1=E7=89=87=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=8F=90=E5=8F=96=E6=99=BA=E8=83=BD=E7=BA=AA=E8=A6=81?= =?UTF-8?q?=E6=A0=87=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VERSION | 2 +- snooper_calendar.py | 24 +++++++++++++++------- tests/test_calendar.py | 46 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 62 insertions(+), 10 deletions(-) diff --git a/VERSION b/VERSION index d3b5ba4..f2722b1 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.11 +0.2.12 diff --git a/snooper_calendar.py b/snooper_calendar.py index 9e2d73b..aac22bc 100644 --- a/snooper_calendar.py +++ b/snooper_calendar.py @@ -16,6 +16,7 @@ AI_NOTES_CHAT_ID = "7391681490647744513" AI_NOTES_URL_RE = re.compile( r"https://[A-Za-z0-9.-]+\.feishu\.cn/(?:docx|minutes)/[A-Za-z0-9]+(?:\?[^\s\x00-\x1f]*)?" ) +TOPIC_RE = re.compile(r"Topic:\s*([^\n]+?)(?=Owner:|\Z)") def resolve_calendar_links(config: dict[str, Any]) -> dict[str, str]: @@ -35,6 +36,12 @@ def resolve_calendar_links(config: dict[str, Any]) -> dict[str, str]: return resolved +def _extract_topic(text: str) -> str: + """Extract a meeting topic from card text like 'Topic: 标题Owner: ...'.""" + match = TOPIC_RE.search(text or "") + return match.group(1).strip() if match else "" + + def fetch_ai_notes_links( client: Any, target_day: date, @@ -69,13 +76,15 @@ def fetch_ai_notes_links( {"chat_id": chat_id, "start": start_ts, "end": end_ts}, ) for content, content_data, ts in result: - # Search content and content_data separately to avoid mixing with JSON - for text_to_search in (str(content or ""), str(content_data or "")): + text = str(content or "") + data_text = str(content_data or "") + topic = _extract_topic(text) or _extract_topic(data_text) + for text_to_search in (text, data_text): for match in AI_NOTES_URL_RE.finditer(text_to_search): url = match.group(0) if url not in seen_urls: seen_urls.add(url) - links.append({"url": url, "create_time": int(ts), "topic": ""}) + links.append({"url": url, "create_time": int(ts), "topic": topic}) except Exception: pass # Fall back to network if storage read fails @@ -133,7 +142,10 @@ def _fetch_ai_notes_links_from_network( if ts: batch_min_time = ts if batch_min_time is None else min(batch_min_time, ts) if start_ts <= ts < end_ts: + content = str(msg.get("content") or "") content_data = msg.get("content_data") + content_data_str = str(content_data or "") + topic = _extract_topic(content) or _extract_topic(content_data_str) if content_data and storage is not None: # Extract URLs with topics from raw content_data extracted = storage._extract_links_with_topics(content_data) @@ -141,17 +153,15 @@ def _fetch_ai_notes_links_from_network( url = item["url"] if url not in seen_urls: seen_urls.add(url) - links.append({"url": url, "create_time": ts, "topic": item.get("topic", "")}) + links.append({"url": url, "create_time": ts, "topic": topic or item.get("topic", "")}) else: # Fallback to plain text search - content = str(msg.get("content") or "") - content_data_str = str(content_data or "") text_to_search = content + content_data_str for match in AI_NOTES_URL_RE.finditer(text_to_search): url = match.group(0) if url not in seen_urls: seen_urls.add(url) - links.append({"url": url, "create_time": ts, "topic": ""}) + links.append({"url": url, "create_time": ts, "topic": topic}) if batch_min_time and batch_min_time < start_ts: break upper = lower - 1 diff --git a/tests/test_calendar.py b/tests/test_calendar.py index 8e2d879..0ed880a 100644 --- a/tests/test_calendar.py +++ b/tests/test_calendar.py @@ -1,7 +1,49 @@ -from datetime import date +from datetime import date, datetime from zoneinfo import ZoneInfo -from snooper_calendar import fetch_calendar_events +from snooper_calendar import ( + AI_NOTES_CHAT_ID, + _extract_topic, + fetch_ai_notes_links, + fetch_calendar_events, +) +from snooper_storage import SnifferStorage + + +def test_extract_topic_from_card_text(): + text = ( + "[卡片]\nMeeting recording completed" + "Topic: 指数-金科定期AI业务讨论会Owner: 万轶伦" + ) + assert _extract_topic(text) == "指数-金科定期AI业务讨论会" + assert _extract_topic("https://efund.feishu.cn/docx/abc") == "" + + +def test_fetch_ai_notes_links_reads_topic_from_local_content(): + tz = ZoneInfo("Asia/Shanghai") + storage = SnifferStorage(url="sqlite:///:memory:") + ts = int(datetime(2026, 11, 17, 14, 0, tzinfo=tz).timestamp()) + storage.save_message({ + "msg_id": "minutes-1", + "chat_id": AI_NOTES_CHAT_ID, + "chat_type": 2, + "from_id": "bot", + "msg_type": 14, + "create_time": ts, + "content": ( + "[卡片]\nMeeting recording completed" + "Topic: 指数-金科定期AI业务讨论会Owner: 万轶伦\n" + "https://efund.feishu.cn/minutes/abc123" + ), + }) + + links = fetch_ai_notes_links( + object(), date(2026, 11, 17), tz, storage=storage + ) + + assert len(links) == 1 + assert links[0]["url"].startswith("https://efund.feishu.cn/minutes/abc123") + assert links[0]["topic"] == "指数-金科定期AI业务讨论会" class FakeCalendarClient: