-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar_summary.py
More file actions
186 lines (157 loc) Β· 8.33 KB
/
calendar_summary.py
File metadata and controls
186 lines (157 loc) Β· 8.33 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
import datetime
import pytz
from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
from credentials import get_credentials_map
tz = pytz.timezone("America/Chicago")
from credentials import get_credentials_map # Adjust the import path if necessary
def generate_calendar_summary_for_user(user_email: str) -> str:
"""
Generates an HTML calendar summary for the specified user.
"""
credentials_map = get_credentials_map()
user_credentials = credentials_map.get(user_email)
if not user_credentials:
raise ValueError(f"No credentials found for user: {user_email}")
return generate_summary_html({user_email: user_credentials})
def create_calendar_edit_url(event_html_link):
if 'calendar.google.com' in event_html_link and 'eid=' in event_html_link:
return event_html_link.replace('/event?', '/eventedit?')
return event_html_link
def generate_summary_html(credentials_map: dict[str, Credentials]) -> str:
tomorrow = datetime.datetime.now(tz) + datetime.timedelta(days=1)
tomorrow_date = tomorrow.date()
# === Gather all events ===
all_events = []
for email, creds in credentials_map.items():
service = build('calendar', 'v3', credentials=creds)
start_of_day = tz.localize(datetime.datetime.combine(tomorrow_date, datetime.time.min)).isoformat()
end_of_day = tz.localize(datetime.datetime.combine(tomorrow_date, datetime.time.max)).isoformat()
events_result = service.events().list(
calendarId=email,
timeMin=start_of_day,
timeMax=end_of_day,
singleEvents=True,
orderBy='startTime'
).execute()
events = events_result.get('items', [])
for event in events:
start = event['start'].get('dateTime')
end = event['end'].get('dateTime')
if not start or not end:
continue
all_events.append({
"start": datetime.datetime.fromisoformat(start.replace('Z', '+00:00')).astimezone(tz),
"end": datetime.datetime.fromisoformat(end.replace('Z', '+00:00')).astimezone(tz),
"summary": event.get('summary', 'No Title'),
"creator": event.get('creator', {}).get('email', email),
"link": event.get('htmlLink', '#'),
"id": event.get('id', ''),
"calendar_email": email
})
all_events.sort(key=lambda x: x['start'])
tomorrow_str = tomorrow.strftime("%A, %B %d, %Y")
html = f"""<!DOCTYPE html>
<html><head><meta charset="UTF-8">
<title>Daily Calendar Summary</title>
<style>
body {{ font-family: 'Segoe UI', sans-serif; padding: 20px; background: #f9f9ff; color: #333; font-size: 14px; line-height: 1.3; }}
h1, h2 {{ color: #5b4fc7; margin: 10px 0 6px 0; }}
.event {{ padding: 6px 10px; margin: 6px 0; border-left: 4px solid #a88efb; background: #fff; }}
.event time {{ font-weight: bold; margin-right: 6px; display: inline-block; width: 120px; }}
.event a {{ color: #4a5bdc; text-decoration: none; }}
small {{ color: #666; font-size: 12px; }}
.conflict {{ color: #b33; font-weight: bold; margin: 4px 0; }}
.suggestion {{ color: #228B22; font-style: italic; margin-left: 20px; }}
.suggestion a {{ color: #1a7a1a; text-decoration: none; font-weight: bold; }}
.suggestion a:hover {{ text-decoration: underline; }}
</style></head><body>
<h1>π
{tomorrow_str} Schedule</h1>
"""
for event in all_events:
html += f"""
<div class="event">
<div><time>{event['start'].strftime('%I:%M %p')} β {event['end'].strftime('%I:%M %p')}</time><a href="{event['link']}" target="_blank">{event['summary']}</a></div>
<div><small>{event['creator']}</small></div>
</div>
"""
def overlap(a, b):
return max(a['start'], b['start']) < min(a['end'], b['end'])
conflicts = []
for i in range(len(all_events)):
for j in range(i + 1, len(all_events)):
if overlap(all_events[i], all_events[j]):
conflicts.append((all_events[i], all_events[j]))
def get_busy_times_for_date_range(start_date, end_date):
all_busy = []
for email, creds in credentials_map.items():
service = build('calendar', 'v3', credentials=creds)
body = {
"timeMin": tz.localize(datetime.datetime.combine(start_date, datetime.time(8, 0))).isoformat(),
"timeMax": tz.localize(datetime.datetime.combine(end_date, datetime.time(17, 0))).isoformat(),
"timeZone": "America/Chicago",
"items": [{"id": email}]
}
try:
freebusy = service.freebusy().query(body=body).execute()
for cal in freebusy.get('calendars', {}).values():
all_busy.extend([
(datetime.datetime.fromisoformat(busy['start'].replace('Z', '+00:00')).astimezone(tz),
datetime.datetime.fromisoformat(busy['end'].replace('Z', '+00:00')).astimezone(tz))
for busy in cal.get('busy', [])
])
except Exception as e:
print(f"Error querying freebusy for {email}: {e}")
return all_busy
all_busy = get_busy_times_for_date_range(tomorrow_date, tomorrow_date)
def is_slot_free(start, end, busy_times):
for busy_start, busy_end in busy_times:
if max(start, busy_start) < min(end, busy_end):
return False
return True
if conflicts:
html += "<h2 style='margin-top: 20px;'>β Conflicts Detected</h2>"
global_suggested_times = []
processed_events = set()
for a, b in conflicts:
html += f"<p class=\"conflict\">π₯ {a['summary']} β {b['summary']}<br>π {a['start'].strftime('%I:%M %p')}β{a['end'].strftime('%I:%M %p')} β {b['start'].strftime('%I:%M %p')}β{b['end'].strftime('%I:%M %p')}</p>"
event = a
event_key = (event['summary'], event['start'], event['end'])
if event_key in processed_events:
continue
processed_events.add(event_key)
duration = event['end'] - event['start']
now = datetime.datetime.now(tz)
suggestion_found = False
for offset in range(14):
candidate_day = tomorrow_date + datetime.timedelta(days=offset)
if candidate_day.weekday() >= 5:
continue
candidate_busy_times = get_busy_times_for_date_range(candidate_day, candidate_day)
for hour in range(8, 17):
for minute in [0, 30]:
proposed_start = tz.localize(datetime.datetime.combine(candidate_day, datetime.time(hour, minute)))
proposed_end = proposed_start + duration
if proposed_end.time() > datetime.time(17, 0) or proposed_start <= now:
continue
if any(max(proposed_start, s) < min(proposed_end, e) for s, e in global_suggested_times):
continue
if is_slot_free(proposed_start, proposed_end, candidate_busy_times):
day_name = proposed_start.strftime('%A')
date_str = proposed_start.strftime('%B %d')
time_str = proposed_start.strftime('%I:%M %p')
end_time_str = proposed_end.strftime('%I:%M %p')
edit_url = create_calendar_edit_url(event['link'])
html += f"""<p class='suggestion'>β
Suggest moving '<strong>{event['summary']}</strong>' to
<a href="{edit_url}" target="_blank">{day_name}, {date_str} at {time_str}β{end_time_str}</a></p>"""
global_suggested_times.append((proposed_start, proposed_end))
suggestion_found = True
break
if suggestion_found:
break
if suggestion_found:
break
if not suggestion_found:
html += f"<p class='suggestion'>β No free slots found in the next 2 weeks for '{event['summary']}' ({duration})</p>"
html += "</body></html>"
return html