Home > database >  Using python, how can I get the events for today through an .ics file
Using python, how can I get the events for today through an .ics file

Time:09-22

I want to be able to get all the events for today by reading a .ics file that is downloaded

CodePudding user response:

There are a few ics parsers like ics-py. Have you tried any? What exactly are you trying to do? (what format do you want your output to be in? what are you going to do with an 'event' in your code?

If you could show what you have tried (and what you want it to do, it would be easier to suggest something).

updated after comment: per the docs you just do:

from ics import Calendar

c = Calendar(your_ics_string)

print(c.events)

event = list(c.events)[0] # c.events is a set
print(type(event))

print(dir(event))

for event in c.events:
   ...

Note that I'm not familiar with ics-py, but the docs look very good.

Of course, you need to install it first (pip install ics) if you haven't already done so.

CodePudding user response:

something like this (in order to get today's event you need to compare the event start date against today)

ics_data = '''BEGIN:VCALENDAR
BEGIN:VEVENT
CREATED:20151219T021727Z
DTEND;TZID=America/Toronto:20170515T110000
DTSTAMP:20151219T022011Z
DTSTART;TZID=America/Toronto:20170515T100000
LAST-MODIFIED:20151219T021727Z
RRULE:FREQ=DAILY;UNTIL=20170519T035959Z
SEQUENCE:0
SUMMARY:Meeting
TRANSP:OPAQUE
UID:21B97459-D97B-4B23-AF2A-E2759745C299
END:VEVENT
BEGIN:VEVENT
CREATED:20151219T022011Z
DTEND;TZID=America/Toronto:20170518T120000
DTSTAMP:20151219T022011Z
DTSTART;TZID=America/Toronto:20170518T110000
LAST-MODIFIED:20151219T022011Z
RECURRENCE-ID;TZID=America/Toronto:20170518T100000
SEQUENCE:0
SUMMARY:Final Meeting
TRANSP:OPAQUE
UID:21B97459-D97B-4B23-AF2A-E2759745C299
END:VEVENT
END:VCALENDAR'''

events = []
in_event = False
lines = ics_data.split('\n')
for line in lines:
  if line == 'BEGIN:VEVENT':
    event = {}
    in_event = True
    continue
  if line == 'END:VEVENT':
    events.append(event)
    in_event = False
    continue
  if in_event:
    key,val = line.split(':')
    event[key]= val
for event in events:
  print(event)

output

{'CREATED': '20151219T021727Z', 'DTEND;TZID=America/Toronto': '20170515T110000', 'DTSTAMP': '20151219T022011Z', 'DTSTART;TZID=America/Toronto': '20170515T100000', 'LAST-MODIFIED': '20151219T021727Z', 'RRULE': 'FREQ=DAILY;UNTIL=20170519T035959Z', 'SEQUENCE': '0', 'SUMMARY': 'Meeting', 'TRANSP': 'OPAQUE', 'UID': '21B97459-D97B-4B23-AF2A-E2759745C299'}
{'CREATED': '20151219T022011Z', 'DTEND;TZID=America/Toronto': '20170518T120000', 'DTSTAMP': '20151219T022011Z', 'DTSTART;TZID=America/Toronto': '20170518T110000', 'LAST-MODIFIED': '20151219T022011Z', 'RECURRENCE-ID;TZID=America/Toronto': '20170518T100000', 'SEQUENCE': '0', 'SUMMARY': 'Final Meeting', 'TRANSP': 'OPAQUE', 'UID': '21B97459-D97B-4B23-AF2A-E2759745C299'}
  • Related