-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendarInterface.py
78 lines (57 loc) · 2.63 KB
/
CalendarInterface.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gflags
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from CalendarHelperFunctions import duration, calsBurned
from person import Person
def getEvents(startTime,endTime):
FLAGS = gflags.FLAGS
# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for native
# applications
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console
FLOW = OAuth2WebServerFlow(
client_id='65056611106.apps.googleusercontent.com',
client_secret='qMzPW-5wrmCUn0D6E-izxq03',
scope='https://www.googleapis.com/auth/calendar',
user_agent='PrintCalendar/0.1')
# To disable the local server feature, uncomment the following line:
# FLAGS.auth_local_webserver = False
# If the Credentials don't exist or are invalid, run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
credentials = run(FLOW, storage)
#print credentials.to_json()
# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)
# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
service = build(serviceName='calendar', version='v3', http=http,
developerKey='AIzaSyDFNRC0qG6KBQt1t7qK3WZTHuAK2R8KQ9s')
calendar = service.calendars().get(calendarId='primary').execute()
eventList=[]
page_token = None
while True:
events = service.events().list(calendarId='primary', pageToken=page_token,timeMin=startTime,timeMax=endTime).execute()
if events['items']:
for event in events['items']:
start= event['start']['dateTime']
end = event['end']['dateTime']
delta = duration(start,end)
eventList.append((event['summary'],delta))
#print event['summary'], delta
page_token = events.get('nextPageToken')
if not page_token:
break
return eventList