def next_random_meal(): """ returns a dictionary for the AJAX query. """ recipe_maker = RecipeMachine() recipe_method = request.args.get("settings") if recipe_method == "kmeans": ingredients = recipe_maker.generate_kmeans_recipe() elif recipe_method == "markov": ingredients = recipe_maker.generate_markov_recipe() elif recipe_method=="random": ingredients = recipe_maker.generate_random_recipe() session['meal'] = ingredients recipe_method=recipe_method if request.args.get("checked") == "yes": try: yummly_api_request = requests.get('http://api.yummly.com/v1/api/recipes?_app_id=' +YUMMLY_APP_ID+'&_app_key=' +YUMMLY_APP_KEY+'&q='+str(ingredients['vegetable']) +'%2C+'+str(ingredients['protein'])+'%2C+' +str(ingredients['starch'])+'&requirePictures=true') json_text = yummly_api_request.text json_object = json.loads(json_text) try: end_of_url = json_object['matches'][0][u'id'] large_image = json_object['matches'][0][u'imageUrlsBySize'][u'90'].replace('=s90-c','=s730-e365') yummly_rec_name = json_object['matches'][0][u'recipeName'] except: end_of_url = "" large_image = "http://upload.wikimedia.org/wikipedia/commons/1/18/Yummly_logo.png" yummly_rec_name = "Why don't you try" except: end_of_url ="" large_image = "" yummly_rec_name ="" else: end_of_url ="" large_image = "" yummly_rec_name ="" rendering_info = {'vegetable':ingredients['vegetable'], 'protein':ingredients['protein'], 'starch':ingredients['starch'],'yummly_image_url':large_image, 'end_of_url':end_of_url,'recipe_name':yummly_rec_name,'recipe_method':recipe_method} return jsonify(rendering_info)
def hello_custom(): """ sends kmeans meal text. Instantiates RecipeMachine class and generates a k-means meal that is then sent out via twilio text if the twilio number is texted. """ recipe_maker = RecipeMachine() ingredients = recipe_maker.generate_kmeans_recipe() message = ingredients['vegetable']+" "+ingredients['protein']+" "+ingredients['starch'] resp = twilio.twiml.Response() resp.message(message) return str(resp)
def random_api(): """ k-means meal to API Instantiates the RecipeMachine class and generates a kmeans meal that is rendered at localhost:5000/api """ recipe_maker = RecipeMachine() ingredients = recipe_maker.generate_kmeans_recipe() dict_meal = {"meal":{"protein":ingredients['protein'], "vegetable":ingredients['vegetable'], "starch":ingredients['starch']}} api_meal = json.dumps(dict_meal) return api_meal
def random_meal(): """Loads the feedme page and creates a random meal. """ recipe_maker = RecipeMachine() recipe_method = session.get('default_setting', "random") methods = {"kmeans":recipe_maker.generate_kmeans_recipe, "markov":recipe_maker.generate_markov_recipe, "random":recipe_maker.generate_random_recipe} ingredients = methods[recipe_method]() session['meal'] = ingredients recipe_method = recipe_method return render_template("random_meal.html",vegetable=ingredients['vegetable'], protein=ingredients['protein'],starch=ingredients['starch'], yummly_image_url="", end_of_url="", recipe_method=recipe_method)