2020-03-08 22:28:45 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
import datetime
|
|
|
|
import pickle
|
|
|
|
import os.path
|
|
|
|
from googleapiclient.discovery import build
|
|
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
|
|
from google.auth.transport.requests import Request
|
|
|
|
|
|
|
|
# If modifying these scopes, delete the file token.pickle.
|
|
|
|
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
|
|
|
|
|
|
|
|
|
2020-03-09 23:54:46 +01:00
|
|
|
def calendarCredentials():
|
2020-03-08 22:28:45 +01:00
|
|
|
creds = None
|
|
|
|
# The file token.pickle stores the user's access and refresh tokens, and is
|
|
|
|
# created automatically when the authorization flow completes for the first
|
|
|
|
# time.
|
|
|
|
if os.path.exists('token.pickle'):
|
|
|
|
with open('token.pickle', 'rb') as token:
|
|
|
|
creds = pickle.load(token)
|
|
|
|
# If there are no (valid) credentials available, let the user log in.
|
|
|
|
if not creds or not creds.valid:
|
|
|
|
if creds and creds.expired and creds.refresh_token:
|
|
|
|
creds.refresh(Request())
|
|
|
|
else:
|
|
|
|
flow = InstalledAppFlow.from_client_secrets_file(
|
|
|
|
'credentials.json', SCOPES)
|
|
|
|
creds = flow.run_local_server(port=0)
|
|
|
|
# Save the credentials for the next run
|
|
|
|
with open('token.pickle', 'wb') as token:
|
|
|
|
pickle.dump(creds, token)
|
|
|
|
|
|
|
|
service = build('calendar', 'v3', credentials=creds)
|
2020-03-09 23:54:46 +01:00
|
|
|
|
|
|
|
return service
|
|
|
|
|
|
|
|
def getCalendarEvents(service, startDate, endDate):
|
2020-03-08 22:28:45 +01:00
|
|
|
page_token = None
|
|
|
|
calendar_ids = []
|
|
|
|
while True:
|
|
|
|
calendar_list = service.calendarList().list(pageToken=page_token).execute()
|
|
|
|
for calendar_list_entry in calendar_list['items']:
|
2020-03-11 00:05:34 +01:00
|
|
|
print(calendar_list_entry['colorId'])
|
2020-03-08 22:28:45 +01:00
|
|
|
calendar_ids.append(calendar_list_entry['id'])
|
|
|
|
page_token = calendar_list.get('nextPageToken')
|
|
|
|
if not page_token:
|
|
|
|
break
|
|
|
|
|
2020-03-09 23:54:46 +01:00
|
|
|
all_events = []
|
2020-03-08 22:28:45 +01:00
|
|
|
|
|
|
|
for calendar in calendar_ids:
|
2020-03-09 23:54:46 +01:00
|
|
|
events_result = service.events().list(calendarId=calendar, timeMin=startDate,
|
|
|
|
timeMax=endDate,
|
2020-03-08 22:28:45 +01:00
|
|
|
maxResults=10, singleEvents=True,
|
|
|
|
orderBy='startTime').execute()
|
2020-03-09 23:54:46 +01:00
|
|
|
all_events.append(events_result.get('items', []))
|
|
|
|
|
|
|
|
return all_events
|
|
|
|
|
|
|
|
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
|
2020-03-11 00:05:34 +01:00
|
|
|
# one_day = datetime.timedelta(days=1)
|
|
|
|
tomorrow = (now.replace(hour=23, minute=59, second=59)).isoformat() + 'Z'
|
2020-03-09 23:54:46 +01:00
|
|
|
|
|
|
|
all_events = getCalendarEvents(service, today, tomorrow)
|
2020-03-08 22:28:45 +01:00
|
|
|
|
2020-03-09 23:54:46 +01:00
|
|
|
# if not events:
|
|
|
|
# print('No upcoming events found.')
|
|
|
|
for event_list in all_events:
|
|
|
|
for event in event_list:
|
2020-03-11 00:05:34 +01:00
|
|
|
start = event['start'].get('dateTime')
|
|
|
|
color = event.get('colorId')
|
|
|
|
end = event['end'].get('dateTime')
|
|
|
|
print(start, " ", end, color, event['summary'])
|
2020-03-08 22:28:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-03-09 23:54:46 +01:00
|
|
|
main()
|