Skip to content

Instantly share code, notes, and snippets.

@Telling
Created December 9, 2013 16:41
Show Gist options
  • Save Telling/7875515 to your computer and use it in GitHub Desktop.
Save Telling/7875515 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
""" .ics parser
Usage:
ics_parser.py [start_date] [end_date] <file>
"""
from icalendar import Calendar
from docopt import docopt
# In the lack of a strftime function on timedelta objects.
def strfdelta(delta):
d = {'days': delta.days}
d['hours'], rem = divmod(delta.seconds, 3600)
d['minutes'], d['seconds'] = divmod(rem, 60)
return '{0}:{1}'.format(d['hours'], d['minutes'])
def parse_ics(ics):
data = open(ics, 'rb')
cal = Calendar.from_ical(data.read())
for component in cal.walk():
if component.name == 'VEVENT':
summary = component.get('summary')
start = component.get('dtstart').dt
end = component.get('dtend').dt
duration = end - start
str_duration = strfdelta(duration)
print '{0}: {1} - {2}'.format(start.date().strftime('%d/%m-%y'),
start.time().strftime('%H:%M'),
end.time().strftime('%H:%M'))
print u'{0} ({1})'.format(summary, str_duration)
print '-' * (len(summary) + len(str_duration) + 3)
print component.get('description')
print ''
data.close()
if __name__ == '__main__':
arguments = docopt(__doc__, version='ics-parser v0.1')
parse_ics(arguments['<file>'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment