restructures programm into multiple functions

This commit is contained in:
Raphael Maenle 2020-03-09 23:54:46 +01:00
parent 571c5a9e0e
commit 7fd7b69f65
3 changed files with 33 additions and 19 deletions

View File

@ -0,0 +1,5 @@
### Requirements
from [this google tutorial](https://developers.google.com/calendar/quickstart/python)
to run the scripts in this repository, please install
pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

View File

@ -10,10 +10,7 @@ from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
def main(): def calendarCredentials():
"""Shows basic usage of the Google Calendar API.
Prints the start and name of the next 10 events on the user's calendar.
"""
creds = None creds = None
# The file token.pickle stores the user's access and refresh tokens, and is # The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first # created automatically when the authorization flow completes for the first
@ -35,12 +32,9 @@ def main():
service = build('calendar', 'v3', credentials=creds) service = build('calendar', 'v3', credentials=creds)
# Call the Calendar API return service
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
one_day = datetime.timedelta(days=1)
tom = (datetime.datetime.utcnow() + one_day).isoformat() + 'Z'
print('Getting todays calendar events')
def getCalendarEvents(service, startDate, endDate):
page_token = None page_token = None
calendar_ids = [] calendar_ids = []
while True: while True:
@ -51,18 +45,33 @@ def main():
if not page_token: if not page_token:
break break
all_events = []
for calendar in calendar_ids: for calendar in calendar_ids:
print(calendar) events_result = service.events().list(calendarId=calendar, timeMin=startDate,
events_result = service.events().list(calendarId=calendar, timeMin=now, timeMax=endDate,
timeMax=tom,
maxResults=10, singleEvents=True, maxResults=10, singleEvents=True,
orderBy='startTime').execute() orderBy='startTime').execute()
events = events_result.get('items', []) all_events.append(events_result.get('items', []))
if not events: return all_events
print('No upcoming events found.')
for event in 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
one_day = datetime.timedelta(days=1)
tomorrow = (now + one_day).isoformat() + 'Z'
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', event['start'].get('date')) start = event['start'].get('dateTime', event['start'].get('date'))
print(start, event['summary']) print(start, event['summary'])

Binary file not shown.