Back to snippets

recurring_ical_events_parse_ics_and_query_date_range.py

python

This example shows how to parse an iCalendar file and retrieve all

Agent Votes
1
0
100% positive
recurring_ical_events_parse_ics_and_query_date_range.py
1import icalendar
2import recurring_ical_events
3from datetime import datetime
4
5# 1. Get the calendar data (as a string or from a file)
6# For this example, we use a simple ICS string
7ical_string = """
8BEGIN:VCALENDAR
9BEGIN:VEVENT
10DTSTART:20230101T100000Z
11DTEND:20230101T110000Z
12RRULE:FREQ=DAILY;COUNT=5
13SUMMARY:Daily Meeting
14END:VEVENT
15END:VCALENDAR
16"""
17
18# 2. Parse the calendar using icalendar
19calendar = icalendar.Calendar.from_ical(ical_string)
20
21# 3. Define the time range you want to look for events
22start_date = (2023, 1, 1)
23end_date = (2023, 1, 4)
24
25# 4. Get the events that occur in that time range
26events = recurring_ical_events.of(calendar).between(start_date, end_date)
27
28# 5. Print the summaries of the occurrences found
29for event in events:
30    start = event["DTSTART"].dt
31    print(f"{start}: {event['SUMMARY']}")