def getMoreData(pageNum): temp = page2 temp += str(pageNum) try: html_text = requests.get(temp).text soup = BeautifulSoup(html_text, 'html.parser') recipes = soup.findAll("a", {"class": "tout__titleLink"}) for recipe in recipes: link = root + recipe.attrs['href'] results = HTMLParser.fetchAndParseHTML(link) ingredients = HTMLParser.get_ingredients(results['ingredients']) steps = InstructionParser.parseToolsAndCookingMethod( results, replaceEmptyMainMethod=False) for ing in ingredients: if ing['name'].lower() in meats or ing['name'].lower( ) in seafood: Italian['protein'][ing['name'].lower( )] = Italian['protein'].get(ing['name'].lower(), 0) + 1 elif ing['name'].lower() in vegetables: Italian['vegetables'][ing['name'].lower( )] = Italian['vegetables'].get(ing['name'].lower(), 0) + 1 elif ing['name'].lower() in herbs_spices: Italian['spices'][ing['name'].lower( )] = Italian['spices'].get(ing['name'].lower(), 0) + 1 Italian['cooking methods'][steps['main_cooking_method'].lower( )] = Italian['cooking methods'].get( steps['main_cooking_method'].lower(), 0) + 1 except AttributeError: print("Error Fetching Page Information")
def RecipeParser(url, toVegetarian, toHealthy, toItalian): results = HTMLParser.fetchAndParseHTML(url) if len(results) == 0: print("Please Provide a valid Recipe URL") sys.exit() steps = {} if toItalian: steps = Transformation.toItalian(results) else: steps = InstructionParser.parseToolsAndCookingMethod(results) if toVegetarian: steps = Transformation.toVeggie(steps) else: steps = Transformation.fromVeggie(steps) if toHealthy: steps['ingredients'] = HTMLParser.to_healthy(steps['ingredients']) else: steps['ingredients'] = HTMLParser.to_unhealthy(steps['ingredients']) print("This Recipe Parse will transform", "\"" + results['name'] + "\"", "to:", "Italian" if toItalian else "", "Vegetarian" if toVegetarian else "Non-Vegetarian", "and", "Healthy" if toHealthy else "Unhealthy") res = HTMLParser.format_ings(steps['ingredients']) print("The Ingredients are: ") for i in res: print(i) print() print("The Tools are:") for t in steps['tools']: print(t) print() print("The Primary Cooking Method is:") print(steps['main_cooking_method']) print() print("The Secondary Cooking Methods Are:") print(steps['secondary_cooking_methods']) print() print("The Steps Are:") counter = 1 for s in steps['steps']: print(str(counter) + ".", s['instruction']) counter += 1
def parserUI(results): ingredients = HTMLParser.format_ings( HTMLParser.get_ingredients(results["ingredients"])) tools_instructions = InstructionParser.parseToolsAndCookingMethod(results) active = True first = True while active is True: if first: user = input( "I've found the recipe for \"" + results["name"] + "\".\nMAIN MENU:\n\n[1] look at the ingredients\n[2] look at the steps\n[3] enter another recipe\n[4] quit the conversation?\n\n" ) else: user = input( "I'm sorry, I couldn't process that input. Would you like to: [1] look at the ingredients, [2] look at the steps, [3] enter another recipe, or [4] quit the conversation? \n" ) if user == '1': print_ing = "Here is the list of ingredients:\n" for ing in ingredients: print_ing += ing + "\n" print(print_ing) conversation(tools_instructions, ingredients) active = False elif user == '2': print("The first step is: " + tools_instructions["steps"][0]["instruction"] + "\n") conversation(tools_instructions, ingredients) active = False elif user == '3': urlinput(False) active = False elif user == '4': print("Goodbye!") time.sleep(2) sys.exit(0) else: first = False
except AttributeError: print("Error Fetching Page Information") try: for i in range(2, 6): getMoreData(2) html_text = requests.get(url).text soup = BeautifulSoup(html_text, 'html.parser') recipes = soup.findAll("div", {"class": "card__detailsContainer"}) for recipe in recipes: link = recipe.findChildren("a", recursive=True)[0].attrs['href'] results = HTMLParser.fetchAndParseHTML(link) ingredients = HTMLParser.get_ingredients(results['ingredients']) steps = InstructionParser.parseToolsAndCookingMethod( results, replaceEmptyMainMethod=False) for ing in ingredients: if ing['name'].lower() in meats or ing['name'].lower() in seafood: Italian['protein'][ing['name'].lower( )] = Italian['protein'].get(ing['name'].lower(), 0) + 1 elif ing['name'].lower() in vegetables: Italian['vegetables'][ing['name'].lower( )] = Italian['vegetables'].get(ing['name'].lower(), 0) + 1 elif ing['name'].lower() in herbs_spices: Italian['spices'][ing['name'].lower()] = Italian['spices'].get( ing['name'].lower(), 0) + 1 Italian['cooking methods'][steps['main_cooking_method'].lower( )] = Italian['cooking methods'].get( steps['main_cooking_method'].lower(), 0) + 1 print(Italian) with open('Italian_Ingredients.json', 'w') as f:
def toItalian(url): if not os.path.exists('Italian_Ingredients.json'): print( "Run StyleOfCuisine.py to generate italian cuisine ingredients first" ) sys.exit() with open('Italian_Ingredients.json') as f: italian = json.load(f) steps = InstructionParser.parseToolsAndCookingMethod(url) new_ingredients = [] protein_subs = list(italian['protein'].keys()) vegetable_subs = list(italian['vegetables'].keys()) spice_subs = list(italian['spices'].keys()) rep_map = {} for ing in steps['ingredients']: for token in ing['name'].lower().split(): if token in meats or token in seafood and ing[ 'name'] not in protein_subs: rep = random.choice(protein_subs) rep_map[ing['name']] = rep if rep in [i['name'] for i in new_ingredients]: for i in new_ingredients: if i['name'] == rep: i['quantity'] += ing['quantity'] break else: ing['name'] = rep protein_subs.remove(rep) break elif token in vegetables and ing['name'] not in vegetable_subs: rep = random.choice(vegetable_subs) rep_map[ing['name']] = rep if rep in [i['name'] for i in new_ingredients]: for i in new_ingredients: if i['name'] == rep: i['quantity'] += ing['quantity'] break else: ing['name'] = rep vegetable_subs.remove(rep) break elif token in herbs_spices and ing['name'] not in spice_subs: rep = random.choice(spice_subs) rep_map[ing['name']] = rep if rep in [i['name'] for i in new_ingredients]: for i in new_ingredients: if i['name'] == rep: i['quantity'] += ing['quantity'] break else: ing['name'] = rep spice_subs.remove(rep) break new_ingredients.append(ing) method_subs = list(italian['cooking methods'].keys()) method_subs = method_subs[1:] for s in steps['steps']: for i in s['ingredients'].keys(): if s['ingredients'][i] in rep_map.keys(): s['instruction'] = s['instruction'].replace( i, rep_map[s['ingredients'][i]]) new_actions = [] for a in s['action']: if a not in method_subs: rep = rep_map.get(a, random.choice(method_subs)) rep_map[a] = rep new_actions.append(rep) ins = nlp(s['instruction']) for token in ins: if token.lemma_ == a: suffix = '' if token.text.endswith('ing'): suffix = 'ing' elif token.text.endswith('ed'): suffix = 'ed' if suffix != '': if rep[-2] in ['a', 'e', 'i', 'o', 'u']: rep += rep[-1] elif rep[-1] == 'e': rep = rep[:-1] elif rep == 'fry' and suffix == 'ed': rep = 'fri' s['instruction'] = s['instruction'].replace( token.text, rep + suffix) else: new_actions.append(a) s['action'] = new_actions steps['main_cooking_method'] = rep_map.get(steps['main_cooking_method'], steps['main_cooking_method']) steps['ingredients'] = new_ingredients return steps