restructures programm into multiple functions
This commit is contained in:
parent
571c5a9e0e
commit
7fd7b69f65
@ -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
|
@ -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
|
||||||
@ -34,13 +31,10 @@ def main():
|
|||||||
pickle.dump(creds, token)
|
pickle.dump(creds, token)
|
||||||
|
|
||||||
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)
|
def getCalendarEvents(service, startDate, endDate):
|
||||||
tom = (datetime.datetime.utcnow() + one_day).isoformat() + 'Z'
|
|
||||||
print('Getting todays calendar events')
|
|
||||||
|
|
||||||
page_token = None
|
page_token = None
|
||||||
calendar_ids = []
|
calendar_ids = []
|
||||||
while True:
|
while True:
|
||||||
@ -51,21 +45,36 @@ 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'])
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
BIN
token.pickle
BIN
token.pickle
Binary file not shown.
Loading…
Reference in New Issue
Block a user