def load_recipes_table(request): """ Required for ajax to show dynamically recipes info when select a recipe """ fs = Fatsecret(settings.CONSUMER_KEY, settings.CONSUMER_SECRET) recipe = request.GET.get('recipe') url_image = '' recipe_info = fs.recipe_get(recipe) ingredients = recipe_info['ingredients'] ingredients = ingredients['ingredient'] ingredients_list = [] cooking_time_min = recipe_info.get('cooking_time_min') for x in ingredients: ingredients_list.append(x.get('ingredient_description')) image = recipe_info.get('recipe_images', '') if image: url_image = image.get('recipe_image') direction = recipe_info.get('directions') direction_list = direction['direction'] cont = 0 description = [] if direction: for x in direction_list: try: description.append( direction['direction']['direction_description']) break except: description.append( direction['direction'][cont].get('direction_description')) cont = cont + 1 else: description = '' nutrients = recipe_info['serving_sizes'] nutrients = nutrients.get('serving') return render( request, 'nutrition/recipes_table.html', { 'url_image': url_image, 'description': description, 'nutrients': nutrients, 'ingredients_list': ingredients_list, 'cooking_time_min': cooking_time_min })
class Bot(object): def __init__(self, name=None, speech_input=False, facebook_input=False): self.phrases = Phrases() self.speech = Speech() self.knowledge = Knowledge(weather_api_token=weather_api_token) self.name = name self.facebook_input = facebook_input if self.facebook_input: self.facebook_response = list() self.speech_input = speech_input self.witai = Wit("S73IKQDWJ22OJMOSD6AOT4CSJOWXIPX6") self.fs = Fatsecret("90fe184a283449ed8a83e35790c04d65", "054e80b2be154337af191be2c9e11c28") self.translator = Translator() def gr_to_en(self, text): return self.translator.translate(text, 'en', 'el').text def en_to_gr(self, text): return self.translator.translate(text, 'el', 'en').text def start(self): if self.speech_input or self.facebook_input: self.decide_action() else: print("Γεία σου! Πως θα μπορούσα να σε βοηθήσω;") while 1: self.decide_action() def learn_action(self, filename, phraseslist): Knowledge.learn_default_responses(file=filename, phrases=phraseslist) def decide_action(self, facebook_input=None): if self.speech_input or self.facebook_input: if self.speech_input: recognizer, audio = self.speech.listen_for_audio() # received audio data, now we'll recognize it using Google Speech Recognition bot_input = self.speech.google_speech_recognition(recognizer, audio) if self.facebook_input: self.facebook_response[:] = [] bot_input = facebook_input else: bot_input = input() if bot_input is not None: try: resp = self.witai.message(bot_input) entities = None intent = None if 'entities' in resp and 'intent' in resp['entities']: entities = resp['entities'] intent = resp['entities']['intent'][0]["value"] # print('Intent: {intent}'.format(intent=intent)) if intent == 'greeting': self.__text_action(self.phrases.get_phrases('greetings_phrases')) elif intent == 'tutorial': self.__tutorial_action() elif intent == 'name': if self.name is not None: self.__text_action(self.phrases.get_phrases('name_phrases').replace('<name>', self.name)) else: self.__text_action('Οι δημιουργοί μου δεν μου έδωσαν κάποιο όνομα') elif intent == 'swear': self.__text_action(self.phrases.get_phrases('swear_phrases')) elif intent == 'funny': self.__text_action(self.phrases.get_phrases('funny_phrases')) elif intent == 'sex_type': self.__text_action('Λίγο απ\'όλα') elif intent == 'user_joke': self.__text_action('χαχαχα') elif intent == 'personal_status': self.__personal_status() elif intent == 'joke': self.__joke_action() elif intent == 'datetime': # print(resp) self.__datetime_action(entities) elif intent == 'weather': self.__weather_action() elif intent == 'search': self.__search_action(entities) elif intent == 'food_det': self.__food_action(entities) elif intent == 'recipe': self.__recipe_action(entities) elif intent == 'thanksgiving': self.__text_action(self.phrases.get_phrases('thanks_phrases')) elif intent == 'sticker': self.__text_action('Επίσης') else: # No recognized intent # print('Intent not recognized') self.__text_action(self.phrases.get_phrases('unrecognized_intent_phrases')) return except Exception as e: print("Exception occured") print(e) traceback.print_exc() self.__text_action("Έγινε κάποιο λάθος") return def __text_action(self, text=None): if text is not None: if self.speech_input: self.speech.synthesize_text(text) if self.facebook_input: self.facebook_response.append(text) if not (self.facebook_input or self.speech_input): print(text) def __tutorial_action(self): self.__text_action(self.phrases.get_phrases('tutorial_phrases')) def __personal_status(self): self.__text_action(self.phrases.get_phrases('personal_status_phrases')) def __joke_action(self): joke = self.phrases.get_phrases('joke_phrases') self.__text_action(joke) def __datetime_action(self, entities): dt = None if 'date' in entities: dt = entities['date'][0]['value'] print('Datetime: {dt}'.format(dt=dt)) if str(dt) == 'hour': self.__text_action('Η ώρα είναι {time}'.format(time=self.knowledge.get_time())) elif str(dt) == 'day': self.__text_action('Σήμερα είναι {weekday}'.format(weekday=self.knowledge.get_weekday())) elif str(dt) == 'date': self.__text_action('Σήμερα είναι {date}'.format(date=self.knowledge.get_date())) def __weather_action(self): weather_obj = self.knowledge.find_weather() self.__text_action('Η θερμοκρασία είναι ' + str(weather_obj['temperature']) + '° Κελσίου.') def __search_action(self, entities=None): self.__text_action(self.phrases.get_phrases('search_phrases')) if 'wikipedia_search_query' in entities: query = entities['wikipedia_search_query'][0]['value'] # print('wikipedia query: {query}'.format(query=query)) wikipedia.set_lang("el") try: self.__text_action(re.sub(r'\([^)]*\)', '', wikipedia.summary(query, sentences=1))) except wikipedia.PageError as e: print(e) self.__text_action('Δεν βρήκα κάποιο αποτέλεσμα') else: self.__text_action('Δεν μου είπες τί να ψάξω') def __food_action(self, entities): self.__text_action(self.phrases.get_phrases('search_phrases')) inp = self.gr_to_en(entities['wikipedia_search_query'][0]['value']) try: resp = self.fs.foods_search(inp) food = self.fs.food_get(resp[0]['food_id']) if 'nutrient_type' in entities.keys(): self.__text_action( self.en_to_gr( '{type} - 1 {serving}'.format(serving=food['servings']['serving'][0]['measurement_description'], type=resp[0]["food_name"]))) for nutrient in entities['nutrient_type']: self.__text_action(self.en_to_gr('{nutrient}: {value}'.format(nutrient=nutrient['value'], value= food['servings']['serving'][0][ nutrient['value']]))) else: self.__text_action(self.en_to_gr(resp[0]["food_name"] + "\n" + resp[0]["food_description"])) except Exception as e: self.__text_action( "Δεν υπάρχουν διαθέσιμες διατροφικές πληροφορίες για " + entities['wikipedia_search_query'][0]['value']) self.__search_action(entities) def __recipe_action(self, entities): self.__text_action(self.phrases.get_phrases('search_phrases')) inp = self.gr_to_en(entities['wikipedia_search_query'][0]['value']) try: resp = self.fs.recipes_search(inp) recipe = self.fs.recipe_get(resp[0]['recipe_id']) if self.facebook_input: self.__text_action("Μπορείς να δεις την συνταγή στο παρακάτω link:") self.__text_action(recipe['recipe_url']) else: self.__text_action(self.en_to_gr(recipe['recipe_name'] + "\n")) self.__text_action("Οδηγίες") for dir in recipe['directions']['direction']: self.__text_action(self.en_to_gr(dir['direction_description'])) self.__text_action("Συστατικά") for ing in recipe['ingredients']['ingredient']: self.__text_action(self.en_to_gr(ing['ingredient_description'])) except Exception as e: self.__text_action( "Δεν υπάρχει διαθέσιμη συνταγή για " + entities['wikipedia_search_query'][0]['value']) self.__search_action(entities)
print("\n\n ---- No Authentication Required ---- \n\n") foods = fs.foods_search("Tacos") print("Food Search Results: {}".format(len(foods))) print("{}\n".format(foods)) food = fs.food_get("1345") print("Food Item 1345") print("{}\n".format(food)) recipes = fs.recipes_search("Tomato Soup") print("Recipe Search Results:") print("{}\n".format(recipes)) recipe = fs.recipe_get("88339") print("Recipe 88339") print("{}\n".format(recipe)) # Test Calls with 3 Legged Oauth print("\n\n ------ OAuth Example ------ \n\n") print(fs.get_authorize_url()) session_token = fs.authenticate(input("\nPIN: ")) foods = fs.foods_get_most_eaten() print("Most Eaten Food Results: {}".format(len(foods))) recipes = fs.recipes_search("Enchiladas") print("Recipe Search Results: {}".format(len(recipes)))
print("\n\n ---- No Authentication Required ---- \n\n") foods = fs.foods_search("Tacos") print("Food Search Results: {}".format(len(foods))) print("{}\n".format(foods)) food = fs.food_get('1345') print("Food Item 1345") print("{}\n".format(food)) recipes = fs.recipes_search("Tomato Soup") print("Recipe Search Results:") print("{}\n".format(recipes)) recipe = fs.recipe_get('88339') print("Recipe 88339") print("{}\n".format(recipe)) # Test Calls with 3 Legged Oauth print("\n\n ------ OAuth Example ------ \n\n") print(fs.get_authorize_url()) session_token = fs.authenticate(input("\nPIN: ")) foods = fs.foods_get_most_eaten() print("Most Eaten Food Results: {}".format(len(foods))) recipes = fs.recipes_search("Enchiladas") print("Recipe Search Results: {}".format(len(recipes)))