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): 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 self.calendars = calendars
# TODO: Add multiple calendar support
self.callbacks = callbacks self.callbacks = callbacks
self.creds = service_account.Credentials.from_service_account_file( self.creds = service_account.Credentials.from_service_account_file(
auth_file, scopes=['https://www.googleapis.com/auth/calendar.readonly']) 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): def update_at_interval(self, frequency, time_to_update=-1):
""" """
:param frequency: Amount of time to wait between updates in seconds :param frequency:
:param time_to_update: normally set to -1 aka no limit. Time in minutes to continually ping the api for. :param time_to_update:
:return: None :return:
""" """
start = None start = None
while True: while True:
try: try:
now = datetime.utcnow() # 'Z' indicates UTC time now = datetime.utcnow()
if start == None: if start == None:
start = now start = now
events_result = self.service.events().list( events = []
calendarId='loskorep@productiveedge.com', for calendar in self.calendars:
timeMin=now.isoformat() + 'Z', events_result = self.service.events().list(
maxResults=10, calendarId=calendar,
singleEvents=True, timeMin=now.isoformat() + 'Z',
orderBy='startTime' maxResults=10,
).execute() singleEvents=True,
events = events_result.get('items', []) orderBy='startTime'
if not events: ).execute()
print('No upcoming events found.') events = events_result.get('items', [])
return if not events:
print('No upcoming events found.')
events = [i for i in [Event.get_from_gcal_api_json(json) for json in events] if i is not None] 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) print(events)
events = sorted(events, key=lambda event: event.start_time)
for callback in self.callbacks: for callback in self.callbacks:
callback(events) callback(events)
if time_to_update > 0 and (now - start).total_seconds() > time_to_update: if time_to_update > 0 and (now - start).total_seconds() > time_to_update: