-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar_utils.py
More file actions
506 lines (444 loc) Β· 18.5 KB
/
Copy pathcalendar_utils.py
File metadata and controls
506 lines (444 loc) Β· 18.5 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#!/usr/bin/env python3
"""
Calendar Utilities - Generate "Add to Calendar" Links
Provides functions to generate calendar links for various calendar services.
"""
from datetime import datetime
from typing import Dict, Any, Optional, List
from urllib.parse import quote_plus, urlencode
import re
def format_datetime_for_calendar(dt_str: str, timezone: str = "America/New_York") -> str:
"""
Convert ISO datetime string to format suitable for calendar links.
Args:
dt_str: ISO datetime string (e.g., "2025-09-20T14:00:00")
timezone: Timezone for the event
Returns:
Formatted datetime string for calendar URLs (YYYYMMDDTHHMMSSZ)
"""
try:
# Parse the datetime string
dt = datetime.fromisoformat(dt_str)
# Format for calendar links (UTC format)
return dt.strftime("%Y%m%dT%H%M%SZ")
except ValueError:
# If parsing fails, try to create a reasonable default
return datetime.now().strftime("%Y%m%dT%H%M%SZ")
def generate_google_calendar_link(
title: str,
start_datetime: str,
end_datetime: str,
description: str = "",
location: str = "",
timezone: str = "America/New_York",
attendees: List[str] = None
) -> str:
"""
Generate a Google Calendar 'Add to Calendar' link optimized for Gmail users.
Args:
title: Event title
start_datetime: Start time in ISO format
end_datetime: End time in ISO format
description: Event description
location: Event location
timezone: Event timezone
attendees: List of attendee email addresses
Returns:
Google Calendar add event URL optimized for Gmail integration
"""
base_url = "https://calendar.google.com/calendar/render"
# Format dates
start_formatted = format_datetime_for_calendar(start_datetime, timezone)
end_formatted = format_datetime_for_calendar(end_datetime, timezone)
# Enhanced description for Gmail users
enhanced_description = description
if description:
enhanced_description += "\n\n(Added via email invitation)"
# Create parameters optimized for Gmail
params = {
"action": "TEMPLATE",
"text": title,
"dates": f"{start_formatted}/{end_formatted}",
"details": enhanced_description,
"location": location,
"sf": "true",
"output": "xml",
"ctz": timezone # Include timezone for better Gmail integration
}
# Add attendees if provided (Gmail handles this well)
if attendees:
params["add"] = ",".join(attendees)
# Remove empty parameters
params = {k: v for k, v in params.items() if v}
return f"{base_url}?{urlencode(params, quote_via=quote_plus)}"
def generate_outlook_calendar_link(
title: str,
start_datetime: str,
end_datetime: str,
description: str = "",
location: str = "",
timezone: str = "America/New_York"
) -> str:
"""
Generate an Outlook Calendar 'Add to Calendar' link.
Args:
title: Event title
start_datetime: Start time in ISO format
end_datetime: End time in ISO format
description: Event description
location: Event location
timezone: Event timezone
Returns:
Outlook Calendar add event URL
"""
base_url = "https://outlook.live.com/calendar/0/deeplink/compose"
# Format dates for Outlook (ISO format)
start_formatted = start_datetime
end_formatted = end_datetime
# Create parameters
params = {
"subject": title,
"startdt": start_formatted,
"enddt": end_formatted,
"body": description,
"location": location
}
# Remove empty parameters
params = {k: v for k, v in params.items() if v}
return f"{base_url}?{urlencode(params, quote_via=quote_plus)}"
def generate_yahoo_calendar_link(
title: str,
start_datetime: str,
end_datetime: str,
description: str = "",
location: str = "",
timezone: str = "America/New_York"
) -> str:
"""
Generate a Yahoo Calendar 'Add to Calendar' link.
Args:
title: Event title
start_datetime: Start time in ISO format
end_datetime: End time in ISO format
description: Event description
location: Event location
timezone: Event timezone
Returns:
Yahoo Calendar add event URL
"""
base_url = "https://calendar.yahoo.com/"
# Format dates
start_formatted = format_datetime_for_calendar(start_datetime, timezone)
end_formatted = format_datetime_for_calendar(end_datetime, timezone)
# Create parameters
params = {
"v": "60",
"view": "d",
"type": "20",
"title": title,
"st": start_formatted,
"et": end_formatted,
"desc": description,
"in_loc": location
}
# Remove empty parameters
params = {k: v for k, v in params.items() if v}
return f"{base_url}?{urlencode(params, quote_via=quote_plus)}"
def generate_ics_content(
title: str,
start_datetime: str,
end_datetime: str,
description: str = "",
location: str = "",
timezone: str = "America/New_York"
) -> str:
"""
Generate ICS (iCalendar) file content for calendar events.
Args:
title: Event title
start_datetime: Start time in ISO format
end_datetime: End time in ISO format
description: Event description
location: Event location
timezone: Event timezone
Returns:
ICS file content as string
"""
# Format dates
start_formatted = format_datetime_for_calendar(start_datetime, timezone)
end_formatted = format_datetime_for_calendar(end_datetime, timezone)
# Generate unique ID
import uuid
uid = str(uuid.uuid4())
# Create timestamp
now = datetime.now().strftime("%Y%m%dT%H%M%SZ")
ics_content = f"""BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Your Organization//Your App//EN
CALSCALE:GREGORIAN
METHOD:PUBLISH
BEGIN:VEVENT
UID:{uid}
DTSTART:{start_formatted}
DTEND:{end_formatted}
DTSTAMP:{now}
SUMMARY:{title}
DESCRIPTION:{description}
LOCATION:{location}
STATUS:CONFIRMED
SEQUENCE:0
END:VEVENT
END:VCALENDAR"""
return ics_content
def generate_all_calendar_links(
title: str,
start_datetime: str,
end_datetime: str,
description: str = "",
location: str = "",
timezone: str = "America/New_York",
attendees: List[str] = None
) -> Dict[str, str]:
"""
Generate calendar links for all major calendar services, optimized for Gmail users.
Args:
title: Event title
start_datetime: Start time in ISO format
end_datetime: End time in ISO format
description: Event description
location: Event location
timezone: Event timezone
attendees: List of attendee email addresses
Returns:
Dictionary with calendar service names as keys and URLs as values
"""
return {
"google": generate_google_calendar_link(title, start_datetime, end_datetime, description, location, timezone, attendees),
"outlook": generate_outlook_calendar_link(title, start_datetime, end_datetime, description, location, timezone),
"yahoo": generate_yahoo_calendar_link(title, start_datetime, end_datetime, description, location, timezone),
"ics_content": generate_ics_content(title, start_datetime, end_datetime, description, location, timezone)
}
def create_gmail_optimized_email(
subject: str,
main_message: str,
event_title: str,
start_datetime: str,
end_datetime: str,
description: str = "",
location: str = "",
timezone: str = "America/New_York",
attendees: List[str] = None,
sender_name: str = "",
use_simple_button: bool = False
) -> str:
"""
Create a complete Gmail-optimized HTML email with calendar integration.
Args:
subject: Email subject (for reference, not included in body)
main_message: Main email content
event_title: Calendar event title
start_datetime: Start time in ISO format
end_datetime: End time in ISO format
description: Event description
location: Event location
timezone: Event timezone
attendees: List of attendee email addresses
sender_name: Name of the sender
use_simple_button: Use single Google Calendar button instead of all options
Returns:
Complete HTML email optimized for Gmail
"""
# Generate calendar links
calendar_links = generate_all_calendar_links(
title=event_title,
start_datetime=start_datetime,
end_datetime=end_datetime,
description=description,
location=location,
timezone=timezone,
attendees=attendees
)
# Choose calendar button format
if use_simple_button:
calendar_html = format_gmail_calendar_button(calendar_links, event_title)
else:
calendar_html = format_calendar_links_html(calendar_links, event_title)
# Format event details for display
from datetime import datetime
try:
start_dt = datetime.fromisoformat(start_datetime)
end_dt = datetime.fromisoformat(end_datetime)
date_str = start_dt.strftime("%A, %B %d, %Y")
time_str = f"{start_dt.strftime('%I:%M %p')} - {end_dt.strftime('%I:%M %p')}"
except:
date_str = "Date TBD"
time_str = "Time TBD"
# Create complete Gmail-optimized email
html_email = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{subject}</title>
</head>
<body style="margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #ffffff;">
<table cellpadding="0" cellspacing="0" border="0" style="width: 100%; max-width: 600px; margin: 0 auto;">
<tr>
<td style="padding: 20px;">
<!-- Main Message -->
<div style="margin-bottom: 30px; color: #202124; font-size: 14px; line-height: 1.6;">
{main_message}
</div>
<!-- Event Details -->
<table cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin-bottom: 20px; background-color: #f8f9fa; border-radius: 8px; border: 1px solid #e8eaed;">
<tr>
<td style="padding: 20px;">
<h2 style="margin: 0 0 15px 0; color: #1a73e8; font-size: 20px; font-weight: 500;">
π
{event_title}
</h2>
<table cellpadding="0" cellspacing="0" border="0" style="width: 100%;">
<tr>
<td style="padding: 5px 0; color: #5f6368; font-size: 14px;">
<strong>π
Date:</strong> {date_str}
</td>
</tr>
<tr>
<td style="padding: 5px 0; color: #5f6368; font-size: 14px;">
<strong>π Time:</strong> {time_str} ({timezone})
</td>
</tr>
{f'<tr><td style="padding: 5px 0; color: #5f6368; font-size: 14px;"><strong>π Location:</strong> {location}</td></tr>' if location else ''}
{f'<tr><td style="padding: 5px 0; color: #5f6368; font-size: 14px;"><strong>π Description:</strong> {description}</td></tr>' if description else ''}
</table>
</td>
</tr>
</table>
<!-- Calendar Links -->
{calendar_html}
{f'<p style="margin-top: 30px; color: #5f6368; font-size: 14px;">Best regards,<br><strong>{sender_name}</strong></p>' if sender_name else ''}
</td>
</tr>
</table>
</body>
</html>
"""
return html_email
def format_calendar_links_html(calendar_links: Dict[str, str], event_title: str = "Event") -> str:
"""
Format calendar links as HTML optimized for Gmail display.
Args:
calendar_links: Dictionary of calendar links from generate_all_calendar_links
event_title: Title of the event for display
Returns:
HTML formatted string with calendar links optimized for Gmail
"""
html = f"""
<table cellpadding="0" cellspacing="0" border="0" style="width: 100%; margin: 20px 0; font-family: Arial, sans-serif;">
<tr>
<td style="padding: 20px; background-color: #f8f9fa; border: 1px solid #e9ecef; border-radius: 8px;">
<table cellpadding="0" cellspacing="0" border="0" style="width: 100%;">
<tr>
<td>
<h3 style="margin: 0 0 15px 0; color: #1a73e8; font-size: 18px; font-weight: 500;">
π
Add "{event_title}" to Your Calendar
</h3>
<p style="margin: 0 0 20px 0; color: #5f6368; font-size: 14px; line-height: 1.4;">
Click below to add this event to your calendar:
</p>
</td>
</tr>
<tr>
<td style="padding-bottom: 15px;">
<!-- Primary Google Calendar Button (Gmail optimized) -->
<table cellpadding="0" cellspacing="0" border="0" style="display: inline-block; margin-right: 10px; margin-bottom: 10px;">
<tr>
<td style="background-color: #1a73e8; border-radius: 6px; padding: 12px 24px;">
<a href="{calendar_links.get('google', '#')}"
style="color: #ffffff; text-decoration: none; font-size: 14px; font-weight: 500; display: block;">
π
Add to Google Calendar
</a>
</td>
</tr>
</table>
<!-- Alternative calendar options -->
<table cellpadding="0" cellspacing="0" border="0" style="display: inline-block; margin-right: 10px; margin-bottom: 10px;">
<tr>
<td style="background-color: #0078d4; border-radius: 6px; padding: 8px 16px;">
<a href="{calendar_links.get('outlook', '#')}"
style="color: #ffffff; text-decoration: none; font-size: 12px; display: block;">
Outlook
</a>
</td>
</tr>
</table>
<table cellpadding="0" cellspacing="0" border="0" style="display: inline-block; margin-bottom: 10px;">
<tr>
<td style="background-color: #6001d2; border-radius: 6px; padding: 8px 16px;">
<a href="{calendar_links.get('yahoo', '#')}"
style="color: #ffffff; text-decoration: none; font-size: 12px; display: block;">
Yahoo
</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<p style="margin: 0; font-size: 12px; color: #80868b; line-height: 1.3;">
Can't access the links?
<a href="data:text/calendar;charset=utf8,{quote_plus(calendar_links.get('ics_content', ''))}"
download="event.ics"
style="color: #1a73e8; text-decoration: none;">
Download calendar file
</a>
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
"""
return html
def format_gmail_calendar_button(calendar_links: Dict[str, str], event_title: str = "Event") -> str:
"""
Format a single prominent Google Calendar button optimized for Gmail.
Args:
calendar_links: Dictionary of calendar links from generate_all_calendar_links
event_title: Title of the event for display
Returns:
HTML formatted string with a single Google Calendar button
"""
html = f"""
<table cellpadding="0" cellspacing="0" border="0" style="margin: 15px 0;">
<tr>
<td style="background-color: #1a73e8; border-radius: 8px; padding: 0;">
<a href="{calendar_links.get('google', '#')}"
style="display: block; padding: 14px 28px; color: #ffffff; text-decoration: none; font-size: 16px; font-weight: 500; font-family: Arial, sans-serif;">
π
Add to Google Calendar
</a>
</td>
</tr>
</table>
"""
return html
def format_calendar_links_text(calendar_links: Dict[str, str], event_title: str = "Event") -> str:
"""
Format calendar links as plain text for email inclusion.
Args:
calendar_links: Dictionary of calendar links from generate_all_calendar_links
event_title: Title of the event for display
Returns:
Plain text formatted string with calendar links
"""
text = f"""
π
ADD "{event_title.upper()}" TO YOUR CALENDAR
Add this event to your calendar by clicking one of these links:
π Google Calendar: {calendar_links.get('google', 'Not available')}
π Outlook Calendar: {calendar_links.get('outlook', 'Not available')}
π Yahoo Calendar: {calendar_links.get('yahoo', 'Not available')}
πΎ For other calendar apps, you can create an ICS file with the event details.
"""
return text