From 0c1849697ed6ad9367b8e601ead49e3a5b056f3d Mon Sep 17 00:00:00 2001 From: Joey <7505194+jknndy@users.noreply.github.com> Date: Sun, 22 Dec 2024 16:47:13 -0500 Subject: [PATCH] refactor --- recipe_scrapers/latelierderoxane.py | 76 +- ...rderoxane.json => latelierderoxane_1.json} | 10 +- ...e.testhtml => latelierderoxane_1.testhtml} | 530 ++++++++----- .../latelierderoxane_2.json | 72 ++ .../latelierderoxane_2.testhtml | 706 ++++++++++++++++++ 5 files changed, 1184 insertions(+), 210 deletions(-) rename tests/test_data/latelierderoxane.com/{latelierderoxane.json => latelierderoxane_1.json} (83%) rename tests/test_data/latelierderoxane.com/{latelierderoxane.testhtml => latelierderoxane_1.testhtml} (54%) create mode 100644 tests/test_data/latelierderoxane.com/latelierderoxane_2.json create mode 100644 tests/test_data/latelierderoxane.com/latelierderoxane_2.testhtml diff --git a/recipe_scrapers/latelierderoxane.py b/recipe_scrapers/latelierderoxane.py index 4e1be91f9..5ee0fb375 100644 --- a/recipe_scrapers/latelierderoxane.py +++ b/recipe_scrapers/latelierderoxane.py @@ -1,5 +1,6 @@ from ._abstract import AbstractScraper from ._utils import get_minutes, get_yields, normalize_string +from ._grouping_utils import IngredientGroup class LAtelierDeRoxane(AbstractScraper): @@ -8,47 +9,74 @@ def host(cls): return "latelierderoxane.com" def title(self): - div = self.soup.find("div", {"class": "bloc_titreh1 bloc_blog"}) - return div.find("h1").get_text() + return self._get_text_from_bloc("bloc_titreh1", "h1") def description(self): - div = self.soup.find("div", {"class": "bloc_chapeau bloc_blog"}) - cleaned_description = div.find("p").get_text() - return normalize_string(cleaned_description) + return normalize_string(self._get_text_from_bloc("bloc_chapeau", "p")) def total_time(self): - return get_minutes(self.get_bloc_temps_value_by_index(0)) + return get_minutes(self._get_bloc_temps_value_by_index(0)) def prep_time(self): - return get_minutes(self.get_bloc_temps_value_by_index(1)) + return get_minutes(self._get_bloc_temps_value_by_index(1)) def cook_time(self): - return get_minutes(self.get_bloc_temps_value_by_index(2)) + return get_minutes(self._get_bloc_temps_value_by_index(2)) def yields(self): - return get_yields(self.get_bloc_temps_value_by_index(4)) + return get_yields(self._get_bloc_temps_value_by_index(4)) def ingredients(self): - raw_ingredients = self.soup.find_all("div", {"class": "ingredient"}) - formatted_ingredients = [] - for ingredient in raw_ingredients: - formatted_ingredients.append(normalize_string(ingredient.get_text())) - return formatted_ingredients + ingredients_divs = self.soup.find_all("div", {"class": "ingredient"}) + ingredients = [] + for ingredient_div in ingredients_divs: + text = normalize_string(ingredient_div.get_text(strip=True)) + if text and ":" not in text: + ingredients.append(text) + return ingredients + + def ingredient_groups(self): + ingredients_divs = self.soup.find_all("div", {"class": "ingredient"}) + groups = [] + current_group = {"purpose": None, "ingredients": []} + + for ingredient_div in ingredients_divs: + text = normalize_string(ingredient_div.get_text(strip=True)) + if not text: + continue + if ":" in text: + if current_group["ingredients"]: + groups.append(current_group) + current_group = {"purpose": text.strip(":"), "ingredients": []} + else: + current_group["ingredients"].append(text) + + if current_group["ingredients"]: + groups.append(current_group) + + return [ + IngredientGroup(ingredients=group["ingredients"], purpose=group["purpose"]) + for group in groups + ] def instructions(self): - instruction_bloc = self.soup.find( + instructions_div = self.soup.find( "div", {"class": "bloc_texte_simple bloc_blog"} ) - instructions = instruction_bloc.find_all("li") + instructions_list = instructions_div.find_all("li") return "\n".join( - [normalize_string(instruction.get_text()) for instruction in instructions] + [ + normalize_string(instruction.get_text(strip=True)) + for instruction in instructions_list + ] ) - def site_name(self): - return "L'Atelier de Roxane" + def _get_bloc_temps_value_by_index(self, index): + temps_div = self.soup.find("div", {"class": "bloc_temps bloc_blog"}) + infos_div = temps_div.find("div", {"class": "infos"}) + value_spans = infos_div.find_all("span", {"class": "valeur"}) + return value_spans[index].get_text(strip=True) - def get_bloc_temps_value_by_index(self, i): - div_bloc = self.soup.find("div", {"class": "bloc_temps bloc_blog"}) - div_infos = div_bloc.find("div", {"class": "infos"}) - span_value = div_infos.find_all("span", {"class": "valeur"}) - return span_value[i].get_text() + def _get_text_from_bloc(self, bloc_class, tag): + bloc_div = self.soup.find("div", {"class": f"{bloc_class} bloc_blog"}) + return bloc_div.find(tag).get_text(strip=True) diff --git a/tests/test_data/latelierderoxane.com/latelierderoxane.json b/tests/test_data/latelierderoxane.com/latelierderoxane_1.json similarity index 83% rename from tests/test_data/latelierderoxane.com/latelierderoxane.json rename to tests/test_data/latelierderoxane.com/latelierderoxane_1.json index 1645417e9..e57ea190c 100644 --- a/tests/test_data/latelierderoxane.com/latelierderoxane.json +++ b/tests/test_data/latelierderoxane.com/latelierderoxane_1.json @@ -1,6 +1,6 @@ { "canonical_url": "https://www.latelierderoxane.com/blog/recette-cake-marbre/", - "site_name": "L'Atelier de Roxane", + "site_name": "L'atelier de Roxane", "host": "latelierderoxane.com", "language": "fr-FR", "title": "Recette cake savane maison", @@ -16,13 +16,13 @@ ], "instructions_list": [ "Préchauffe le four à 165°.", - "Commence par fouetter les œufs et le sucre, à l’aide de ton robot ou batteur électrique, pendant 10 minutes : ton mélange doit s’éclaircir et doubler de volume !", + "Commence par fouetter les œufs et le sucre, à l’aide de ton robot oubatteur électrique, pendant 10 minutes : ton mélange doit s’éclaircir et doubler de volume !", "Ajoute le beurre fondu, la levure, la farine et fouette brièvement.", "Verse le lait et fouette jusqu’à l’obtention d’un mélange homogène.", "Sépare la préparation obtenue dans deux bols.", - "Dans un des deux bols, ajoute l’arôme ou la poudre de vanille.", + "Dans un des deux bols, ajoutel’arômeou la poudre de vanille.", "Fais fondre ton chocolat, au bain-marie ou au micro-ondes et incorpore-le dans le second bol à l’aide d’une maryse.", - "Récupère ton moule à cake et beurre-le généreusement.", + "Récupère tonmoule à cakeet beurre-le généreusement.", "Verse, dans le fond de ton moule, la moitié de la pâte à la vanille puis la moitié de celle au chocolat.", "Répète l’opération une deuxième fois.", "Enfourne pendant 45 min.", @@ -31,7 +31,7 @@ "À manger sans modération !" ], "yields": "6 servings", - "description": "Je pense que nous avons déjà tous acheté, au moins une fois, un cake type Savane au supermarché ! Aujourd’hui, je te propose une recette facile et rapide pour réaliser un délicieux marbré au chocolat, moelleux à souhait au bon goût de vanille et cacao ! Réalise cette recette et tu n’achèteras plus l’industriel ! Découvre ma box de pâtisserie goûters faits maison", + "description": "Je pense que nous avons déjà tous acheté, au moins une fois, un cake type Savane au supermarché ! Aujourd’hui, je te propose une recette facile et rapide pour réaliser un délicieux marbré au chocolat, moelleux à souhait au bon goût de vanille et cacao ! Réalise cette recette et tu n’achèteras plus l’industriel !", "total_time": 65, "cook_time": 45, "prep_time": 20, diff --git a/tests/test_data/latelierderoxane.com/latelierderoxane.testhtml b/tests/test_data/latelierderoxane.com/latelierderoxane_1.testhtml similarity index 54% rename from tests/test_data/latelierderoxane.com/latelierderoxane.testhtml rename to tests/test_data/latelierderoxane.com/latelierderoxane_1.testhtml index f017b5d2a..6dd744982 100644 --- a/tests/test_data/latelierderoxane.com/latelierderoxane.testhtml +++ b/tests/test_data/latelierderoxane.com/latelierderoxane_1.testhtml @@ -4,40 +4,50 @@
-