-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar_manager.py
More file actions
305 lines (274 loc) · 10.9 KB
/
calendar_manager.py
File metadata and controls
305 lines (274 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import json
import requests
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
import platform
from tzlocal import get_localzone
def get_user_timezone():
"""Get the user's local timezone"""
try:
local_tz = get_localzone()
if hasattr(local_tz, 'key'):
return local_tz.key
elif hasattr(local_tz, 'zone'):
return local_tz.zone
else:
return str(local_tz)
except:
return 'UTC'
def parse_time(time_str):
"""
Parse time string using built-in Python datetime parsing with added natural language support
"""
local_tz = ZoneInfo(get_user_timezone())
now = datetime.now(local_tz)
# Handle common natural language expressions
time_str = time_str.lower().strip()
# Handle date-only expressions
if time_str == 'today':
return now.replace(hour=0, minute=0, second=0, microsecond=0)
elif time_str == 'tomorrow':
tomorrow = now + timedelta(days=1)
return tomorrow.replace(hour=0, minute=0, second=0, microsecond=0)
elif time_str == 'yesterday':
yesterday = now - timedelta(days=1)
return yesterday.replace(hour=0, minute=0, second=0, microsecond=0)
elif time_str == 'next week':
next_week = now + timedelta(weeks=1)
return next_week.replace(hour=0, minute=0, second=0, microsecond=0)
# Handle "tomorrow at X" or "tomorrow X" formats
if time_str.startswith('tomorrow'):
base_date = now + timedelta(days=1)
time_part = time_str.replace('tomorrow', '').replace('at', '').strip()
try:
time_obj = datetime.strptime(time_part, "%H:%M")
except ValueError:
try:
time_obj = datetime.strptime(time_part, "%I:%M %p")
except ValueError:
raise ValueError(f"Could not parse time part in: {time_str}")
return datetime.combine(base_date.date(), time_obj.time()).replace(tzinfo=local_tz)
# Try parsing as ISO format
try:
parsed_time = datetime.fromisoformat(time_str)
if parsed_time.tzinfo is None:
return parsed_time.replace(tzinfo=local_tz)
return parsed_time
except ValueError:
pass
# Try parsing as time
try:
# Try 24-hour format
time_obj = datetime.strptime(time_str, "%H:%M")
naive_time = datetime.combine(now.date(), time_obj.time())
return naive_time.replace(tzinfo=local_tz)
except ValueError:
try:
# Try 12-hour format
time_obj = datetime.strptime(time_str, "%I:%M %p")
naive_time = datetime.combine(now.date(), time_obj.time())
return naive_time.replace(tzinfo=local_tz)
except ValueError:
raise ValueError(f"Could not parse time string: {time_str}")
def create_calendar_event(summary, start_time, end_time=None, description=None):
"""
Create a new calendar event
"""
try:
timezone = get_user_timezone()
start_time_dt = parse_time(start_time)
# If no end time provided, default to start time + 1 hour
if end_time is None:
end_time_dt = start_time_dt + timedelta(hours=1)
else:
end_time_dt = parse_time(end_time)
data = {
"summary": summary,
"startTime": start_time_dt.isoformat(),
"endTime": end_time_dt.isoformat(),
"timeZone": timezone
}
if description:
data["description"] = description
headers = {
"Authorization": f"Bearer {authtoken}"
}
response = requests.post(
"https://scripty.me/api/assistant/calendar/events",
headers=headers,
json=data
)
response.raise_for_status()
return response.json()
except Exception as e:
return {"success": False, "error": str(e)}
def get_calendar_events():
"""
Get all calendar events
"""
try:
headers = {
"Authorization": f"Bearer {authtoken}"
}
response = requests.get(
"https://scripty.me/api/assistant/calendar/events",
headers=headers
)
response.raise_for_status()
return response.json()
except Exception as e:
return {"success": False, "error": str(e)}
def get_free_slots(start_date=None, end_date=None, min_duration=30):
"""
Get free time slots in calendar.
start_date: can be 'today', 'tomorrow', or other supported date expressions
end_date: same as start_date
min_duration: minimum duration of free slot in minutes
"""
try:
timezone = get_user_timezone()
local_tz = ZoneInfo(timezone)
now = datetime.now(local_tz)
if not start_date:
start_date = now.replace(hour=0, minute=0, second=0, microsecond=0)
else:
try:
start_date = parse_time(start_date)
except ValueError as e:
return {"success": False, "error": str(e)}
if not end_date:
# Default to end of current week
days_until_sunday = (6 - now.weekday()) % 7
end_date = (now + timedelta(days=days_until_sunday)).replace(
hour=23, minute=59, second=59, microsecond=0
)
else:
try:
end_date = parse_time(end_date)
except ValueError as e:
return {"success": False, "error": str(e)}
headers = {
"Authorization": f"Bearer {authtoken}"
}
response = requests.get(
"https://scripty.me/api/assistant/calendar/events",
headers=headers
)
response.raise_for_status()
events = response.json().get('items', [])
busy_slots = []
for event in events:
event_start = datetime.fromisoformat(event['start'].get('dateTime', event['start'].get('date')))
event_end = datetime.fromisoformat(event['end'].get('dateTime', event['end'].get('date')))
if event_start.tzinfo is None:
event_start = event_start.replace(tzinfo=local_tz)
if event_end.tzinfo is None:
event_end = event_end.replace(tzinfo=local_tz)
if event_end > start_date and event_start < end_date:
busy_slots.append((event_start, event_end))
busy_slots.sort(key=lambda x: x[0])
free_slots = []
current_time = max(start_date, now)
for busy_start, busy_end in busy_slots:
if current_time + timedelta(minutes=min_duration) <= busy_start:
free_slots.append({
"start": current_time.isoformat(),
"end": busy_start.isoformat()
})
current_time = max(current_time, busy_end)
if current_time + timedelta(minutes=min_duration) <= end_date:
free_slots.append({
"start": current_time.isoformat(),
"end": end_date.isoformat()
})
return {
"success": True,
"free_slots": free_slots,
"timezone": timezone,
"period": {
"start": start_date.isoformat(),
"end": end_date.isoformat()
}
}
except Exception as e:
return {"success": False, "error": str(e)}
async def func(args):
"""
Main function to handle different calendar actions
"""
try:
action = args.get("action")
if not action:
return json.dumps({"success": False, "error": "Action is required"})
if action == "get_free_slots":
return json.dumps(get_free_slots(
args.get("start_date"),
args.get("end_date"),
int(args.get("min_duration", 30))
))
elif action == "get_events":
return json.dumps(get_calendar_events())
elif action == "create_event":
# Only validate start_time
if "start_time" not in args:
return json.dumps({
"success": False,
"error": "Missing required field: start_time"
})
return json.dumps(create_calendar_event(
args.get("summary", "New Event"),
args["start_time"],
args.get("end_time"),
args.get("description")
))
else:
return json.dumps({
"success": False,
"error": f"Unknown action: {action}"
})
except Exception as e:
return json.dumps({"success": False, "error": str(e)})
# API definition with detailed parameter descriptions
object = {
"name": "calendar_manager",
"description": "Manage Google Calendar events and find free time slots. All times are handled in your local timezone.",
"parameters": {
"type": "object",
"properties": {
"action": {
"type": "string",
"description": "Action to perform: 'get_events' (list all events), 'create_event' (create new event), or 'get_free_slots' (find available time slots)",
"enum": ["get_events", "create_event", "get_free_slots"]
},
"summary": {
"type": "string",
"description": "Event title (optional for create_event, defaults to 'New Event')"
},
"description": {
"type": "string",
"description": "Event description (optional for create_event)"
},
"start_time": {
"type": "string",
"description": "Event start time (required for create_event). Accepts multiple formats:\n- ISO format: '2024-01-30T15:00:00'\n- Natural language: 'tomorrow 3:00 PM'\n- 24-hour time: '15:00'\n- 12-hour time: '3:00 PM'"
},
"end_time": {
"type": "string",
"description": "Event end time (required for create_event). Accepts same formats as start_time"
},
"start_date": {
"type": "string",
"description": "Start date for finding free slots (optional for get_free_slots). Accepts ISO format ('2024-01-30') or basic expressions ('today', 'tomorrow', 'next week'). Defaults to today"
},
"end_date": {
"type": "string",
"description": "End date for finding free slots (optional for get_free_slots). Accepts same formats as start_date. Defaults to end of current week"
},
"min_duration": {
"type": "integer",
"description": "Minimum duration in minutes for free time slots (optional for get_free_slots, defaults to 30)"
}
},
"required": ["action"]
}
}