From 0c2dec4441754e18bd3ba95b6aa0304b7e6e3fba Mon Sep 17 00:00:00 2001 From: raphael Date: Thu, 12 Mar 2020 00:08:30 +0100 Subject: [PATCH] adds event structure and saves color information of calendar over to event --- quickstart.py | 55 +++++++++++++++++++++++++++++++++----------------- token.pickle | Bin 752 -> 752 bytes 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/quickstart.py b/quickstart.py index bd95daf..d4a28e8 100644 --- a/quickstart.py +++ b/quickstart.py @@ -9,6 +9,17 @@ from google.auth.transport.requests import Request # If modifying these scopes, delete the file token.pickle. 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(): creds = None @@ -36,24 +47,33 @@ def calendarCredentials(): def getCalendarEvents(service, startDate, endDate): page_token = None - calendar_ids = [] + calendars = [] while True: calendar_list = service.calendarList().list(pageToken=page_token).execute() for calendar_list_entry in calendar_list['items']: - print(calendar_list_entry['colorId']) - calendar_ids.append(calendar_list_entry['id']) + calendars.append(Calendar(calendar_list_entry['id'], calendar_list_entry['colorId'])) page_token = calendar_list.get('nextPageToken') if not page_token: break all_events = [] - for calendar in calendar_ids: - events_result = service.events().list(calendarId=calendar, timeMin=startDate, + for calendar in calendars: + events_result = service.events().list(calendarId=calendar.calendarId, timeMin=startDate, timeMax=endDate, maxResults=10, singleEvents=True, - orderBy='startTime').execute() - all_events.append(events_result.get('items', [])) + orderBy='startTime').execute() + + 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 @@ -61,22 +81,21 @@ def main(): service = calendarCredentials() # Call the Calendar API - now = datetime.datetime.now() - now = now.replace(hour=00, minute=00) - today = now.isoformat() + 'Z' # 'Z' indicates UTC time + now = datetime.datetime.now(datetime.timezone.utc).astimezone() + now = now.replace(hour=0, minute=0) + today = now.isoformat() # + '+01:00' # 'Z' indicates UTC time + print("today: ") + print(today) # 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) # if not events: # print('No upcoming events found.') - for event_list in all_events: - for event in event_list: - start = event['start'].get('dateTime') - color = event.get('colorId') - end = event['end'].get('dateTime') - print(start, " ", end, color, event['summary']) + for event in all_events: + print(event.name + ": " + event.start + ", " + event.color) if __name__ == '__main__': diff --git a/token.pickle b/token.pickle index 76117d905f5448af93ebd6d4f38976a691bc7afe..9a6227501b67a9e424ec74b472a79000c4a82343 100644 GIT binary patch delta 181 zcmeys`hj&qP`!z6x=%qsv6*FNu4!11Z&XQDK)!pndw^$AvYVf;pOdSBX|bDYVo185 zrFp)qX^vB}k!wU$s<%&oOJI^`ly`D`RA@zFhCy+;L9lmrv4>|$rBjBXm#Jq)QF^?t zPiU!IhGCSeN12JMYgu`se?efnrLV6?WMxK0pj$8PWKwFFub+Woa!N#KP?%eAVwsyqhL@33hIv_#pRZA3x_eT*ZiY#^ zMR7&2ZisPdh+A?{QHq6oaf+pPXsC(1hlzz@Xo-hKX-2A{(h?8M*fJ>s6p`3A)u5pP`dPGXFZdhqzU|B$D ha=uf!nTdOpLApWZ#P^!)T;k?p?EGn)