adds event structure and saves color information of calendar over to event
This commit is contained in:
parent
cfa2540c90
commit
0c2dec4441
@ -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', []))
|
||||
|
||||
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__':
|
||||
|
BIN
token.pickle
BIN
token.pickle
Binary file not shown.
Loading…
Reference in New Issue
Block a user