-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGoogleCalendar.py
203 lines (160 loc) · 7.24 KB
/
GoogleCalendar.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
199
200
201
202
203
# -*- coding: utf-8 -*-
import os
import json
import datetime
from googleapiclient.http import BatchHttpRequest
import httplib2
from googleapiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
import rfc3339
class GoogleCalendar:
_scope = 'https://www.googleapis.com/auth/calendar'
def __init__(self, key_p12_path, service_account_mail):
if not os.path.exists(key_p12_path):
raise AttributeError('p12 key path is not vaild')
f = file(key_p12_path, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(
service_account_mail,
key,
scope=self._scope)
http = httplib2.Http()
self._http = credentials.authorize(http)
self._service = build('calendar', 'v3', http=http)
# batch is limited up to 1000 queries in one
self._batch = BatchHttpRequest()
self._calendar_id = None
self._event_json_path = None
self._event_name_to_id_dict = None
self._service_mail = service_account_mail
def pushChanges(self):
self._batch.execute(http=self._http)
def eventAdded(self, request_id, response, exception):
if exception is None:
self._event_name_to_id_dict[request_id] = response['id']
else:
pass # TODO add log message
def eventModified(self, request_id, response, exception):
if exception is None:
self._event_name_to_id_dict[request_id] = response['id']
else:
pass # TODO add log message
def _convertDateTime(self, date, time):
return rfc3339.rfc3339(datetime.datetime(int(date[:4]),
int(date[5:7]),
int(date[8:10]),
int(time[:2]),
int(time[3:5])))
def _eventStartDateTime(self, event_details):
return self._convertDateTime(event_details[u'Data rozpoczęcia'.encode('windows-1250')],
event_details[u'Czas rozpoczęcia'.encode('windows-1250')])
def _eventEndDateTime(self, event_details):
return self._convertDateTime(event_details[u'Data zakończenia'.encode('windows-1250')],
event_details[u'Czas zakończenia'.encode('windows-1250')])
def _eventLocation(self, event_details):
return event_details[u'Lokalizacja'.encode('windows-1250')]
def addScheduleEvents(self, event_names, event_details):
for ev_name in event_names:
ev_det = event_details[ev_name]
event = {
'summary': ev_name.decode('windows-1250'),
'start': {
'dateTime': self._eventStartDateTime(ev_det)
},
'end': {
'dateTime': self._eventEndDateTime(ev_det)
},
}
loc = self._eventLocation(ev_det)
if loc is not None:
event['location'] = loc
if '(w)' in ev_name:
col = '10'
elif '(L)' in ev_name:
col = '4'
elif '(p)' in ev_name:
col = '6'
elif u'(ć)'.encode('windows-1250') in ev_name:
col = '11'
else:
col = '1'
event['colorId'] = col
self._batch.add(self._service.events().insert(calendarId=self._calendar_id,
body=event), callback=self.eventAdded, request_id=ev_name)
def modifyScheduleEvents(self, event_names, new_event_details):
for ev_name in event_names:
ev_det = new_event_details[ev_name]
patch = {
'start': {
'dateTime': self._eventStartDateTime(ev_det)
},
'end': {
'dateTime': self._eventEndDateTime(ev_det)
},
}
loc = self._eventLocation(ev_det)
if loc is not None:
patch['location'] = loc
self._batch.add(self._service.events().patch(calendarId=self._calendar_id,
eventId=self._event_name_to_id_dict[ev_name.decode('windows-1250')],
body=patch), callback=self.eventModified, request_id=ev_name)
def removeScheduleEvents(self, event_names):
#TODO usuwanie z jsona lub dawanie gdzieś idziej informacji o usunięciu
for ev_name in event_names:
patch = {
'status': 'cancelled'
}
self._batch.add(self._service.events().patch(calendarId=self._calendar_id,
eventId=self._event_name_to_id_dict[ev_name.decode('windows-1250')],
body=patch), callback=self.eventModified, request_id=ev_name)
def createCalendar(self, name):
calendar = {
'summary': name,
'timeZone': 'Europe/Warsaw'
}
created_calendar = self._service.calendars().insert(body=calendar).execute()
return created_calendar['id']
def setCalendar(self, calendar_id, event_dict_json_path):
self._calendar_id = calendar_id
self._event_json_path = event_dict_json_path
self._event_name_to_id_dict = json.loads(open(event_dict_json_path).read(), encoding='windows-1250') if os.path.exists(event_dict_json_path) else {}
def shareCalendarWithGroup(self, email):
rule = {
'scope': {
'type': 'group',
'value': email,
},
'role': 'reader'
}
created_rule = self._service.acl().insert(calendarId=self._calendar_id, body=rule).execute()
return created_rule['id']
def shareCalendarWithOwner(self, email):
rule = {
'scope': {
'type': 'user',
'value': email,
},
'role': 'owner'
}
created_rule = self._service.acl().insert(calendarId=self._calendar_id, body=rule).execute()
return created_rule['id']
def deletePrivilege(self, acl_id):
self._service.acl().delete(calendarId=self._calendar_id, ruleId=acl_id).execute()
def clearAllDataInCalendar(self):
self._service.calendars().clear(calendarId=self._calendar_id).execute()
def removeCalendar(self):
self._service.calendarList().delete(calendarId=self._calendar_id).execute()
def removeAllCalendars(self):
page_token = None
while True:
calendar_list = self._service.calendarList().list(pageToken=page_token).execute()
for calendar_list_entry in calendar_list['items']:
self._service.calendars().delete(calendarId=calendar_list_entry['id']).execute()
page_token = calendar_list.get('nextPageToken')
if not page_token:
break
def end(self):
json_file = open(self._event_json_path, mode='w')
json_file.write(json.dumps(self._event_name_to_id_dict, encoding='windows-1250'))
json_file.close()