-
Notifications
You must be signed in to change notification settings - Fork 6
/
GoogleapiClass.py
198 lines (166 loc) · 7.19 KB
/
GoogleapiClass.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from __future__ import print_function
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import httplib2
import os
import datetime
import pytz
import HelperClass as hc
import DBClass as db
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
class GoogleAPI(object):
def __init__(self):
# If modifying these SCOPES, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
self.SCOPES = 'https://www.googleapis.com/auth/calendar'
self.CLIENT_SECRET_FILE = '../api/client_secret.json'
self.APPLICATION_NAME = 'Google Calendar API Python Quickstart'
self.credentials = self.get_credentials()
self.http = self.credentials.authorize(httplib2.Http())
self.service = discovery.build('calendar', 'v3', http=self.http)
def get_credentials(self):
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'calendar-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(self.CLIENT_SECRET_FILE, self.SCOPES)
flow.user_agent = self.APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def createEvent(self, summary, location, start, end):
# Event Details
event = {
'summary': summary,
'location': location,
'description': 'Created by TechBot',
'start': {
'dateTime': start,
'timeZone': 'Asia/Singapore',
},
'end': {
'dateTime': end,
'timeZone': 'Asia/Singapore',
},
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'popup', 'minutes': 60}
],
},
}
event = self.service.events().insert(calendarId='primary', body=event).execute()
print('Event created: %s' % (event.get('htmlLink')))
return event.get('id')
def CreateEventIndex(self, chat_id, summary, location, desc, start_time, end_time, first_week, first_recess_week, recurrence, day, is_ignore_first_event=False):
# First Instance of the course
# first_event's start_time
first_event_start_str = first_week + 'T' + start_time # to avoid ambiguity
first_event_start_obj = datetime.datetime.strptime(first_event_start_str, '%Y-%m-%dT%H:%M:%S')
first_event_start_iso = first_event_start_obj.isoformat()
first_event_ugly_start = first_event_start_obj.strftime("%Y%m%dT%H%M%S")
# first_event's end_time
first_event_end_str = first_week + 'T' + end_time
first_event_end_obj = datetime.datetime.strptime(first_event_end_str, '%Y-%m-%dT%H:%M:%S')
first_event_end_iso = first_event_end_obj.isoformat()
# The recess week
first_recess_week_str = first_recess_week + 'T' + start_time
first_recess_week_obj = datetime.datetime.strptime(first_recess_week_str, '%Y-%m-%dT%H:%M:%S')
# Ignore recess week
ParseObject = hc.StringParseGoogleAPI(start_time)
ignore_recess_week = ParseObject.ParseDateWeek(first_recess_week_obj)
# ignore the first event
ignore_first_event = ""
# Comma Issues
if is_ignore_first_event:
if recurrence != '':
ignore_first_event = ',' + first_event_ugly_start + ','
else:
ignore_first_event = ',' + first_event_ugly_start
else:
recurrence = ',' + recurrence
final_week_obj = datetime.datetime.strptime(first_week, '%Y-%m-%d') + datetime.timedelta(days=7 * 13 + 5)
final_week_str = final_week_obj.strftime("%Y%m%d")
# Event Details
event = {
'summary': summary,
'location': location,
'description': desc,
'start': {
'dateTime': first_event_start_iso,
'timeZone': 'Asia/Singapore',
},
'end': {
'dateTime': first_event_end_iso,
'timeZone': 'Asia/Singapore',
},
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'popup', 'minutes': 60}
],
},
'recurrence': [
"EXDATE;TZID=Asia/Singapore;VALUE=DATE:" + ignore_recess_week + ignore_first_event + recurrence,
"RRULE:FREQ=WEEKLY;UNTIL=" + final_week_str + ";BYDAY=" + day
]
}
event = self.service.events().insert(calendarId='primary', body=event).execute()
print('Event created: %s' % (event.get('htmlLink')))
event_id = event['id']
print(event_id)
course_code, course_type = summary.split(' ')
db.DB().UpdateCourseCodeEventId(chat_id, course_code, event_id)
def FreeBusyQuery(self, str_date_start, str_date_end): # str_date --> yyyy-mm-dd hh:mm
# Parsing date
iso_date_start = hc.StringParseGoogleAPI(str_date_start).ParseDate()
iso_date_end = hc.StringParseGoogleAPI(str_date_end).ParseDate()
# query details
query = {
'timeMin': iso_date_start,
'timeMax': iso_date_end,
'timeZone': 'Asia/Singapore',
'items': [
{
'id': 'primary'
}
]
}
query = self.service.freebusy().query(body=query).execute()
return query
def isFree(self, query):
return len(query['calendars']['primary']['busy']) == 0
def BusyInfo(self, query):
busy = query['calendars']['primary']['busy']
# start_busy = busy[0]['start']
# end_busy = busy[0]['end']
return busy
def deleteEvent(self,InputtedeventID):
self.service.events().delete(calendarId='primary', eventId=InputtedeventID).execute()
def getUpcomingEventList(self, num_event):
"""Description: Getting upcoming events
Return: dictionary
"""
tz = pytz.timezone('Asia/Singapore')
now = datetime.datetime.now()
tz_now = tz.localize(now)
tz_now_iso = tz_now.isoformat()
print('Getting the upcomming %d events' %(num_event))
eventsResult = self.service.events().list(
calendarId='primary', timeMin=tz_now_iso, maxResults=num_event, singleEvents=True,
orderBy='startTime').execute()
events = eventsResult.get('items', [])
return events