Python ical: get events for a day including recurring ones
Asked Answered
I

3

7

Is there an easy way to get a day's events from an ical file in Python?

For non-recurring, one day events I have used something like

from icalendar import Calendar
for event in Calendar.from_ical(ical).walk('vevent'):
    if edate > ref_ref_day_start and event.get('dtstart').dt < ref_day_end:
        # code here

But recurring events only occur in walk once.

I can see how having an infinite event iteration for repeating events without end could be a problem. But still there must be an easier way than calculating the repetitions by myself, right?

(I can't find much documentation. I read the icalendar test related to recurring events but it doesn't seem to do anything like this).

Induration answered 3/3, 2015 at 10:19 Comment(1)
(This question isn't solved yet. I already tried a small bounty, which expired. Any solutions are still most welcome!)Induration
R
4

You could use python-recurring-ical-events in addition to icalendar.

pip install recurring-ical-events

Then, you can change your code to

from icalendar import Calendar
import recurring_ical_events
import datetime

today = datetime.date.today()

for event in recurring_ical_events.of(Calendar.from_ical(ical)).at(today):
    # code here for events of today

icalendar only parses the calendar file. This module works on event repetitions.

Roping answered 25/12, 2021 at 10:16 Comment(1)
This is a good and extremely simple lib. of() also includes non-recurring events btw.Santee
B
2

you can use pyICSParser.

It will take an ICS file and return the dates of recurring events in a list of datetime objects.

You need to specify the timewindow you want those events to be returned, as otherwise events recurring without an UNTIL or COUNT parameter would render an infinite list.

(disclaimer I'm the author of the package)

Bevel answered 4/3, 2015 at 16:29 Comment(2)
Hmm the idea is really nice but I can't get it to work. pip throws an IOError so I downloaded the source. I adapted the example to mycal = iCalendar(); mycal.string_load(ical); print mycal.get_event_instances(start = '20150301', end = '20150315') to try and make it work, but get a long exception trace.Induration
Look like a nice package but the documentation is non-existant, and it can't read Google Calendar iCAL files. It fails with: RFC5545 specifies: \'\xc2\xa73.6.1 specifies that \'The "VEVENT" calendar component cannot be nested within another calendar component.\', following line is not compliant \n line: 17 - END:VTIMEZONE'Reminiscence
G
1

Yeah.Instead of Ical Do it By gcalcli

gcalcli is a Python application that allows you to access your Google Calendar(s) from a command line

[1]: https://github.com/insanum/gcalcli

Groome answered 3/3, 2015 at 10:27 Comment(1)
I need the results in Python, sorry that was a little unclear. Can it do that (I'm not finding much) or only command line?Induration

© 2022 - 2024 — McMap. All rights reserved.