Exemple #1
0
def recipe_instructions():
    '''
    GET Recipe Information - spoonacular API
    costs 1 request
    '''

    # get api object from db
    api_obj = Api.query.filter_by(id=1).first()

    # check if we have reached api call limits
    if api_obj.requests < 50:

        # if no limits reached, proceed with api call
        recipe_id = request.args.get('id')
        api_part1 = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/"
        api_part2 = "/information?includeNutrition=false"
        url = api_part1 + recipe_id + api_part2
        headers = {"X-Mashape-Key": mash_key, "Accept": "application/json"}

        json_data = requests.get(url, headers=headers).json()

        recipe_name = json_data['title']

        ingreds = json_data['extendedIngredients']

        recipe_ingredients = []
        for i in range(0, len(ingreds)):
            recipe_ingredients.append(ingreds[i]['originalString'])

        recipe_instructs = json_data['instructions']

        recipe_time = int(json_data['readyInMinutes'])
        '''
        same_recipe = Recipe.query.filter_by(name=recipe_name, instructions=recipe_instructs).first()
        if same_recipe:
            #already in db, just display that one
            return render_template('recipe.html', recipe=same_recipe, ingredients=clean_ingreds(same_recipe))
        '''

        # Make sure recipe found has ingredient list and instructions (surprisingly they don't always)
        if recipe_ingredients == None or recipe_instructions == None:
            flash("That isn't a complete recipe, pick another.", 'negative')
            return redirect('search.html')

        else:
            # add to db if not there
            user = User.getUserByName(session['username'])
            cookbook = Cookbook.query.filter_by(owner_id=user.id).first()

            new_recipe = Recipe(recipe_name, str(recipe_ingredients),
                                recipe_instructs, recipe_time, cookbook.id)
            new = True

            # create variables for current requests and last-api-call to update data in db
            current_requests = api_obj.requests + 1
            last_api_call = date.today()

            # update api table in db then return search results
            api_obj.requests = current_requests
            api_obj.last_api_call = last_api_call
            db.session.commit()

            return render_template('recipe.html',
                                   recipe=new_recipe,
                                   instructions=new_recipe.clean_instructs(),
                                   ingredients=new_recipe.clean_ingreds(),
                                   new=new)

    else:
        if session['username'] == 'admin':
            # call api
            recipe_id = request.args.get('id')
            api_part1 = "https://spoonacular-recipe-food-nutrition-v1.p.mashape.com/recipes/"
            api_part2 = "/information?includeNutrition=false"
            url = api_part1 + recipe_id + api_part2
            headers = {"X-Mashape-Key": mash_key, "Accept": "application/json"}

            json_data = requests.get(url, headers=headers).json()

            recipe_name = json_data['title']

            ingreds = json_data['extendedIngredients']

            recipe_ingredients = []
            for i in range(0, len(ingreds)):
                recipe_ingredients.append(ingreds[i]['originalString'])

            recipe_instructs = json_data['instructions']
            recipe_time = int(json_data['readyInMinutes'])
            '''
            same_recipe = Recipe.query.filter_by(name=recipe_name, instructions=recipe_instructs).first()
            if same_recipe:
                #already in db, just display that one
                return render_template('recipe.html', recipe=same_recipe, ingredients=clean_ingreds(same_recipe))
            '''

            # Make sure recipe found has ingredient list and instructions (surprisingly they don't always)
            if recipe_ingredients == None or recipe_instructions == None:
                flash("That isn't a complete recipe, pick another.",
                      'negative')
                return redirect('search.html')

            else:
                # add to db if not there
                user = User.getUserByName(session['username'])
                cookbook = Cookbook.query.filter_by(owner_id=user.id).first()

                new_recipe = Recipe(recipe_name, str(recipe_ingredients),
                                    recipe_instructs, recipe_time, cookbook.id)
                new = True

                # create variables for current requests and last-api-call to update data in db
                current_requests = api_obj.requests + 1
                last_api_call = date.today()

                # update api table in db then return search results
                api_obj.requests = current_requests
                api_obj.last_api_call = last_api_call
                db.session.commit()

                return render_template(
                    'recipe.html',
                    recipe=new_recipe,
                    instructions=new_recipe.clean_instructs(),
                    ingredients=new_recipe.clean_ingreds(),
                    new=new)

        # if api limit reached and admin not logged in
        else:
            flash(
                "API recipe instructions limit reached.Login as admin to bypass.",
                'negative')
            return render_template('search.html')