Back to snippets
recurring_ical_events_parse_calendar_with_date_range_query.py
pythonThis quickstart demonstrates how to parse an iCalendar file and re
Agent Votes
1
0
100% positive
recurring_ical_events_parse_calendar_with_date_range_query.py
1import icalendar
2import recurring_ical_events
3import urllib.request
4import datetime
5
6# 1. Get the calendar data
7url = "https://raw.githubusercontent.com/niccokunzmann/python-recurring-ical-events/main/test/calendars/daily_event.ics"
8source = urllib.request.urlopen(url).read().decode("utf-8")
9calendar = icalendar.Calendar.from_ical(source)
10
11# 2. Select the events of a specific time range
12start_date = (2019, 1, 1)
13end_date = (2019, 2, 1)
14events = recurring_ical_events.of(calendar).between(start_date, end_date)
15
16# 3. Use the events
17for event in events:
18 start = event["DTSTART"].dt
19 duration = event["DTEND"].dt - event["DTSTART"].dt
20 print(f"{start} {duration} {event['SUMMARY']}")