From 7fd7b69f6515961c4433112631653e0610d116b6 Mon Sep 17 00:00:00 2001 From: raphael Date: Mon, 9 Mar 2020 23:54:46 +0100 Subject: [PATCH] restructures programm into multiple functions --- README.md | 5 +++++ quickstart.py | 47 ++++++++++++++++++++++++++++------------------- token.pickle | Bin 752 -> 752 bytes 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index e69de29..bc48ec4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/quickstart.py b/quickstart.py index bf1a429..54c1529 100644 --- a/quickstart.py +++ b/quickstart.py @@ -10,10 +10,7 @@ from google.auth.transport.requests import Request SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] -def main(): - """Shows basic usage of the Google Calendar API. - Prints the start and name of the next 10 events on the user's calendar. - """ +def calendarCredentials(): 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 @@ -34,13 +31,10 @@ def main(): pickle.dump(creds, token) service = build('calendar', 'v3', credentials=creds) - - # Call the Calendar API - 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') - + + return service + +def getCalendarEvents(service, startDate, endDate): page_token = None calendar_ids = [] while True: @@ -51,21 +45,36 @@ def main(): if not page_token: break + all_events = [] for calendar in calendar_ids: - print(calendar) - events_result = service.events().list(calendarId=calendar, timeMin=now, - timeMax=tom, + events_result = service.events().list(calendarId=calendar, timeMin=startDate, + timeMax=endDate, maxResults=10, singleEvents=True, orderBy='startTime').execute() - events = events_result.get('items', []) + all_events.append(events_result.get('items', [])) - if not events: - print('No upcoming events found.') - for event in events: + 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 + 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')) print(start, event['summary']) if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/token.pickle b/token.pickle index b1ca644c74b3dcd189ef87c8cd88ba4c519155d9..84c4373e592cf5b4ed7bbc0033010e45d00f5841 100644 GIT binary patch delta 182 zcmeys`hj&qV12l!Nm8gspt*C9X;pGWM6!vge?@4DL6l3XNq~V{R8f(sPgz=Wg?nCF zQhZrnU|FhfRY_o>nV+{wVX~8JzNLSdUr}jVxM`|!ra@s+c2Y@}hpBH;MrnC)erQ!m zo?)@CkB4`5aBzTAii<(1SE*ZJL7-`sZb_I|Sc#Xho4iGyUsZ}>REDobc3Me=V{w|L ib3s+Uhm)(Wp}VK0+r;;p?407tT%7Y4ZkA`f$Or(m-aYvM delta 182 zcmeys`hj&qV0}bZh)bTEVUdYrkYQ+4ez9SWcYZ)AT4J7auA!+X?jtq zVOBv{Ricq=cuKKjnr~21Zb`C*r*TT8w|iElvqxrFw!CS9iHCWWQ;tDazN1rOPFYfh he~<-GB*((PI4j6};(JYY4t_0l&d=GK