def test_get_recipes_from_category(self): self.user.create_recipe_category(self.recipe_category) self.user.add_recipe(self.recipe_category, self.recipe) self.assertEqual(len(self.recipe_category.recipes), 1) self.recipe = Recipe('Chicken Luwombo', 'Spectacular local source food') self.user.add_recipe(self.recipe_category, self.recipe) self.recipe = Recipe('Vegetable Luwombo', 'Spectacular local source food') self.user.add_recipe(self.recipe_category, self.recipe) self.assertEqual( len(self.user.get_recipes_from_category(self.recipe_category)), 3)
def test_add_recipes(self): """Adds recipes to recipe category """ self.user.create_recipe_category(self.recipe_category) self.user.add_recipe(self.recipe_category, self.recipe) self.assertEqual(len(self.recipe_category.recipes), 1) self.recipe = Recipe('Chicken Luwombo', 'Spectacular local source food') self.user.add_recipe(self.recipe_category, self.recipe) self.recipe = Recipe('Vegetable Luwombo', 'Spectacular local source food') self.user.add_recipe(self.recipe_category, self.recipe) self.assertEqual(len(self.recipe_category.recipes), 3)
def scrape(): global urls recipes = [] for url in urls: recipe_id = 0 page = requests.get(url) soup = BeautifulSoup(page.content, 'html.parser') title = soup.find('h1') # look for a div where the classname contains ingredients and get the list from there ingredient_container = soup.find('div', {'class': 'recipe-shopper-wrapper'}) ingredients = [] try: ingredients = ingredient_container.find_all('li') except: print('could not find ingredient') for index, ingredient in enumerate(ingredients): ingredient_name = ingredient.find('span', { 'class': re.compile(r'name|Name|ingredient(?!.*(unit|amount))') }) if ingredient_name: ingredient = ingredient_name ingredient = ingredient.text.strip() ingredients[index] = re.sub(r'\s+', ' ', ingredient) #look for a div where the classname contains ingredients and get the list from there direction_container = soup.find('section', {'class': 'recipe-instructions'}) directions = [] try: directions = direction_container.find_all('li') except: print('could not find directions') for direction in directions: direction = direction.text.strip() direction = re.sub(r'\s+', ' ', direction) if title: print('RECIPE:', title.text.strip()) title = title.text.strip() else: title = None recipe = Recipe(title, ingredients, directions, url) recipes.append(recipe) print('this recipe has', len(ingredients), 'ingredients, and', len(directions), 'steps') return recipes
def test_edit_recipe_in_category(self): self.user.create_recipe_category(self.recipe_category) self.user.add_recipe(self.recipe_category, self.recipe) self.assertEqual(len(self.recipe_category.recipes), 1) self.recipe = Recipe('Chicken Luwombo', 'Spectacular local source food', 'Onions') self.user.add_recipe(self.recipe_category, self.recipe) self.user.edit_recipe_in_category(self.recipe_category, 'Chicken Luwombo', 'Chicken Recipe Luwombo', 'Spectacular Recipe local source food', 'Tomatoes') self.assertEqual(self.recipe_category.recipes[1].name, 'Chicken Recipe Luwombo')
def test_get_single_recipe_from_category(self): self.user.create_recipe_category(self.recipe_category) self.user.add_recipe(self.recipe_category, self.recipe) self.assertEqual(len(self.recipe_category.recipes), 1) self.recipe = Recipe('Chicken Luwombo', 'Spectacular local source food') self.user.add_recipe(self.recipe_category, self.recipe) self.assertEqual(self.user.get_single_recipe_from_category( self.recipe_category, 'Chicken Luwombo' ).name, 'Chicken Luwombo') self.assertEqual(self.user.get_single_recipe_from_category( self.recipe_category, 'Chicken Luwombo' ).description, 'Spectacular local source food')
def scrape(): global urls recipes = [] for url in urls: # Q: What if the recipe site I want to extract information from is not listed below? # A: You can give it a try with the wild_mode option! If there is Schema/Recipe available it will work just fine. try: scraper = scrape_me(url, wild_mode=True) if scraper: recipe = Recipe(scraper.title(), scraper.ingredients(), scraper.instructions(), url, scraper.author()) recipes.append(recipe) except: print('no recipe schema for ', url) return recipes
def add_recipe(): """Add recipes to recipe category""" if request.method == 'POST': category_name = request.form['category_name'] recipe_name = request.form['recipe_name'] ingredients = request.form['ingredients'] description = request.form['description'] user = [user for user in recipe_app.users if user.id == session['id']] category = user[0].get_single_category(category_name) recipe = Recipe(recipe_name, description, ingredients) if user[0].add_recipe(category, recipe): return redirect( url_for('recipe_category', categoryname=category_name))
def scrape(): #try for 1000 files recipes = [] directory = os.listdir('nyt_recipes/') for index, file in enumerate(directory): # print(index, file) if index < 200: d = pq(filename='nyt_recipes/' + file) ingredients = [] directions = [] tags = [] url = d('meta[property="og:url"]').attr("content") cuisineTag = d('meta[itemProp="recipeCuisine"]').attr("value") if cuisineTag is not None: tags.append(cuisineTag) for el in d('div.tags-nutrition-container a'): print(d(el).text()) tags.append(d(el).text()) # print(d) title = d(".recipe-title").text() author = d("span.byline-name").text() print(title, author) for el in d("span.ingredient-name"): ingredients.append(d(el).text()) for el in d("ol.recipe-steps > li"): directions.append(d(el).text()) recipe = Recipe(title, ingredients, directions, url, author, tags) recipes.append(recipe) return recipes
def recipe_get(self, idx, conn): selrecipes = select([self.recipes]).where(self.recipes.c.id == idx) recipesresult = conn.execute(selrecipes) recipex = "" for recipesrow in recipesresult: selusers = select([self.users]).where(self.users.c.id == recipesrow.user_id) usersresult = conn.execute(selusers) author = usersresult.fetchone().name selingrslst = select([self.ingredients_list]).where( self.ingredients_list.c.recipe_id == idx) ingredslstres = conn.execute(selingrslst) ings = [] for ingredslstrow in ingredslstres: selingrds = select([self.ingredients]).where( self.ingredients.c.id == ingredslstrow.ingredient_id) ingrdsresult = conn.execute(selingrds) for ingredsrow in ingrdsresult: ing = Ingredient( ingredsrow.name, ingredsrow.allergen, ingredslstrow.quantity) ings.append(ing) seldirs = select([self.directions_list]).where( self.directions_list.c.recipe_id == idx) dirsresult = conn.execute(seldirs) dirs = [] for dirsrow in dirsresult: dir = Direction(dirsrow.number, dirsrow.text) dirs.append(dir) recipex = Recipe(idx, recipesrow.name, recipesrow.country, recipesrow.course, recipesrow.views, author, ings, recipesrow.image, dirs) return recipex
def setUp(self): self.user = User('*****@*****.**', 'pato123') self.recipe_category = RecipeCategory('Luwombo', 'Delicious Luwombo') self.recipe = Recipe('Meat Luwombo', 'Spectacular local source food')
def scrape(): recipes = [] for page in pages: page = "https://www.spendwithpennies.com/borscht-recipe-beet-soup/" driver = webdriver.Chrome('/Users/MyUsername/Downloads/chromedriver') driver.get(page) sleep(randint(1, 2)) soup = BeautifulSoup(driver.page_source, 'html.parser') title = soup.find('h1') ingredient_container = soup.find( 'div', { 'class': re.compile( r'(?<!direction.)ingredient(?!.*(direction|instruction|steps))' ) }) ingredients = [] try: ingredients = ingredient_container.find_all('li') except: print("ingredient not found") for index, ingredient in enumerate(ingredients): ingredient_name = ingredient.find('span', { 'class': re.compile(r'name|Name|ingredient(?!.*(unit|amount))') }) if ingredient_name: ingredient = ingredient_name ingredient = ingredient.text.strip() ingredients[index] = re.sub(r'\s+', ' ', ingredient) direction_container = soup.find( 'div', { 'class': re.compile( r'(?<!ingredient.)direction|instruction|steps(?!.*(ingredient|description))' ) }) directions = [] try: directions = direction_container.find_all('li') except: print('could not find directions') for direction in directions: direction = direction.text.strip() direction = re.sub(r'\s+', ' ', direction) if title: print('RECIPE:', title.text.strip()) title = title.text.strip() else: title = None recipe = Recipe(title, ingredients, directions, page) recipes.append(recipe) print('this recipe has', len(ingredients), 'ingredients, and', len(directions), 'steps') return recipes