2026-11-10 21:55:50 +08:00
|
|
|
|
"""Read-only daily calendar instances through the existing LarkAgentX session."""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import date, datetime, time, timedelta
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
from zoneinfo import ZoneInfo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CalendarError(RuntimeError):
|
|
|
|
|
|
"""Calendar retrieval failed without exposing event contents."""
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-12 23:36:01 +08:00
|
|
|
|
def resolve_calendar_links(config: dict[str, Any]) -> dict[str, str]:
|
|
|
|
|
|
"""Validate manual date|summary to meeting-link mappings."""
|
|
|
|
|
|
links = config.get("calendar", {}).get("meeting_links", {})
|
|
|
|
|
|
if links is None:
|
|
|
|
|
|
return {}
|
|
|
|
|
|
if not isinstance(links, dict):
|
|
|
|
|
|
raise CalendarError("YAML 的 calendar.meeting_links 必须是映射。")
|
|
|
|
|
|
resolved = {}
|
|
|
|
|
|
for key, url in links.items():
|
|
|
|
|
|
if not isinstance(key, str) or not isinstance(url, str):
|
|
|
|
|
|
raise CalendarError("会议链接的键和 URL 必须是字符串。")
|
|
|
|
|
|
if not url.startswith(("https://", "http://")):
|
|
|
|
|
|
raise CalendarError("会议链接必须是 HTTP(S) URL。")
|
|
|
|
|
|
resolved[key] = url
|
|
|
|
|
|
return resolved
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-11-10 21:55:50 +08:00
|
|
|
|
def fetch_calendar_events(
|
2026-11-12 23:36:01 +08:00
|
|
|
|
client: Any,
|
|
|
|
|
|
target_day: date,
|
|
|
|
|
|
tz: ZoneInfo,
|
|
|
|
|
|
meeting_links: dict[str, str] | None = None,
|
2026-11-10 21:55:50 +08:00
|
|
|
|
) -> list[dict[str, Any]]:
|
|
|
|
|
|
"""Return normalized primary-calendar instances for one natural day."""
|
|
|
|
|
|
start = datetime.combine(target_day, time.min, tzinfo=tz)
|
|
|
|
|
|
end = start + timedelta(days=1)
|
|
|
|
|
|
try:
|
|
|
|
|
|
primary = client.api("calendars.GetUserPrimaryCalendarRequest", {}) or {}
|
|
|
|
|
|
calendar_id = str((primary.get("calendar") or {}).get("id") or "")
|
|
|
|
|
|
if not calendar_id:
|
|
|
|
|
|
raise CalendarError("未找到主日历 ID。")
|
|
|
|
|
|
response = client.api(
|
|
|
|
|
|
"calendarevents.QueryInstanceRequest",
|
|
|
|
|
|
{
|
|
|
|
|
|
"calendarIds": [calendar_id],
|
|
|
|
|
|
"startTime": int(start.timestamp()),
|
|
|
|
|
|
"endTime": int(end.timestamp()),
|
|
|
|
|
|
"needsBriefEvents": True,
|
|
|
|
|
|
},
|
|
|
|
|
|
) or {}
|
|
|
|
|
|
except CalendarError:
|
|
|
|
|
|
raise
|
|
|
|
|
|
except Exception as error:
|
|
|
|
|
|
raise CalendarError(f"日历请求失败({type(error).__name__})。") from error
|
|
|
|
|
|
|
|
|
|
|
|
event_map = response.get("events") or {}
|
|
|
|
|
|
events = []
|
|
|
|
|
|
for instance in response.get("calendarEventInstance") or []:
|
|
|
|
|
|
event = event_map.get(str(instance.get("eventRefId") or ""), {})
|
|
|
|
|
|
locations = event.get("locations") or []
|
|
|
|
|
|
location_names = [
|
|
|
|
|
|
str(location.get("name") or "")
|
|
|
|
|
|
for location in locations
|
|
|
|
|
|
if isinstance(location, dict) and location.get("name")
|
|
|
|
|
|
]
|
2026-11-12 23:36:01 +08:00
|
|
|
|
summary = str(event.get("summary") or "未命名会议")
|
|
|
|
|
|
native_url = str(event.get("meetingMinuteUrl") or event.get("url") or "")
|
|
|
|
|
|
manual_url = (meeting_links or {}).get(
|
|
|
|
|
|
f"{target_day.isoformat()}|{summary}", ""
|
|
|
|
|
|
)
|
2026-11-10 21:55:50 +08:00
|
|
|
|
events.append(
|
|
|
|
|
|
{
|
2026-11-12 23:36:01 +08:00
|
|
|
|
"summary": summary,
|
2026-11-10 21:55:50 +08:00
|
|
|
|
"begin": datetime.fromtimestamp(
|
|
|
|
|
|
int(instance["beginTime"]), tz
|
|
|
|
|
|
).isoformat(timespec="seconds"),
|
|
|
|
|
|
"end": datetime.fromtimestamp(
|
|
|
|
|
|
int(instance["endTime"]), tz
|
|
|
|
|
|
).isoformat(timespec="seconds"),
|
|
|
|
|
|
"location": ";".join(location_names),
|
|
|
|
|
|
"cancelled": bool(event.get("isDeleted")),
|
2026-11-12 23:36:01 +08:00
|
|
|
|
"url": native_url or manual_url,
|
2026-11-10 21:55:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
)
|
|
|
|
|
|
events.sort(key=lambda event: (event["begin"], event["summary"]))
|
|
|
|
|
|
return events
|