forked from andrewramsay/ical_to_gcal_sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ical_to_gcal_sync.py
265 lines (220 loc) · 12.1 KB
/
ical_to_gcal_sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from __future__ import print_function
import logging
import time
import string
import re
import sys
import pickle
import googleapiclient
import requests
import ics
import arrow
from auth import auth_with_calendar_api
from config import *
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename=LOGFILE, mode='a')
handler.setFormatter(logging.Formatter('%(asctime)s|[%(levelname)s] %(message)s'))
logger.addHandler(handler)
def get_current_events():
"""Retrieves data from iCal iCal feed and returns an ics.Calendar object
containing the parsed data.
Returns the parsed Calendar object or None if an error occurs.
"""
resp = requests.get(ICAL_FEED)
if resp.status_code != 200:
logger.error('> Error retrieving iCal feed!')
return None
try:
cal = ics.Calendar(resp.text)
except Exception as e:
logger.error('> Error parsing iCal data ({})'.format(e))
return None
return cal
# modified from Google Calendar API quickstart example
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
store = Storage(CREDENTIAL_PATH)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store, None)
return credentials
def get_gcal_events(service, from_time):
"""Retrieves the current set of Google Calendar events from the selected
user calendar. Only includes upcoming events (those taking place from start
of the current day.
Returns a dict containing the event(s) existing in the calendar.
"""
# The list() method returns a dict containing various metadata along with the actual calendar entries (if any).
# It is not guaranteed to return all available events in a single call, and so may need called multiple times
# until it indicates no more events are available, signalled by the absence of "nextPageToken" in the result dict
logger.debug('Retrieving Google Calendar events')
# make an initial call, if this returns all events we don't need to do anything else,,,
eventsResult = service.events().list(calendarId=CALENDAR_ID,
timeMin=from_time,
singleEvents=True,
orderBy='startTime',
showDeleted=True).execute()
events = eventsResult.get('items', [])
# if nextPageToken is NOT in the dict, this should be everything
if 'nextPageToken' not in eventsResult:
logger.info('> Found {:d} upcoming events in Google Calendar (single page)'.format(len(events)))
return events
# otherwise keep calling the method, passing back the nextPageToken each time
while 'nextPageToken' in eventsResult:
token = eventsResult['nextPageToken']
eventsResult = service.events().list(calendarId=CALENDAR_ID,
timeMin=from_time,
pageToken=token,
singleEvents=True,
orderBy='startTime',
showDeleted=True).execute()
newevents = eventsResult.get('items', [])
events.extend(newevents)
logger.debug('> Found {:d} events on new page, {:d} total'.format(len(newevents), len(events)))
logger.info('> Found {:d} upcoming events in Google Calendar (multi page)'.format(len(events)))
return events
def delete_all_events(service):
for gc in get_gcal_events(service):
try:
service.events().delete(calendarId=CALENDAR_ID, eventId=gc['id']).execute()
time.sleep(API_SLEEP_TIME)
except googleapiclient.errors.HttpError:
pass # event already marked as deleted
def get_gcal_datetime(arrow_datetime, gcal_timezone):
arrow_datetime = arrow_datetime.to(gcal_timezone)
return {u'dateTime': arrow_datetime.format('YYYY-MM-DDTHH:mm:ssZZ'), 'timeZone': gcal_timezone}
def get_gcal_date(arrow_datetime):
return {u'date': arrow_datetime.format('YYYY-MM-DD')}
def create_id(uid, begintime, endtime):
""" Converts ical UUID, begin and endtime to a valid Gcal ID
Characters allowed in the ID are those used in base32hex encoding, i.e. lowercase letters a-v and digits 0-9, see section 3.1.2 in RFC2938
Te length of the ID must be between 5 and 1024 characters
https://developers.google.com/resources/api-libraries/documentation/calendar/v3/python/latest/calendar_v3.events.html
Returns:
ID
"""
allowed_chars = string.ascii_lowercase[:22] + string.digits
temp = re.sub('[^{}]'.format(allowed_chars), '', uid.lower())
return re.sub('[^{}]'.format(allowed_chars), '', uid.lower()) + str(arrow.get(begintime).timestamp) + str(arrow.get(endtime).timestamp)
if __name__ == '__main__':
# setting up Google Calendar API for use
logger.debug('> Loading credentials')
service = auth_with_calendar_api()
# retrieve events from Google Calendar, starting from beginning of current day
today = arrow.now().replace(hour=0, minute=0, second=0, microsecond=0)
logger.info('> Retrieving events from Google Calendar')
gcal_events = get_gcal_events(service, today.isoformat())
# retrieve events from the iCal feed
logger.info('> Retrieving events from iCal feed')
ical_cal = get_current_events()
if ical_cal is None:
sys.exit(-1)
# convert iCal event list into a dict indexed by (converted) iCal UID
ical_events = {}
for ev in ical_cal.events:
# filter out events in the past, don't care about syncing them
if arrow.get(ev.begin) > today:
# optionally filter out events >24 hours ahead
if ICAL_DAYS_TO_SYNC > 0:
tdelta = ev.begin - arrow.now()
if tdelta.days >= ICAL_DAYS_TO_SYNC:
logger.info(u'Filtering out event {} at {} due to ICAL_DAYS_TO_SYNC={}'.format(ev.name, ev.begin, ICAL_DAYS_TO_SYNC))
else:
ical_events[create_id(ev.uid, ev.begin, ev.end)] = ev
else:
ical_events[create_id(ev.uid, ev.begin, ev.end)] = ev
logger.debug('> Collected {:d} iCal events'.format(len(ical_events)))
# retrieve the Google Calendar object itself
gcal_cal = service.calendars().get(calendarId=CALENDAR_ID).execute()
logger.info('> Processing Google Calendar events...')
gcal_event_ids = [ev['id'] for ev in gcal_events]
# first check the set of Google Calendar events against the list of iCal
# events. Any events in Google Calendar that are no longer in iCal feed
# get deleted. Any events still present but with changed start/end times
# get updated.
for gcal_event in gcal_events:
eid = gcal_event['id']
if eid not in ical_events:
# if a gcal event has been deleted from iCal, also delete it from gcal.
# Apparently calling delete() only marks an event as "deleted" but doesn't
# remove it from the calendar, so it will continue to stick around.
# If you keep seeing messages about events being deleted here, you can
# try going to the Google Calendar site, opening the options menu for
# your calendar, selecting "View bin" and then clicking "Empty bin
# now" to completely delete these events.
try:
logger.info(u'> Deleting event "{}" from Google Calendar...'.format(gcal_event.get('summary', '<unnamed event>')))
service.events().delete(calendarId=CALENDAR_ID, eventId=eid).execute()
time.sleep(API_SLEEP_TIME)
except googleapiclient.errors.HttpError:
pass # event already marked as deleted
else:
ical_event = ical_events[eid]
gcal_begin = arrow.get(gcal_event['start'].get('dateTime', gcal_event['start'].get('date')))
gcal_end = arrow.get(gcal_event['end'].get('dateTime', gcal_event['end'].get('date')))
gcal_has_location = 'location' in gcal_event
ical_has_location = ical_event.location is not None
# event name can be left unset, in which case there's no summary field
gcal_name = gcal_event.get('summary', None)
log_name = '<unnamed event>' if gcal_name is None else gcal_name
# check if the iCal event has a different: start/end time, name, location,
# or description, and if so sync the changes to the GCal event
if gcal_begin != ical_event.begin \
or gcal_end != ical_event.end \
or gcal_name != ical_event.name \
or gcal_has_location != ical_has_location \
or (gcal_has_location and gcal_event['location'] != ical_event.location) \
or gcal_event['description'] != ical_event.description:
logger.info(u'> Updating event "{}" due to date/time change...'.format(log_name))
delta = arrow.get(ical_event.end) - arrow.get(ical_event.begin)
# all-day events handled slightly differently
# TODO multi-day events?
if delta.days >= 1:
gcal_event['start'] = get_gcal_date(ical_event.begin)
gcal_event['end'] = get_gcal_date(ical_event.end)
else:
gcal_event['start'] = get_gcal_datetime(ical_event.begin, gcal_cal['timeZone'])
if ical_event.has_end:
gcal_event['end'] = get_gcal_datetime(ical_event.end, gcal_cal['timeZone'])
gcal_event['summary'] = ical_event.name
gcal_event['description'] = ical_event.description
gcal_event['source'] = {'title': 'imported from ical_to_gcal_sync.py', 'url': ICAL_FEED}
gcal_event['location'] = ical_event.location
service.events().update(calendarId=CALENDAR_ID, eventId=eid, body=gcal_event).execute()
time.sleep(API_SLEEP_TIME)
# now add any iCal events not already in the Google Calendar
logger.info('> Processing iCal events...')
for ical_event in ical_events.values():
if create_id(ical_event.uid, ical_event.begin, ical_event.end) not in gcal_event_ids:
gcal_event = {}
gcal_event['summary'] = ical_event.name
gcal_event['id'] = create_id(ical_event.uid, ical_event.begin, ical_event.end)
gcal_event['description'] = '%s (Imported from mycal.py)' % ical_event.description
gcal_event['location'] = ical_event.location
# check if no time specified in iCal, treat as all day event if so
delta = arrow.get(ical_event.end) - arrow.get(ical_event.begin)
# TODO multi-day events?
if delta.days >= 1:
gcal_event['start'] = get_gcal_date(ical_event.begin)
logger.info(u'iCal all-day event {} to be added at {}'.format(ical_event.name, ical_event.begin))
if ical_event.has_end:
gcal_event['end'] = get_gcal_date(ical_event.end)
else:
gcal_event['start'] = get_gcal_datetime(ical_event.begin, gcal_cal['timeZone'])
logger.info(u'iCal event {} to be added at {}'.format(ical_event.name, ical_event.begin))
if ical_event.has_end:
gcal_event['end'] = get_gcal_datetime(ical_event.end, gcal_cal['timeZone'])
try:
time.sleep(API_SLEEP_TIME)
service.events().insert(calendarId=CALENDAR_ID, body=gcal_event).execute()
except:
time.sleep(API_SLEEP_TIME)
service.events().update(calendarId=CALENDAR_ID, eventId=gcal_event['id'], body=gcal_event).execute()