Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests,sys,json
- def get_leftover_ingredient():
- while True:
- ingredient=input("List ingredient you want to use-up and its amount separated by comma, with unit of measure at the end,separated by comma\nThe following units are accepted: piece,oz,lb,g,kg,cup,teaspoon,tablespoon,ml,l\nIngredient:").strip().split(",")
- if len(ingredient)==3:
- if check_ingredient_validity(ingredient[0])>0:
- if check_uom(ingredient[2])==True:
- return ingredient
- break
- def check_ingredient_validity(ingredient):
- try:
- r = requests.get('https://api.spoonacular.com/food/ingredients/search?apiKey=f017f95bd138484ea337209a60528299&query='+ingredient)
- if next(iter(r.json().values()))!="failure":
- if r.json()["totalResults"]==0:
- return False
- else:
- id=(r.json()["results"][0]["id"])
- return id
- else:
- sys.exit("Access point limit exceeded. Aborting program. Try again later")
- except requests.RequestException:
- sys.exit("Connection to spoonacular.com error. Try again later")
- def check_uom(uom):
- if uom.strip().lower() not in ["piece","oz","lb","g","kg","cup","teaspoon","tablespoon","ml","l"]:
- return False
- else:
- return True
- def get_recipes_from_file(ingredient):
- with open(r'recipes.json') as rec:
- recipes = json.load(rec)
- where_used=[recipe["Name"] for recipe in recipes if ingredient in recipe["Ingredients"]]
- return where_used
- def get_sugar_per_item(name,qty,uom):
- try:
- id=check_ingredient_validity(name)
- if id=="Point limit exceeded":
- sys.exit("Daily connection limit to spoonacular server exceeded. Aborting program. Try again later.")
- else:
- data = requests.get('https://api.spoonacular.com/food/ingredients/'+str(id)+'/information?apiKey=f017f95bd138484ea337209a60528299&amount='+qty+'&unit='+uom)
- json_data=data.json()
- amount = [nutrient['amount'] for nutrient in json_data["nutrition"]["nutrients"] if nutrient["name"] == "Sugar"]
- return amount
- except requests.RequestException:
- sys.exit("Connection to spoonacular server error. Aborting program. Try again later.")
- def get_sugar_per_recipe(recipe_name):
- with open(r'recipes.json') as rec:
- recipes = json.load(rec)
- sugar_amount_matrix = [get_sugar_per_item(item,recipe["Ingredients"][item][0],recipe["Ingredients"][item][1]) for recipe in recipes for item in recipe['Ingredients'].keys() if recipe["Name"]==recipe_name]
- sugar_amount_value=sum(y for x in sugar_amount_matrix for y in x)
- return sugar_amount_value
- def get_ingredient_balance_for_recipe(ingr,recipe):
- with open(r'recipes.json') as rec:
- recipes = json.load(rec)
- for item in recipes:
- for ingredient in item["Ingredients"].keys():
- if ingredient==ingr[0] and item["Name"]==recipe:
- if ingr[2]==item["Ingredients"][ingredient][1]:
- balance=[ingr[1]-item["Ingredients"][ingredient][0],"same uom",item["Ingredients"][ingredient][2]]
- else:
- balance=[item["Ingredients"][ingredient][0],"different uom",item["Ingredients"][ingredient][1]]
- return balance
- def print_recipe(recipe):
- with open(r'recipes.json') as rec:
- recipes = json.load(rec)
- print("Recipe name:",recipe)
- print("\nGrams of sugar in the recipe:",get_sugar_per_recipe(recipe),"\n\nIngredients:")
- for item in recipes:
- if item["Name"]==recipe:
- for ingredient in item["Ingredients"].keys():
- print(" ",ingredient,item["Ingredients"][ingredient][0],item["Ingredients"][ingredient][1])
- def main():
- ingredient=get_leftover_ingredient()
- recipes=get_recipes_from_file(ingredient[0])
- ingredient_balance= [get_ingredient_balance_for_recipe(ingredient,recipe) for recipe in recipes]
- print("The ingredient is used in the following recipes:")
- for recipe in recipes:
- print(recipe)
- balance=get_ingredient_balance_for_recipe(ingredient,recipe)
- if balance[1]=="same uom":
- if balance[0]>0:
- print("You'll have",balance[0],"of",ingredient[0],"left after preparing",recipe)
- elif balance[1]<0:
- print("You'll need additional",-int[balance[1]],"of",ingredient[0],"for preparing",recipe)
- else:
- print("You have exactly the needed amount of",ingredient[0],"for preparing",recipe)
- else:
- print("You have",ingredient[1],ingredient[2],"of",ingredient[0],"while",balance[0],balance[2],"is needed\n" )
- main()
Advertisement
Add Comment
Please, Sign In to add comment