forked from martin-riedl/CALexa
-
Notifications
You must be signed in to change notification settings - Fork 3
/
calexa.py
223 lines (184 loc) · 7.28 KB
/
calexa.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
import re
import pprint
import json
from datetime import datetime
from datetime import timedelta
import caldav
import urllib3
from caldav.elements import dav, cdav
from calendar import monthrange
from ics import Calendar
from flask import Flask
from flask_ask import Ask, statement, request
app = Flask(__name__)
ask = Ask(app, '/')
# read configuration
with open('./config.json') as json_data_file:
config = json.load(json_data_file)
# open log files
#f = open('./calexa.log', 'a+')
def log(msg):
# global f
# f.write(msg)
# f.flush()
print(msg)
def connectCalendar():
global config
client = caldav.DAVClient(config["url"], username=config["username"], password=config["password"])
principal = client.principal()
calendars = principal.calendars()
return sorted(calendars,key=lambda calendar: str(calendar.url))
# split away TRIGGER and VALARM fields, since caldav library does not always parse them correctly
def filterEventTriggers(events):
for r in events:
ev = "";
ev_data = str(r._data).splitlines()
for line in ev_data:
if not line.lstrip().startswith("TRIGGER") and ":VALARM" not in line:
ev += (line + '\n')
r._data = ev
return events
def getCalDavEvents(begin, end):
calendars = connectCalendar()
speech_text = ""
if (len(calendars) <= 0):
speech_text = " ich konnte mich leider nicht mit dem Kalender verbinden\n"
log("ERROR: " + speech_text)
else:
eventList = []
flatten = lambda l: [item for sublist in l for item in sublist]
log(" gefundene Kalender: " + str(len(calendars)) + "\n")
i = 0
for calendar in calendars:
log(" [" + str(i + 1) + "]: " + str(calendar))
results = calendar.date_search(begin, end)
log(" -> " + str(len(results)) + " Termine \n")
if len(results) > 0:
results = filterEventTriggers(results)
eventList = eventList + flatten([Calendar(event._data).events for event in results])
i = i + 1
if (len(eventList) <= 0):
speech_text = "Es sind keine Termine eingetragen"
else:
log(" returning " + str(len(eventList)) + " event(s)\n")
sortedEventList = sorted(eventList,key=lambda icsEvent: icsEvent.begin)
# pp = pprint.PrettyPrinter(indent=4)
# pp.pprint(sortedEventList)
speech_text += '<speak>\n'
speech_text += ' Es sind folgende Termine auf dem Kalender:\n'
for icsEvent in sortedEventList:
speech_text += ' <break time="1s"/>' + icsEvent.begin.humanize(locale='de') + ' ist ' + getEventName(icsEvent) + '\n'
speech_text += '</speak>'
return speech_text
#@ask.intent('GetTodayEventsIntent')
#def getTodayEvents():
# speech_text = getCalDavEvents(datetime.now(), datetime.now() + timedelta(days=1))
# print(speech_text)
# return statement(speech_text).simple_card('Kalendertermine', speech_text)
@ask.intent('GetEventsIntent', convert={ 'date': 'date', 'enddate': 'date' })
def getDateEvents(date, enddate):
log("Reading events!\n")
log(" date (from user): " + str(date) + " " + str(type(date)) + "\n")
log(" enddate (from user): " + str(enddate) + " " + str(type(enddate)) + "\n")
# in case that default "enddate" does not comply to "date",
# the enddate is set to end of the day of "date"
if date==None:
date = datetime.now()
if enddate==None or date>=enddate:
enddate = getEndDate(date, request.intent.slots.date.value)
log(" date: " + str(date) + "\n")
log(" endDate: " + str(enddate) + "\n")
speech_text = getCalDavEvents(date, enddate)
log(" text: " + speech_text + "\n")
return statement(speech_text).simple_card('Kalendertermine', speech_text)
def getEndDate(date, orig_date):
log(" orig_date: " + str(orig_date) + " " + str(type(orig_date)) + "\n")
orig_date = re.sub('X$', '0', orig_date)
if re.match('^\d{4}-W\d{2}$', orig_date):
# this week
endDate = date + timedelta(days=7)
elif re.match('\d{4}-W\d{2}-WE$', orig_date):
# this weekend
endDate = date + timedelta(days=2)
elif re.match('^\d{4}-\d{2}$', orig_date):
# this month
last_day = monthRange(date.year, date.month)
endDate = dateTime.date(date.year, date.month, last_day)
elif re.match('^\d{4}$', orig_date):
# this/next year
endDate = dateTime.date(date.year, 12, 31)
else:
endDate = datetime(date.year, date.month, date.day + 1)
return endDate
def getEventName(event):
# see: https://stackoverflow.com/que stions/40135637/error-unable-to-parse-the-provided-ssml-the-provided-text-is-not-valid-ssml
name = event.name
name = name.replace('&', ' und ')
name = name.replace('*', '')
return name
# We do have a minor problem here. There is no timezone information in the date/time objects...
# ... we assume the server's timezone, but it could be that this is wrong. So if created events are off by some hour(s)
# this is the reason. If someone wants to provide a simple PR then this would be great :-)
@ask.intent('SetEventIntent', convert={'date': 'date', 'time':'time', 'duration' : 'timedelta'})
def setEvent(date, time, duration, eventtype, location):
log("Creating event!\n");
log(" date (from user): " + str(date) + "\n")
log(" time (from user): " + str(time) + "\n")
log(" duration (from user): " + str(duration) + "\n")
log(" eventtype (from user): " + str(eventtype) + "\n")
log(" location (from user): " + str(location) + "\n")
speech_text = "Termin konnte nicht eingetragen werden!"
if eventtype==None:
eventtype='Besprechung'
if date==None:
date = datetime.today()
if duration==None:
duration = timedelta(hours=1)
d = datetime.combine(date,time)
creationDate = datetime.now().strftime("%Y%m%dT%H%M%S")
startDate = d.strftime("%Y%m%dT%H%M%S")
endDate = (d + duration).strftime("%Y%m%dT%H%M%S")
log(" startDate: " + str(startDate) + "\n")
log(" endDate: " + str(endDate) + "\n")
vcal = "BEGIN:VCALENDAR"+"\n"
vcal += "VERSION:2.0"+"\n"
vcal += "PRODID:-//Example Corp.//CalDAV Client//EN"+"\n"
vcal += "BEGIN:VEVENT"+"\n"
vcal += "UID:1234567890"+"\n"
vcal += "DTSTAMP:" + creationDate +"\n"
vcal += "DTSTART:" + startDate +"\n"
vcal += "DTEND:" + endDate +"\n"
vcal += "SUMMARY:" + eventtype + "\n"
vcal += "END:VEVENT"+"\n"
vcal += "END:VCALENDAR"
log(" entry: " + vcal + "\n")
calendars = connectCalendar()
if (len(calendars) <= 0):
speech_text = "Ich konnte mich leider nicht mit dem Kalender verbinden"
log("ERROR: " + speech_text + "\n")
else:
# This could be sooo much easier if we had something like "if (calendar.isReadOnly())"
i = 0
log(" gefundene Kalender: #" + str(len(calendars)) + "\n")
for calendar in calendars:
log(" [" + str(i + 1) + "]: " + str(calendar) + "\n")
try:
event = calendar.add_event(vcal)
speech_text = "Termin wurde eingetragen!"
# Everything worked out well and event has been entered into one calendar -> we do not have to try other calendars and therefore skip the loop
break
except Exception as te:
if (i >= len(calendars)):
speech_text = "Ich konnte in keinen Kalender schreiben"
log("ERROR: " + speech_text + "\n")
log("ERROR: " + str(te) + "\n")
pass
else:
log(" konnte nicht in Kalender schreiben: " + str(calendar) + ". Versuche nächsten Kalender...\n")
# Try using the next calendar... we will fail when the event could not be added to any calendar
i = i + 1
log(" text: " + speech_text + "\n")
return statement(speech_text).simple_card('Kalendertermine', speech_text)
#print getTodayEvents()
if __name__ == '__main__':
app.run(host="0.0.0.0", port=config["calexaPort"])