forked from forslund/skill-cocktail
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
89 lines (75 loc) · 2.98 KB
/
__init__.py
File metadata and controls
89 lines (75 loc) · 2.98 KB
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
from mycroft import MycroftSkill, intent_file_handler, intent_handler, \
AdaptIntent
from mycroft.util.log import LOG
import requests
import time
API_KEY = '2432'
API_URL = 'https://www.thecocktaildb.com/api/json/v1/{}/'.format(API_KEY)
SEARCH = API_URL + 'search.php'
def search_cocktail(name):
r = requests.get(SEARCH, params={'s': name})
if (200 <= r.status_code < 300 and 'drinks' in r.json() and
r.json()['drinks']):
return r.json()['drinks'][0]
else:
return None
def ingredients(drink):
ingredients = []
for i in range(1, 15):
if not drink['strIngredient' + str(i)]:
break
ingredients.append(' '.join((drink['strMeasure' + str(i)],
drink['strIngredient' + str(i)])))
return nice_ingredients(ingredients)
def nice_ingredients(ingredients):
units = {
'oz': 'ounce',
'1 tbl': '1 table spoon',
'tbl': 'table spoons',
'1 tsp': 'tea spoon',
'tsp': 'tea spoons',
'ml ': 'milliliter ',
'cl ': 'centiliter '
}
ret = []
for i in ingredients:
for word, replacement in units.items():
i = i.lower().replace(word, replacement)
ret.append(i)
return ret
class CocktailSkill(MycroftSkill):
@intent_file_handler('Recipie.intent')
def get_recipie(self, message):
cocktail = search_cocktail(message.data['drink'])
if cocktail:
self.speak_dialog('YouWillNeed', {
'ingredients': ', '.join(ingredients(cocktail)[:-1]),
'final_ingredient': ingredients(cocktail)[-1]})
time.sleep(1)
self.speak(cocktail['strInstructions'])
self.set_context('IngredientContext', str(ingredients(cocktail)))
else:
self.speak_dialog('NotFound')
def repeat_ingredients(self, ingredients):
self.speak(ingredients)
@intent_file_handler('Needed.intent')
def get_ingredients(self, message):
cocktail = search_cocktail(message.data['drink'])
if cocktail:
self.speak_dialog('YouWillNeed', {
'ingredients': ', '.join(ingredients(cocktail)[:-1]),
'final_ingredient': ingredients(cocktail)[-1]})
self.set_context('IngredientContext', str(ingredients(cocktail)))
else:
self.speak_dialog('NotFound')
@intent_handler(AdaptIntent().require('Ingredients').require('What')
.require('IngredientContext'))
def what_were_ingredients(self, message):
return self.repeat_ingredients(message.data['IngredientContext'])
@intent_handler(AdaptIntent().require('Ingredients').require('TellMe')
.require('Again')
.require('IngredientContext'))
def tell_ingredients_again(self, message):
return self.repeat_ingredients(message.data['IngredientContext'])
def create_skill():
return CocktailSkill()