Last active
July 6, 2018 20:15
-
-
Save cswingler/cce59e61f7301c2cb271b8e152aa09eb to your computer and use it in GitHub Desktop.
Apple Calendar (iCal) to Timex Datalink script
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 python3 | |
import subprocess | |
import tempfile | |
# Scrapes my calendar for output to my watch. | |
# Depends on: | |
# icalBuddy (https://github.com/ali-rantakari/icalBuddy) | |
# datalink tools from https://github.com/dfries/datalink_ironman | |
def getEvents(): | |
""" | |
Calls out to iCalBuddy and gets my events. | |
:return: | |
""" | |
s = subprocess.check_output( | |
[ | |
'icalBuddy', | |
'--bullet', '%%EVENT%%', | |
'--includeOnlyEventsFromNowOn', | |
'--excludeAllDayEvents', | |
'--noCalendarNames', | |
'--noRelativeDates', | |
'--excludeEndDates', | |
'-iep','title,datetime', | |
'-df', '%m/%d', | |
'-tf', '%H:%M', | |
'eventsToday+7' | |
], | |
) | |
d = s.decode('unicode_escape') | |
return d | |
def buddy_to_tupleset(buddy_output): | |
""" | |
given the output of getEVents(), store it in a set of tuples. | |
:param buddy_output: | |
:return: | |
""" | |
events = buddy_output.split('%%EVENT%%') | |
events_s = set() | |
for e in events: | |
try: | |
description, timestamp = e.splitlines() | |
timestamp = timestamp.lstrip() | |
date, time = timestamp.split(' at ') | |
event_t = (date, time, description) | |
events_s.add(event_t) | |
except ValueError: | |
pass # Probably fine. | |
return events_s | |
def datalink_string(events_s): | |
""" | |
Given the events_set, print out the datalink output. | |
:param events_s: | |
:return: | |
""" | |
r_s = "" | |
for event in events_s: | |
date, time, description = event | |
r_s += 'appointment = {date}, {time}, "{description}"\n'.format(date=date, time=time, description=description) | |
return r_s | |
def send_to_watch(datalink_string): | |
""" | |
Fires up ./setwatch and fires noise at your watch. | |
:param datalink_string: | |
:return: | |
""" | |
with tempfile.NamedTemporaryFile(mode='w') as fp: | |
fp.write(datalink_string) | |
fp.flush() | |
subprocess.run([ | |
'/Users/chrisswingler/src/datalink_ironman/datalink/setwatch', | |
'-serial', | |
'-model150', | |
'-sort-app-by-datetime', | |
'-all', | |
fp.name | |
] | |
) | |
if __name__ == "__main__": | |
e = getEvents() | |
events = buddy_to_tupleset(e) | |
ds = datalink_string(events) | |
send_to_watch(ds) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment