-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
172 lines (129 loc) · 5.61 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
from mycroft import MycroftSkill, intent_file_handler
class ShoppingList(MycroftSkill):
def __init__(self):
MycroftSkill.__init__(self)
self.todoist_api = None
self.parent_project_id = None
def initialize(self):
self._ensure_todoist_parent_project()
@intent_file_handler('AddToList.intent')
def handle_add_to_list(self, message):
if not self._ensure_todoist():
return
item_name = message.data.get('item')
list_name = message.data.get('list_name')
list_project = self._get_project(list_name)
if list_project:
self.todoist_api.items.add(item_name, project_id=list_project['id'])
self.todoist_api.commit()
self.speak_dialog('AddToList_success', {'item': item_name, 'list_name': list_name})
else:
self.speak_dialog('ListNotFound', {'list_name': list_name})
@intent_file_handler('RemoveFromList.intent')
def handle_remove_from_list(self, message):
if not self._ensure_todoist():
return
item_name = message.data.get('item')
list_name = message.data.get('list_name')
list_project = self._get_project(list_name)
if list_project:
item = next((i for i in self._get_items(list_name) if i['content'].casefold() == item_name.casefold()), None)
if item is not None:
action_item = self.todoist_api.items.get_by_id(item['id'])
action_item.delete()
self.todoist_api.commit()
self.speak_dialog('RemoveFromList_success', {'item': item_name, 'list_name': list_name})
else:
self.speak_dialog('ListNotFound', {'list_name': list_name})
@intent_file_handler('IsItemOnList.intent')
def handle_is_item_on_list(self, message):
if not self._ensure_todoist():
return
item_name = message.data.get('item')
list_name = message.data.get('list_name')
list_project = self._get_project(list_name)
if list_project:
item = next((i for i in self._get_items(list_name) if i['content'].casefold() == item_name.casefold()), None)
if item:
self.speak_dialog('IsItemOnList_yes', {'item': item_name, 'list_name': list_name})
else:
self.speak_dialog('IsItemOnList_no', {'item': item_name, 'list_name': list_name})
else:
self.speak_dialog('ListNotFound', {'list_name': list_name})
@intent_file_handler('WhatIsOnList.intent')
def handle_whats_on_list(self, message):
if not self._ensure_todoist():
return
list_name = message.data.get('list_name')
list_project = self._get_project(list_name)
if list_project:
list_items = self._get_items(list_name)
if len(list_items) > 0:
suffix = ''
if len(list_items) > 1:
last_item = list_items.pop()
suffix = ' and ' + last_item['content']
item_string = ', '.join(i['content'] for i in list_items) + suffix
self.speak_dialog('WhatIsOnList_someItems', {'list_name': list_name, 'val1': item_string})
else:
self.speak_dialog('WhatIsOnList_noItems', {'list_name': list_name})
else:
self.speak_dialog('ListNotFound', {'list_name': list_name})
@intent_file_handler('CreateList.intent')
def handle_create_list(self, message):
if not self._ensure_todoist():
return
list_name = message.data.get('list_name')
# existing_list = next((p for p in self.todoist_api.state['projects'] if p['parent_id'] == self.parent_project_id and p['name'] == list_name), None)
existing_list = self._get_project(list_name)
if not existing_list:
self.todoist_api.projects.add(list_name, parent_id=self.parent_project_id)
self.todoist_api.commit()
self.speak_dialog('CreateList_success', {'list_name': list_name})
else:
self.speak_dialog('CreateList_alreadyExists', {'list_name': list_name})
# this should be called as the first step by each skill to ensure Todoist is connected; will play the "API key not configured" dialog if not
def _ensure_todoist(self):
if self.todoist_api and self.todoist_api.token:
return True
if not self._init_todoist():
self.speak_dialog('ApiKeyNotSet')
return False
return True
# checks if the todoist API has been initialized successfully and attempts to initialize it if not. returns true if successful, false otherwise
def _init_todoist(self):
if self.todoist_api and self.todoist_api.token:
return True
import todoist
self.todoist_api = todoist.TodoistAPI(self.settings.get('todoist_api_key'))
return not not self.todoist_api.token
# checks if the todoist parent project exists and creates it if not. returns true if successful, false otherwise
def _ensure_todoist_parent_project(self):
if self.parent_project_id:
return True
if (not self.todoist_api or not self.todoist_api.token) and not self._init_todoist():
return False
parent_project_name = self.settings.get('parent_project_name')
if not parent_project_name:
parent_project_name = 'Shopping Lists'
self.todoist_api.sync()
projects = self.todoist_api.state['projects']
parent_project = next((p for p in projects if p['name'] == parent_project_name), None)
if not parent_project:
parent_project = self.todoist_api.projects.add(parent_project_name)
self.todoist_api.commit()
self.parent_project_id = parent_project['id']
return True
def _get_project(self, name):
self.todoist_api.sync()
result = next((p for p in self.todoist_api.state['projects'] if p['parent_id'] == self.parent_project_id and p['name'].casefold() == name.casefold()), None)
return result
def _get_items(self, list_name):
self.todoist_api.sync()
items = []
project_id = next(p['id'] for p in self.todoist_api.state['projects'] if p['parent_id'] == self.parent_project_id and p['name'].casefold() == list_name.casefold())
if project_id:
items = [item for item in self.todoist_api.projects.get_data(project_id).get('items')]
return items
def create_skill():
return ShoppingList()