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).

Auberon Vacher
  • 4,655
  • 1
  • 24
  • 36
Mark
  • 18,730
  • 7
  • 107
  • 130
  • (This question isn't solved yet. I already tried a small bounty, which expired. Any solutions are still most welcome!) – Mark Mar 14 '15 at 17:42

3 Answers3

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)

Auberon Vacher
  • 4,655
  • 1
  • 24
  • 36
  • 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. – Mark Mar 06 '15 at 13:37
  • 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'` – kleptog Oct 06 '16 at 09:19
2

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.

User
  • 14,131
  • 2
  • 40
  • 59
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

  • 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? – Mark Mar 03 '15 at 10:57