2011-10-06 51 views
1

我試圖解析與多個事件和它的唯一返回我一個項目解析iCal供稿與多個事件Python和VOBJECT

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]} 

我在做什麼錯源?

回答

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]} 
+0

icalendar在Linux中不可用。 vobject很有可能。 –

+0

那不是真的,你只是「點安裝icalendar」,它的工作原理。它的純python並沒有C擴展,所以它可以在所有平臺上運行。 https://pypi.python.org/pypi/icalendar/3.11.4 – sontek