collective / icalendar

icalendar parser library for Python

Home Page:https://icalendar.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

help getting values from ics file

C-Powers opened this issue · comments

Hi there, I'm using this package to parse a file. I looked in the docs, but there doesn't seem to be much documentation on how to do this. This is what I've got, and it kinda works, but I'm not getting the native python types out of it.

I have vText to string, vDDDTypes to datetime, and vCalAddress to string that I need to parse.

Thanks for the help!

As an example:

gcal = Calendar.from_ical(content)
out: dict[str, Union[str, datetime]] = {}
for component in gcal.walk():
    if component.name == "VEVENT":
        print(component.get("UID"), type(component.get("UID")))
        print(
            vText.from_ical(component.get("UID")),
            type(vText.from_ical(component.get("UID"))),
        )
        

"""
prints:
[radact]@google.com <class 'icalendar.prop.vText'>
[radact]@google.com <class 'icalendar.prop.vText'>
"""

ah, I think it's to_ical instead of from_ical.

Sorry for the false alarm, I had to rubber duck myself, but this may be nice for someone in the future.

gcal = Calendar.from_ical(content)
    out: dict[str, Any] = {}
    for component in gcal.walk():
        if component.name == "VEVENT":
            out["id"] = vText.to_ical(component.get("UID")).decode("utf-8")

            out["start"] = vDDDTypes.to_ical(component.get("DTSTART")).decode("utf-8")  # type: ignore
            out["end"] = vDDDTypes.to_ical(component.get("DTEND")).decode("utf-8")  # type: ignore

            out["summary"] = vText.to_ical(component.get("SUMMARY")).decode("utf-8")
            out["description"] = vText.to_ical(component.get("DESCRIPTION")).decode(
                "utf-8"
            )
            out["location"] = vText.to_ical(component.get("LOCATION")).decode("utf-8")
            attendees = []
            for a in component.get("ATTENDEE"):
                attendees.append(vCalAddress.to_ical(a).decode("utf-8"))
            out["attendees"] = attendees

            out["google_meet_url"] = vText.to_ical(
                component.get("X-GOOGLE-CONFERENCE")
            ).decode("utf-8")
            ```

@C-Powers Can you provide a example ical file that you parsed with the above script?

I'd like to add it as an example to the docs