All pastes #1210001 Raw Edit

Mine

public text v1 · immutable
#1210001 ·published 2008-09-24 02:43 UTC
rendered paste body
#!/usr/bin/env python
# parsecal

"""This script parses an *.ics file from google calendar
and returns the date and textwrapped 'DESCRIPTION' from
each event whose 'SUMMARY' contains the string passed
in as sys.argv[2]
"""

from icalendar import Calendar, Event
#from datetime import datetime
import sys, textwrap, time, calendar

if len(sys.argv) < 3:
    print """
    usage: parsecal file.ics 'summary'

    'summary' may be any substring of an events SUMMARY field.
    All DECRIPTION fields of events containing that substring
    will be printed, prefaced by date, and starting with the latest.
    """
    sys.exit(1)

ical = sys.argv[1]
client = sys.argv[2]
cal = Calendar.from_string(open(ical,'rb').read())
events = {}

for event in cal.walk():
    if event.name == 'VEVENT' and \
        event.has_key('SUMMARY') and client in event['SUMMARY'] and \
        event.has_key('DESCRIPTION') and len(event['DESCRIPTION']) > 0:
            dt = event.decoded('dtstart')
            # google 'broke' DTSTART to accomodate Outlook: we need local time
            dt = time.localtime(calendar.timegm(dt.utctimetuple()))
            print time.mktime(dt)
            events[dt] = event.decoded('description')

# google uses last modified not dtstart date to order, so we need to sort
for key in sorted(events.iterkeys()):
    print time.asctime(key)
    for i in textwrap.wrap(events[key], 80): print i
    print ''