-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendarutils.py
175 lines (144 loc) · 5.43 KB
/
calendarutils.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
import os
import datetime
from datetime import date
import httplib2
import xbmcaddon
from email.mime.text import MIMEText
import utils
import oauth2client
import webbrowser
from oauth2client import client
from oauth2client import tools
from oauth2client import file
libDir = os.path.join(xbmcaddon.Addon().getAddonInfo('path'), 'resources', 'lib')
os.sys.path.insert(0, libDir)
import googleapiclient
from googleapiclient import discovery
import tzlocal
from tzlocal import get_localzone
from googleapiclient.discovery import build
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import run
class CalendarConnection(object):
def __init__(self):
credentials = self.get_credentials()
http = credentials.authorize(httplib2.Http())
self.service = discovery.build('calendar', 'v3', http=http)
self.calendars = self.getCalendars()
self.colors = self.getColors()
eventId_calendarId = {}
self.date_to_events = {}
self.calendar_control_to_calendar_name = {}
def getEvents(self, start_calendar_date, end_calendar_date):
events = []
self.eventId_calendarId = {}
self.date_to_events = {}
s = utils.formatDateTime_get_iso(start_calendar_date) + 'Z'
e = utils.formatDateTime_get_iso(end_calendar_date) + 'Z'
print str(s) + " --- " + str(e)
tmp_events=[]
# Loop over all calendars
for cal in self.calendars:
# Check if calendar is enabled
if self.is_selected(cal['summary']):
eventsResult = self.service.events().list(calendarId=cal['id'], timeMin=s, timeMax=e, maxResults=100, singleEvents=True, orderBy='startTime').execute()
tmp_events = eventsResult.get('items', [])
# Save mapping
for event in tmp_events:
# Map event id to calendar id
self.eventId_calendarId[event['id']] = cal['id']
if not utils.formatDate(event, 'start') in self.date_to_events:
self.date_to_events[utils.formatDate(event, 'start')] = []
self.date_to_events[utils.formatDate(event, 'start')].append(event)
events.extend(tmp_events)
return events
def deleteEvent(self, e):
self.service.events().delete(calendarId=self.eventId_calendarId[e['id']], eventId=e['id']).execute()
def createEvent(self, e):
event = self.service.events().insert(calendarId='primary', body=e).execute()
def updateEvent(self, e):
event = e
updated_event = self.service.events().update(calendarId=self.eventId_calendarId[event['id']], eventId=event['id'], body=event).execute()
def getColors(self):
colorsListResult = self.service.colors().get().execute()
colors = colorsListResult.get('calendar')
return colors
def getCalendars(self):
calendarListResult = self.service.calendarList().list().execute()
calendars = calendarListResult['items']
return calendars
def getCalendarColor(self, color_id):
cal_color = str(self.colors[color_id].get('background'))
while cal_color[0] == '#': cal_color = cal_color[1:]
return '0xFF'+cal_color
def is_selected(self, calendar_name):
tmp = utils.addon.getSetting(calendar_name.encode('utf-8'))
if not tmp or tmp == '':
tmp = '1'
return int(tmp)
def createNewEventBody(self, start_time, end_time):
timezone = utils.addon.getSetting('timezone')
if not timezone:
timezone = get_localzone()
event = {
'summary': '',
'location': '',
'description': '',
'start': {
'dateTime': start_time,
'timeZone': str(timezone),
},
'end': {
'dateTime': end_time,
'timeZone': str(timezone),
},
'recurrence': [
],
'attendees': [
{'email': 'yohay.kamchi@gmail.com'},
],
'reminders': {
'useDefault': False,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
],
},
}
return event #= service.events().insert(calendarId='primary', body=event).execute()
def get_credentials(self):
store = oauth2client.file.Storage(os.path.join(utils.userpath, 'calendar-quickstart'+utils.username+'.json'))
credentials = store.get()
if not credentials or credentials.invalid:
print "2"
client_id = '987683463712-se9dhv6gteobsmlpkgvqptub59fajuiq.apps.googleusercontent.com'
print "3"
client_secret = os.path.join(utils.addonpath,'client_secret.json')
print "4"
scope = 'https://www.googleapis.com/auth/calendar'
redirect_uri='urn:ietf:wg:oauth:2.0:oob'
flow = oauth2client.client.flow_from_clientsecrets(client_secret, scope, redirect_uri)
auth_uri = flow.step1_get_authorize_url()
webbrowser.open(auth_uri)
auth_code = utils.getKeyboardResults("", 'Code')
credentials = flow.step2_exchange(auth_code)
store.put(credentials)
return credentials
def get_credentials_old(self):
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
store = oauth2client.file.Storage(os.path.join(utils.userpath, 'calendar-quickstart'+utils.username+'.json'))
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(os.path.join(utils.addonpath,'client_secret.json'), 'https://www.googleapis.com/auth/calendar')
flow.user_agent = 'Google Calendar API Quickstart'
flags = tools.argparser.parse_args(args=[])
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatability with Python 2.6
credentials = tools.run(flow, store)
return credentials