adds event structure and saves color information of calendar over to event

This commit is contained in:
Raphael Maenle 2020-03-12 00:08:30 +01:00
parent cfa2540c90
commit 0c2dec4441
2 changed files with 37 additions and 18 deletions

View File

@ -9,6 +9,17 @@ from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle. # If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
class Event:
def __init__(self, name_, color_, start_, end_):
self.name = name_
self.color = color_
self.start = start_
self.end = end_
class Calendar:
def __init__(self, calendarId_, color_):
self.calendarId = calendarId_
self.color = color_
def calendarCredentials(): def calendarCredentials():
creds = None creds = None
@ -36,24 +47,33 @@ def calendarCredentials():
def getCalendarEvents(service, startDate, endDate): def getCalendarEvents(service, startDate, endDate):
page_token = None page_token = None
calendar_ids = [] calendars = []
while True: while True:
calendar_list = service.calendarList().list(pageToken=page_token).execute() calendar_list = service.calendarList().list(pageToken=page_token).execute()
for calendar_list_entry in calendar_list['items']: for calendar_list_entry in calendar_list['items']:
print(calendar_list_entry['colorId']) calendars.append(Calendar(calendar_list_entry['id'], calendar_list_entry['colorId']))
calendar_ids.append(calendar_list_entry['id'])
page_token = calendar_list.get('nextPageToken') page_token = calendar_list.get('nextPageToken')
if not page_token: if not page_token:
break break
all_events = [] all_events = []
for calendar in calendar_ids: for calendar in calendars:
events_result = service.events().list(calendarId=calendar, timeMin=startDate, events_result = service.events().list(calendarId=calendar.calendarId, timeMin=startDate,
timeMax=endDate, timeMax=endDate,
maxResults=10, singleEvents=True, maxResults=10, singleEvents=True,
orderBy='startTime').execute() orderBy='startTime').execute()
all_events.append(events_result.get('items', []))
for event in events_result.get('items', []):
name = event['summary']
start = event['start'].get('dateTime')
end = event['end'].get('dateTime')
color = event.get('colorId')
if color == None:
color = calendar.color
all_events.append(Event(name, color, start, end))
return all_events return all_events
@ -61,22 +81,21 @@ def main():
service = calendarCredentials() service = calendarCredentials()
# Call the Calendar API # Call the Calendar API
now = datetime.datetime.now() now = datetime.datetime.now(datetime.timezone.utc).astimezone()
now = now.replace(hour=00, minute=00) now = now.replace(hour=0, minute=0)
today = now.isoformat() + 'Z' # 'Z' indicates UTC time today = now.isoformat() # + '+01:00' # 'Z' indicates UTC time
print("today: ")
print(today)
# one_day = datetime.timedelta(days=1) # one_day = datetime.timedelta(days=1)
tomorrow = (now.replace(hour=23, minute=59, second=59)).isoformat() + 'Z' tomorrow = (now.replace(hour=23, minute=0, second=1)).isoformat() # + '+01:00'
print("tomorrow: ")
print(tomorrow)
all_events = getCalendarEvents(service, today, tomorrow) all_events = getCalendarEvents(service, today, tomorrow)
# if not events: # if not events:
# print('No upcoming events found.') # print('No upcoming events found.')
for event_list in all_events: for event in all_events:
for event in event_list: print(event.name + ": " + event.start + ", " + event.color)
start = event['start'].get('dateTime')
color = event.get('colorId')
end = event['end'].get('dateTime')
print(start, " ", end, color, event['summary'])
if __name__ == '__main__': if __name__ == '__main__':

Binary file not shown.