generated from streamlit/streamlit-hello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendarDataRetriever.py
35 lines (30 loc) · 1.44 KB
/
calendarDataRetriever.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import datetime
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def getWeekLocations(creds):
try:
service = build('calendar', 'v3', credentials=creds)
# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
later = (datetime.datetime.utcnow() + datetime.timedelta(days=7)).isoformat() + 'Z'
print('Getting all events from now to 7 days later...')
events_result = service.events().list(calendarId='primary', timeMin=now,
timeMax=later, singleEvents=True,
orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
return
# Prints the locations of the events for the week
locations = []
startDates = []
for event in events:
startTime = event['start'].get('dateTime', event['start'].get('date'))
startDate = event['start']['dateTime'][:10]
print(event)
# print(start, event['location'])
locations.append((event['summary'], startTime, event['location'])) # (eventName, startTime, location)
startDates.append(startDate)
return locations, startDates
except HttpError as error:
print('An error occurred: %s' % error)