Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing iCal feed with Python using icalendar

I'm trying to parse a feed with multiple events using the icalendar lib in python.

'summary' , 'description' and so on works fine, but for 'dtstart' and 'dtend' it's returning me: icalendar.prop.vDDDTypes object at 0x101be62d0

def calTest():
    req = urllib2.Request('https://www.google.com/calendar/ical/XXXXXXXXXX/basic.ics')
    response = urllib2.urlopen(req)
    data = response.read()

    cal = Calendar.from_ical(data)

    for event in cal.walk('vevent'):

        date = event.get('dtstart')
        summery = event.get('summary')

        print str(date)
        print str(summery)

    return

What am I doing wrong? To use vobject its not a option, have to use the icalendar lib. Many thanks for any help for a python rookie.

like image 842
user3163194 Avatar asked Jan 05 '14 18:01

user3163194


2 Answers

The objects representing dtstart and dtend have an attribute dt which contains a standard datetime.datetime object.

start = event.get('dtstart')
print(start.dt)
like image 155
t-8ch Avatar answered Oct 06 '22 00:10

t-8ch


A little late here, but if that helps: the API has been updated since (I did the same mistake // copy pasting another stackoverflow post) You need to use the method decoded() instead of get()

You can find the latest API reference to icalendar here : http://icalendar.readthedocs.io/en/latest/api.html

replace your call to get by decoded :

date = event.decoded('dtstart')
summery = event.decoded('summary')

It should work.

like image 41
Franck Nouyrigat Avatar answered Oct 06 '22 00:10

Franck Nouyrigat