使用Python和vobject解析具有多个事件的iCal提要

时间:2011-10-06 02:25:00

标签: python icalendar vobject

我正在尝试使用多个事件解析一个Feed,它只返回一个项目

ics = urllib.urlopen("https://www.google.com/calendar/ical/pcolalug%40gmail.com/public/basic.ics").read()
events = []

components = vobject.readComponents(ics)
for event in components:
    to_zone = tz.gettz('America/Chicago')

    date = event.vevent.dtstart.value.astimezone(to_zone)
    description = event.vevent.description.value

    events.append({
                'start': date.strftime(DATE_FORMAT),
                'description': description if description else 'No Description', 
                })

return {'events': events[:10]}

我做错了什么?

1 个答案:

答案 0 :(得分:2)

切换到使用icalendar而不是vobject,它的效果要好得多。

ics = urllib.urlopen("https://www.google.com/calendar/ical/pcolalug%40gmail.com/public/basic.ics").read()
events = []

cal = Calendar.from_string(ics)

for event in cal.walk('vevent'):
    to_zone = tz.gettz('America/Chicago')

    date = event.get('dtstart').dt.astimezone(to_zone)
    description = event.get('description')

    events.append({
                'start': date.strftime(DATE_FORMAT),
                'description': description if description else 'No Description', 
                })

return {'events': events[:10]}