Created
December 9, 2013 16:41
-
-
Save Telling/7875515 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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