-
Notifications
You must be signed in to change notification settings - Fork 12
/
__init__.py
385 lines (326 loc) · 13.6 KB
/
__init__.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import httplib2
import sys
from datetime import datetime, timedelta, timezone
from adapt.intent import IntentBuilder
from googleapiclient import discovery
from requests import HTTPError
from mycroft import MycroftSkill, intent_file_handler, intent_handler
from mycroft.util.parse import extract_datetime
from mycroft.util.time import now_local
from .mycroft_token_cred import MycroftTokenCredentials
UTC_TZ = u'+00:00'
def parse_google_datetime(input):
"""Convert a datetime string from google calendar to datetime."""
# Remove decimals
work_string = input.split('.')[0]
# Remove Zulu indicator from the end of string if needed
if work_string[-1] == 'Z':
work_string = work_string[:-1]
# Convert to datetime
return datetime.strptime(work_string, '%Y-%m-%dT%H:%M:%S')
def zulu_time(dt):
"""Convert datetime object to Zulu time ISO string."""
ret = dt.astimezone(timezone.utc)
ret = ret.replace(tzinfo=None)
return ret.isoformat() + 'Z'
def nice_time(dt, lang="en-us", speech=True, use_24hour=False,
use_ampm=False):
"""
Format a time to a comfortable human format
For example, generate 'five thirty' for speech or '5:30' for
text display.
Args:
dt (datetime): date to format (assumes already in local timezone)
lang (str): code for the language to use
speech (bool): format for speech (default/True) or display (False)=Fal
use_24hour (bool): output in 24-hour/military or 12-hour format
use_ampm (bool): include the am/pm for 12-hour format
Returns:
(str): The formatted time string
"""
if use_24hour:
# e.g. "03:01" or "14:22"
string = dt.strftime("%H:%M")
else:
if use_ampm:
# e.g. "3:01 AM" or "2:22 PM"
string = dt.strftime("%I:%M %p")
else:
# e.g. "3:01" or "2:22"
string = dt.strftime("%I:%M")
if string[0] == '0':
string = string[1:] # strip leading zeros
return string
if not speech:
return string
# Generate a speakable version of the time
if use_24hour:
speak = ""
# Either "0 8 hundred" or "13 hundred"
if string[0] == '0':
if string[1] == '0':
speak = "0 0"
else:
speak = "0 " + string[1]
else:
speak += string[0:2]
if string[3] == '0':
if string[4] == '0':
# Ignore the 00 in, for example, 13:00
speak += " oclock" # TODO: Localize
else:
speak += " o " + string[4] # TODO: Localize
else:
if string[0] == '0':
speak += " " + string[3:5]
else:
# TODO: convert "23" to "twenty three" in helper method
# Mimic is speaking "23 34" as "two three 43" :(
# but it does say "2343" correctly. Not ideal for general
# TTS but works for the moment.
speak += ":" + string[3:5]
return speak
else:
if lang.startswith("en"):
if dt.hour == 0 and dt.minute == 0:
return "midnight" # TODO: localize
if dt.hour == 12 and dt.minute == 0:
return "noon" # TODO: localize
# TODO: "half past 3", "a quarter of 4" and other idiomatic times
# lazy for now, let TTS handle speaking "03:22 PM" and such
return string
def is_today(d):
return d.date() == datetime.today().date()
def is_tomorrow(d):
return d.date() == datetime.today().date() + timedelta(days=1)
def is_wholeday_event(e):
return 'dateTime' not in e['start']
def remove_tz(string):
return string[:-6]
def get_date_time(event):
start = event['start'].get('dateTime')
return datetime.strptime(remove_tz(start), '%Y-%m-%dT%H:%M:%S')
class GoogleCalendarSkill(MycroftSkill):
def __init__(self):
super(GoogleCalendarSkill, self).__init__('Google Calendar')
@property
def use_24hour(self):
return self.config_core.get('time_format') == 'full'
def __calendar_connect(self, msg=None):
argv = sys.argv
sys.argv = []
try:
# Get token for this skill (id 4)
self.credentials = MycroftTokenCredentials(4)
self.log.info('Credentials: {}'.format(self.credentials))
http = self.credentials.authorize(httplib2.Http())
self.service = discovery.build('calendar', 'v3', http=http)
sys.argv = argv
self.register_intents()
self.cancel_scheduled_event('calendar_connect')
except HTTPError:
self.log.info('No Credentials available')
pass
def register_intents(self):
intent = IntentBuilder('GetNextAppointmentIntent')\
.require('NextKeyword')\
.one_of('AppointmentKeyword', 'ScheduleKeyword')\
.build()
self.register_intent(intent, self.get_next)
intent = IntentBuilder('GetDaysAppointmentsIntent')\
.require('QueryKeyword')\
.one_of('AppointmentKeyword', 'ScheduleKeyword')\
.build()
self.register_intent(intent, self.get_day)
intent = IntentBuilder('GetFirstAppointmentIntent')\
.one_of('AppointmentKeyword', 'ScheduleKeyword')\
.require('FirstKeyword')\
.build()
self.register_intent(intent, self.get_first)
def initialize(self):
self.schedule_event(self.__calendar_connect, datetime.now(),
name='calendar_connect')
def get_selected_calendars_ids(self):
"""
Returns an array of ids of all calendars which are selected in the
Calendar UI.
"""
calendar_list_results = self.service.calendarList().list().execute()
calendar_list = []
for result in calendar_list_results.get('items', []):
if result.get('selected', False):
calendar_list.append(result.get('id'))
return calendar_list
def get_interval_all_calendars(self, start, stop=None, max_results=None):
"""Searches all selected calendars for events in the provided interval.
The result is returned sorted by start time. Wholeday events are listed
last.
Returns:
(event[]): All events sorted by start time.
"""
calendar_list = self.get_selected_calendars_ids()
all_events = []
all_wholeday_events = []
# Collect all events
for calendar_id in calendar_list:
events_result = self.service.events().list(
calendarId=calendar_id, timeMin=start, timeMax=stop,
singleEvents=True, orderBy='startTime',
maxResults=max_results).execute()
events = events_result.get('items', [])
if events:
for event in events:
if not is_wholeday_event(event):
all_events.append(event)
else:
all_wholeday_events.append(event)
# Sort events by start time
all_events.sort(key=get_date_time)
all_events.extend(all_wholeday_events)
return all_events
def get_next(self, msg=None):
now = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
events = self.get_interval_all_calendars(now, max_results=4)
if not events:
self.speak_dialog('NoNextAppointments')
else:
event = events[0]
self.log.debug(event)
if not is_wholeday_event(event):
start = event['start'].get('dateTime')
d = datetime.strptime(remove_tz(start), '%Y-%m-%dT%H:%M:%S')
starttime = nice_time(d, self.lang, True, self.use_24hour,
True)
startdate = d.strftime('%-d %B')
else:
start = event['start']['date']
d = datetime.strptime(start, '%Y-%m-%d')
startdate = d.strftime('%-d %B')
starttime = None
# Speak result
if starttime is None:
if d.date() == datetime.today().date():
data = {'appointment': event['summary']}
self.speak_dialog('NextAppointmentWholeToday', data)
elif is_tomorrow(d):
data = {'appointment': event['summary']}
self.speak_dialog('NextAppointmentWholeTomorrow', data)
else:
data = {'appointment': event['summary'],
'date': startdate}
self.speak_dialog('NextAppointmentWholeDay', data)
elif d.date() == datetime.today().date():
data = {'appointment': event['summary'],
'time': starttime}
self.speak_dialog('NextAppointment', data)
elif is_tomorrow(d):
data = {'appointment': event['summary'],
'time': starttime}
self.speak_dialog('NextAppointmentTomorrow', data)
else:
data = {'appointment': event['summary'],
'time': starttime,
'date': startdate}
self.speak_dialog('NextAppointmentDate', data)
def speak_interval(self, start, stop, max_results=None):
events = self.get_interval_all_calendars(start, stop, max_results)
if not events:
self.log.debug(start)
d = parse_google_datetime(start)
if is_today(d):
self.speak_dialog('NoAppointmentsToday')
elif is_tomorrow(d):
self.speak_dialog('NoAppointmentsTomorrow')
else:
self.speak_dialog('NoAppointments')
else:
for e in events:
if is_wholeday_event(e):
data = {'appointment': e['summary']}
self.speak_dialog('WholedayAppointment', data)
else:
start = e['start'].get('dateTime', e['start'].get('date'))
d = datetime.strptime(remove_tz(start),
'%Y-%m-%dT%H:%M:%S')
starttime = nice_time(d, self.lang, True, self.use_24hour,
True)
if is_today(d) or is_tomorrow(d) or True:
data = {'appointment': e['summary'],
'time': starttime}
self.speak_dialog('NextAppointment', data)
def speak_events_for_date(self, date):
self.log.info(f'dt: {date}')
date = date.replace(hour=0, minute=0, second=1)
date_start = zulu_time(date)
date_end = zulu_time(date.replace(hour=23, minute=59, second=59))
self.speak_interval(date_start, date_end)
def get_day(self, msg=None):
extracted_date = extract_datetime(msg.data['utterance'])
if extracted_date:
self.speak_events_for_date(extracted_date[0])
else:
self.get_events_for_date(now_local())
return
def get_first(self, msg=None):
d = extract_datetime(msg.data['utterance'])[0]
d = d.replace(hour=0, minute=0, second=1, tzinfo=None)
d_end = d.replace(hour=23, minute=59, second=59, tzinfo=None)
d = d.isoformat() + 'Z'
d_end = d_end.isoformat() + 'Z'
self.speak_interval(d, d_end, max_results=1)
@intent_handler('what.is.left.today.intent')
def get_left_today(self):
d = datetime.utcnow()
d_end = d.replace(hour=23, minute=59, second=59, tzinfo=None)
d = d.isoformat() + 'Z'
d_end = d_end.isoformat() + 'Z'
self.speak_interval(d, d_end)
@property
def utc_offset(self):
return timedelta(seconds=self.location['timezone']['offset'] / 1000)
@intent_file_handler('Schedule.intent')
def add_new(self, message=None):
title = self.get_response('whatsTheNewEvent')
start = self.get_response('whenDoesItStart')
end = self.get_response('whenDoesItEnd')
if title and start and end:
st = extract_datetime(start)
et = extract_datetime(end)
if st and et:
st = st[0] - self.utc_offset
et = et[0] - self.utc_offset
self.add_calendar_event(title, start_time=st, end_time=et)
@intent_file_handler('ScheduleAt.intent')
def add_new_quick(self, msg=None):
title = msg.data.get('appointmenttitle', None)
if title is None:
self.log.debug("NO TITLE")
return
st = extract_datetime(msg.data['utterance'])[0] # start time
# convert to UTC
st -= timedelta(seconds=self.location['timezone']['offset'] / 1000)
et = st + timedelta(hours=1)
self.add_calendar_event(title, st, et)
def add_calendar_event(self, title, start_time, end_time, summary=None):
start_time = start_time.strftime('%Y-%m-%dT%H:%M:00')
stop_time = end_time.strftime('%Y-%m-%dT%H:%M:00')
stop_time += UTC_TZ
event = {}
event['summary'] = title
event['start'] = {
'dateTime': start_time,
'timeZone': 'UTC'
}
event['end'] = {
'dateTime': stop_time,
'timeZone': 'UTC'
}
data = {'appointment': title}
try:
self.service.events()\
.insert(calendarId='primary', body=event).execute()
self.speak_dialog('AddSucceeded', data)
except Exception:
self.speak_dialog('AddFailed', data)
def create_skill():
return GoogleCalendarSkill()