-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_engage.py
More file actions
355 lines (293 loc) · 11.2 KB
/
sync_engage.py
File metadata and controls
355 lines (293 loc) · 11.2 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
#!/usr/bin/env python3
"""
Sync events from Babson Engage iCal feed into EventBoost.
Usage:
python scripts/sync_engage.py
python scripts/sync_engage.py --url https://engage.babson.edu/ical/babsongrad/ical_babsongrad.ics
python scripts/sync_engage.py --dry-run
Smart matching:
- Orgs matched by engage_url → name (case-insensitive) → auto-create
- Events deduped by engage_uid → rsvp_link → insert new
- Tags extracted from structured CATEGORIES fields
"""
import argparse
import os
import re
import sys
from datetime import datetime, timezone
import requests
from icalendar import Calendar
from dotenv import load_dotenv
from supabase import create_client
# Load env from backend/.env
load_dotenv(os.path.join(os.path.dirname(__file__), '..', '.env'))
SUPABASE_URL = os.getenv("SUPABASE_URL")
SUPABASE_SERVICE_KEY = os.getenv("SUPABASE_SERVICE_KEY")
DEFAULT_ICAL_URL = "https://engage.babson.edu/ical/babsongrad/ical_babsongrad.ics"
if not SUPABASE_URL or not SUPABASE_SERVICE_KEY:
print("Error: SUPABASE_URL and SUPABASE_SERVICE_KEY must be set in .env")
sys.exit(1)
supabase = create_client(SUPABASE_URL, SUPABASE_SERVICE_KEY)
def parse_organizer(component):
"""
Extract org name and engage URL from ORGANIZER field.
Format: ORGANIZER;CN="Org Name":https://engage.babson.edu/ACRONYM/
"""
organizer = component.get("ORGANIZER")
if not organizer:
return None, None
# CN parameter has the org name
org_name = organizer.params.get("CN", "").strip().strip('"')
# The value is the URL
org_url = str(organizer).strip()
return org_name or None, org_url or None
def parse_categories(component):
"""
Extract structured categories from CATEGORIES fields.
The iCal has multiple CATEGORIES lines like:
CATEGORIES;X-CG-CATEGORY=club_acronym:BFAT
CATEGORIES;X-CG-CATEGORY=event_type:Social
CATEGORIES;X-CG-CATEGORY=event_tags:Career,Food
"""
tags = []
event_type = None
club_acronym = None
# component.get("CATEGORIES") returns only the first one
# We need to iterate through all CATEGORIES
for line in component.property_items():
name, prop = line
if name != "CATEGORIES":
continue
category_type = prop.params.get("X-CG-CATEGORY", "")
values = [str(v).strip() for v in prop.cats] if hasattr(prop, 'cats') else [str(prop).strip()]
if category_type == "event_type":
event_type = values[0] if values else None
tags.extend(v.lower() for v in values)
elif category_type == "event_tags":
tags.extend(v.lower() for v in values)
elif category_type == "club_acronym":
club_acronym = values[0] if values else None
# Deduplicate and clean
tags = list(dict.fromkeys(t for t in tags if t))
return tags, event_type, club_acronym
def parse_vevent(component):
"""Parse a VEVENT component into a dict."""
title = str(component.get("SUMMARY", "")).strip()
# Remove ENCODING artifacts
title = re.sub(r'\s+', ' ', title).strip()
uid = str(component.get("UID", "")).strip()
dtstart = component.get("DTSTART")
dtend = component.get("DTEND")
starts_at = None
if dtstart:
dt = dtstart.dt
if isinstance(dt, datetime):
starts_at = dt.isoformat()
else:
starts_at = datetime.combine(dt, datetime.min.time(), tzinfo=timezone.utc).isoformat()
ends_at = None
if dtend:
dt = dtend.dt
if isinstance(dt, datetime):
ends_at = dt.isoformat()
else:
ends_at = datetime.combine(dt, datetime.min.time(), tzinfo=timezone.utc).isoformat()
location = str(component.get("LOCATION", "")).strip() or None
# Clean up "Sign in to download the location"
if location and "sign in to download" in location.lower():
location = None
description = str(component.get("DESCRIPTION", "")).strip() or None
# Remove the trailing "---\nEvent Details: ..." from descriptions
if description:
description = re.split(r'\n---\n', description)[0].strip()
# Convert literal \n to actual newlines
description = description.replace('\\n', '\n')
rsvp_link = str(component.get("URL", "")).strip() or None
org_name, org_url = parse_organizer(component)
tags, event_type, club_acronym = parse_categories(component)
return {
"uid": uid,
"title": title,
"starts_at": starts_at,
"ends_at": ends_at,
"location": location,
"description": description,
"rsvp_link": rsvp_link,
"org_name": org_name,
"org_url": org_url,
"club_acronym": club_acronym,
"tags": tags,
"event_type": event_type,
}
def resolve_org(org_name, org_url, org_cache):
"""
Find or create an organization. Returns org_id.
Matching order:
1. Check cache (already resolved this session)
2. Match by engage_url
3. Match by name (case-insensitive)
4. Create new org
"""
if not org_name:
return None
cache_key = org_url or org_name
if cache_key in org_cache:
return org_cache[cache_key]
# Try by engage_url first
if org_url:
result = supabase.table("organizations") \
.select("id") \
.eq("engage_url", org_url) \
.execute()
if result.data:
org_id = result.data[0]["id"]
org_cache[cache_key] = org_id
return org_id
# Try by name (case-insensitive)
result = supabase.table("organizations") \
.select("id, engage_url") \
.ilike("name", org_name) \
.execute()
if result.data:
org_id = result.data[0]["id"]
# Update engage_url if not set
if org_url and not result.data[0].get("engage_url"):
supabase.table("organizations") \
.update({"engage_url": org_url}) \
.eq("id", org_id) \
.execute()
org_cache[cache_key] = org_id
return org_id
# Create new org
new_org = supabase.table("organizations").insert({
"name": org_name,
"engage_url": org_url,
"created_by": None,
}).execute()
org_id = new_org.data[0]["id"]
org_cache[cache_key] = org_id
return org_id
def resolve_event(event_data, org_id):
"""
Find or create an event. Returns (event_id, action).
Matching order:
1. Match by engage_uid
2. Match by rsvp_link (catches native EventBoost events with same RSVP link)
3. Insert new
"""
uid = event_data["uid"]
rsvp_link = event_data["rsvp_link"]
# Build the row data
row = {
"title": event_data["title"],
"starts_at": event_data["starts_at"],
"ends_at": event_data["ends_at"],
"location": event_data["location"],
"description": event_data["description"],
"rsvp_link": rsvp_link,
"tags": event_data["tags"],
"source": "engage_ical",
"engage_uid": uid,
"org_id": org_id,
"tone": "casual",
}
# 1. Check by engage_uid
if uid:
result = supabase.table("events") \
.select("id") \
.eq("engage_uid", uid) \
.execute()
if result.data:
event_id = result.data[0]["id"]
# Update with latest data
update_data = {k: v for k, v in row.items() if k != "engage_uid"}
supabase.table("events") \
.update(update_data) \
.eq("id", event_id) \
.execute()
return event_id, "updated"
# 2. Check by rsvp_link (native EventBoost event with same RSVP)
if rsvp_link:
result = supabase.table("events") \
.select("id, source") \
.eq("rsvp_link", rsvp_link) \
.execute()
if result.data:
event_id = result.data[0]["id"]
# Merge engage metadata into existing event
merge_data = {
"tags": event_data["tags"],
"engage_uid": uid,
}
# Only update source if it was already engage_ical
if result.data[0]["source"] == "engage_ical":
merge_data.update({
"location": event_data["location"],
"description": event_data["description"],
"ends_at": event_data["ends_at"],
})
supabase.table("events") \
.update(merge_data) \
.eq("id", event_id) \
.execute()
return event_id, "merged"
# 3. Insert new
result = supabase.table("events").insert(row).execute()
return result.data[0]["id"], "created"
def sync(ical_url, dry_run=False):
"""Main sync function."""
print(f"Fetching iCal feed from: {ical_url}")
resp = requests.get(ical_url, allow_redirects=True, timeout=30)
resp.raise_for_status()
print(f"Feed downloaded ({len(resp.content)} bytes)")
cal = Calendar.from_ical(resp.content)
events = []
for component in cal.walk():
if component.name == "VEVENT":
events.append(parse_vevent(component))
print(f"Parsed {len(events)} events from feed")
if dry_run:
print("\n--- DRY RUN (no database changes) ---\n")
orgs_seen = set()
for ev in events:
if ev["org_name"]:
orgs_seen.add(ev["org_name"])
print(f" [{ev['event_type'] or 'unknown'}] {ev['title']}")
print(f" Org: {ev['org_name']} | Tags: {ev['tags']}")
print(f" Date: {ev['starts_at']} | Location: {ev['location']}")
print()
print(f"Summary: {len(events)} events, {len(orgs_seen)} unique orgs")
return
# Process events
org_cache = {}
stats = {"orgs_created": 0, "events_created": 0, "events_updated": 0, "events_merged": 0}
for i, ev in enumerate(events, 1):
# Resolve org
org_id = resolve_org(ev["org_name"], ev["org_url"], org_cache)
# Resolve event
_, action = resolve_event(ev, org_id)
if action == "created":
stats["events_created"] += 1
elif action == "updated":
stats["events_updated"] += 1
elif action == "merged":
stats["events_merged"] += 1
print(f" [{i}/{len(events)}] {action.upper()}: {ev['title'][:60]}")
# Count new orgs (orgs that were created this session)
# We can approximate by checking how many orgs have engage_url set
all_orgs = supabase.table("organizations") \
.select("id") \
.not_.is_("engage_url", "null") \
.execute()
stats["orgs_total"] = len(all_orgs.data) if all_orgs.data else 0
print(f"\n--- Sync Complete ---")
print(f"Events created: {stats['events_created']}")
print(f"Events updated: {stats['events_updated']}")
print(f"Events merged: {stats['events_merged']}")
print(f"Total engage orgs: {stats['orgs_total']}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Sync Babson Engage events into EventBoost")
parser.add_argument("--url", default=DEFAULT_ICAL_URL, help="iCal feed URL")
parser.add_argument("--dry-run", action="store_true", help="Parse and print without DB changes")
args = parser.parse_args()
sync(args.url, dry_run=args.dry_run)