multiple calendar support

This commit is contained in:
Lucas Oskorep
2022-07-17 21:51:59 -04:00
parent ea37a40ef7
commit 9da62a9004
+24 -21
View File
@@ -10,9 +10,12 @@ from event import Event
class CalGrab(object):
def __init__(self, auth_file, calendars=[], callbacks=[]):
def __init__(self, auth_file, calendars=None, callbacks=None):
if callbacks is None:
callbacks = []
if calendars is None:
calendars = []
self.calendars = calendars
# TODO: Add multiple calendar support
self.callbacks = callbacks
self.creds = service_account.Credentials.from_service_account_file(
auth_file, scopes=['https://www.googleapis.com/auth/calendar.readonly'])
@@ -24,32 +27,32 @@ class CalGrab(object):
def update_at_interval(self, frequency, time_to_update=-1):
"""
:param frequency: Amount of time to wait between updates in seconds
:param time_to_update: normally set to -1 aka no limit. Time in minutes to continually ping the api for.
:return: None
:param frequency:
:param time_to_update:
:return:
"""
start = None
while True:
try:
now = datetime.utcnow() # 'Z' indicates UTC time
now = datetime.utcnow()
if start == None:
start = now
events_result = self.service.events().list(
calendarId='loskorep@productiveedge.com',
timeMin=now.isoformat() + 'Z',
maxResults=10,
singleEvents=True,
orderBy='startTime'
).execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
return
events = [i for i in [Event.get_from_gcal_api_json(json) for json in events] if i is not None]
events = []
for calendar in self.calendars:
events_result = self.service.events().list(
calendarId=calendar,
timeMin=now.isoformat() + 'Z',
maxResults=10,
singleEvents=True,
orderBy='startTime'
).execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
return
events.extend([i for i in [Event.get_from_gcal_api_json(json) for json in events] if i is not None])
print(events)
events = sorted(events, key=lambda event: event.start_time)
for callback in self.callbacks:
callback(events)
if time_to_update > 0 and (now - start).total_seconds() > time_to_update: