File size: 1,006 Bytes
3b9a6b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from icalendar import Calendar


def extract_ical_entries(file_bytes):
    try:
        cal = Calendar.from_ical(file_bytes)
        entries = []
        for component in cal.walk():
            if component.name == "VEVENT":
                summary = str(component.get("summary", ""))
                dtstart = component.get("dtstart", "")
                dtend = component.get("dtend", "")

                def to_iso(val):
                    if hasattr(val, "dt"):
                        dt = val.dt
                        if hasattr(dt, "isoformat"):
                            return dt.isoformat()
                        return str(dt)
                    return str(val)

                entries.append(
                    {
                        "summary": summary,
                        "dtstart": to_iso(dtstart),
                        "dtend": to_iso(dtend),
                    }
                )
        return entries, None
    except Exception as e:
        return None, str(e)