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