Пример #1
0
def make_recipe_dict(url):
	recipe = {'ingredients':[],'cooking tools':[]}
	contents = recipe_parser.http_string(url)
	recipe['name'] = recipe_parser.recipe_name(url)
	recipe['directions'] = recipe_parser.directions(contents)


	ingredients = recipe_parser.ingredients(contents)
	for ingredient in ingredients:
		[iname,quantity,measure,descriptor,prep,category] = ingredient
		recipe['ingredients'].append({'name':iname.lower(),'quantity':quantity,'measurement':measure,'descriptor':descriptor,'preparation':prep, 'category':category})
		
		#find all tools implied by prep fields, add to 'cooking tools', (TODO:)and maybe add actions to 'intermediate methods'
		tool = database.find_prep_tool_for_action(prep)
		if tool != 'notfound' and tool not in recipe['cooking tools']:
			recipe['cooking tools'].append(tool)
	
	#find all tools implied by actions in directions, adding where appropriate
	#find all tools mentioned in directions, add to 'cooking tools', and maybe add actions to 'intermediate methods'
	recipe['cooking tools'].extend(database.detect_tools(recipe['directions']))
	#make 'cooking tools' a set
	recipe['cooking tools']= list(set(recipe['cooking tools']))
	#find cooking method and add to 'cooking method'
	#start with the directions and then move on to the tools if still not found
	methods = recipe_methods.getMethods(url)
	if methods ==[]:
		for tool in recipe['cooking tools']:
			method = database.find_method_from_tool(tool)
			if method != 'notfound':
				recipe['cooking method'] = method
	else:
		#grab last one because it is most likely to be the main cooking method
		recipe['cooking method'] = methods[-1]
	return recipe
def getMethods(recipe_url):
	all_methods, all_tools = database.get_tools_and_methods()
	# feed in the url of the recipe
	content = recipe_parser.http_string(recipe_url)

	methods = []
	# tools = []

	#send it to directions to get the recipe's directions section
	recipDirections = recipe_parser.directions_steps(content)
	
	# to print each instruction
	for direction in recipDirections:
	# 	found_tool = search(direction,all_tools)
	# 	if found_tool != None and found_tool not in tool:
	# 		tools.append(found_tool)

		found_method = search(direction, all_methods)
		if found_method != None and found_method not in method:
			methods.append(found_method)
			
	return methods#, tools