Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Remi/IngredientSearch().py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
151 lines (143 sloc)
7.03 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def IngredientSearch(): | |
import requests | |
import json | |
#imports the python modules used in this program | |
#json allows the api to be read and used in python | |
#reqests allows website urls o be read and used in python | |
url="https://api.spoonacular.com/recipes/findByIngredients?apiKey=cbd2e8ce18e04227b99205001c304bfa" | |
#this is the api url used to find recipes by ingredients. This is the base url with no specific ingredients yet | |
numberloop=0 | |
while numberloop == 0: | |
#this is a loop so that the user can go back when they've entered an invalid answer. As long as numberloop=0, the loop will continue | |
number= input("How many recipes would you like to see? ") | |
#this is an input with a variable. This allows the user to enter a value of their choice | |
if number.isdigit(): | |
#isdigit() is an inbuilt python function that checks tha the value that the user has entered is infact an integer | |
if 0 < int(number) <= 100: | |
#this checks the value is inbetween the values 1-100 as they are the the only values the api accepts | |
numberloop=1 | |
#this breaks the loop so that the user can continue | |
if int(number) > 100: | |
#this checks if the value is over 100 | |
print("I havent got enough cook books for this amount of recipes!") | |
print("Please enter a number between 1-100") | |
#these two lines output to the user that the answer that they've enetered in infact invalid and can't be used, then gives the user direction | |
numberloop= 0 | |
#this makes sure the loop continues | |
if int(number) < 1: | |
#this checks if the value is 0 or less | |
print("You can't have no recipes, how will you be a great chef like me?") | |
print("Please enter a number between 1-100") | |
#these two lines output to the user that the value that they've entered is infact invalid and gives them guidance on what to enter | |
numberloop=0 | |
#this makes sure the loop continues | |
else: | |
#this is triggered when the input is not an integer | |
print("This isn't a number! Please enter a number between 1-100") | |
#this outputs to the user that they've entered an invalid value and gives them guiance on what to enter | |
numberloop=0 | |
#this makes sure the loop continues | |
rankingloop=0 | |
#sets the value for the next loop | |
while rankingloop == 0: | |
#this is the start of the loop for the user to choose a ranking. This is so if the user enters an incorrect value the program will allow them to change it | |
#this will stop the progra mbreaking further down the line | |
ranking= input("Would you like to see maximum ingredients used first or the minimal missing ingredients first? ") | |
if ranking == "used" or ranking == "maximum ingredients used" or ranking == "maximum": | |
ranking= 1 | |
rankingloop=1 | |
break | |
if ranking == "minimal" or ranking == "missing" or ranking == "minimal missing ingredients": | |
ranking= 2 | |
rankingloop=1 | |
break | |
else: | |
print("Thats not a valid input, please enter 'minimal' or 'maximum'") | |
rankingloop=0 | |
url= url + "&number=" + str(number) + "&ranking=" + str(ranking) + "&ingredients=" | |
x=0 | |
y=0 | |
state=0 | |
while state == 0: | |
ingredient=input("Please enter the name of the ingredient you would like to find" ) | |
ingredient2= ingredient.lower() | |
if y == 1: | |
x=x+1 | |
if x == 0: | |
url= url + str(ingredient2) | |
rsp= input("Would you like to add another ingredient? ") | |
if rsp == "yes": | |
y=1 | |
state= 0 | |
if rsp == "no": | |
state= 1 | |
break | |
if x > 0: | |
url= url + ",+" + str(ingredient2) | |
rsp= input("Would you like to add another ingredient? ") | |
if rsp == "yes": | |
state= 0 | |
if rsp == "no": | |
state= 1 | |
break | |
r= requests.get(url) | |
response=r.json() | |
idlist=[] | |
titlelist=[] | |
for n in response: | |
titlelist.append(n['title']) | |
idlist.append(n['id']) | |
print(n['title']) | |
print("You are missing " + str(n['missedIngredientCount']) + " ingredient/s") | |
print("You can use " + str(n['usedIngredientCount']) + " of your ingredients") | |
print("") | |
count=0 | |
loop=0 | |
while loop == 0: | |
value=0 | |
rsp=input("Would you like to choose one of these recipes") | |
if rsp.lower() == "no" or rsp.lower() == "nah" or rsp.lower() == "n" or rsp.lower() == "nope": | |
tryagain=input("Would you like to try for another recipe or not?") | |
if tryagain.lower() == "yes" or tryagain.lower() == "yeah" or tryagain.lower() == "yep" or tryagain.lower() == "y": | |
IngredientSearch() | |
if tryagain.lower() == "no" or tryagain.lower() == "nope" or tryagain.lower() == "nah" or tryagain.lower() == "n": | |
print("Im sorry I couldnt help you today") | |
from Stub import Stub | |
Stub() | |
loop=1 | |
break | |
else: | |
print("Sorry I didn't recognise your answer") | |
loop=0 | |
value=1 | |
if rsp.lower() == "yes" or rsp.lower() == "yeah" or rsp.lower() == "y" or rsp.lower() == "yep": | |
recipe=input("Please enter the name of the recipe") | |
for item in range(0,len(titlelist)): | |
if recipe == titlelist[item]: | |
print("The recipe you have chosen is " + recipe) | |
recipeid= idlist[item] | |
url2="https://api.spoonacular.com/recipes/informationBulk?apiKey=cbd2e8ce18e04227b99205001c304bfa&ids=" + str(recipeid) | |
r2=requests.get(url2) | |
response2= r2.json() | |
url3="https://api.spoonacular.com/recipes/" + str(recipeid) + "/analyzedInstructions?apiKey=cbd2e8ce18e04227b99205001c304bfa" | |
print(recipeid) | |
r3=requests.get(url3) | |
response3= r3.json() | |
print("These are the ingredients:") | |
for m in response2[0]['extendedIngredients']: | |
print(m['originalString']) | |
for n in response3[0]['steps']: | |
print("This is step number " + str(n['number'])) | |
print("") | |
print(n['step']) | |
print("") | |
loop=1 | |
break | |
else: | |
print("You entered the wrong recipe") | |
loop=0 | |
else: | |
if value == 0: | |
print("Sorry I dont recognise your answer!") | |
loop=0 | |
IngredientSearch() |