Created
December 31, 2012 15:03
-
-
Save dehowell/4420448 to your computer and use it in GitHub Desktop.
Python script for converting iCal events (using the appscript module) to a CSV file.
This file contains hidden or 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
import csv | |
import re | |
import sys | |
import appscript | |
def get_calendar(name): | |
calendar = appscript.app('iCal').calendars[name] | |
try: | |
calendar.get() | |
return calendar | |
except appscript.CommandError: | |
return None | |
def parse_summary(summary): | |
m = re.match('(?:(?P<category>\[.+?\]) ?)?(?P<title>[^\[\]]+)?', summary) | |
if m is not None: | |
d = m.groupdict() | |
return d['category'], d['title'] | |
else: | |
return None, None | |
writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONNUMERIC, quotechar='"') | |
writer.writerow(['category', 'title', 'start', 'duration']) | |
for event in get_calendar('Time Tracking').events.get(): | |
category, title = parse_summary(event.summary.get()) | |
start = event.start_date.get() | |
end = event.end_date.get() | |
duration = int((end - start).total_seconds() / 60) | |
writer.writerow([category, title, start, duration]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Events are title following the convention: