multiple calendar support
This commit is contained in:
+14
-11
@@ -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,19 +27,20 @@ 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 = []
|
||||
for calendar in self.calendars:
|
||||
events_result = self.service.events().list(
|
||||
calendarId='loskorep@productiveedge.com',
|
||||
calendarId=calendar,
|
||||
timeMin=now.isoformat() + 'Z',
|
||||
maxResults=10,
|
||||
singleEvents=True,
|
||||
@@ -46,10 +50,9 @@ class CalGrab(object):
|
||||
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.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:
|
||||
|
||||
Reference in New Issue
Block a user