From e45440324a57ed55ec373aa725431d347a05a967 Mon Sep 17 00:00:00 2001 From: Joey <7505194+jknndy@users.noreply.github.com> Date: Fri, 27 Dec 2024 11:28:04 -0500 Subject: [PATCH] Refactor (#1445) --- recipe_scrapers/nhshealthierfamilies.py | 52 +-- .../nhs.uk/nhshealthierfamilies_1.json | 7 +- .../nhs.uk/nhshealthierfamilies_1.testhtml | 351 ++++++++--------- .../nhs.uk/nhshealthierfamilies_2.json | 7 +- .../nhs.uk/nhshealthierfamilies_2.testhtml | 353 +++++++++--------- 5 files changed, 375 insertions(+), 395 deletions(-) diff --git a/recipe_scrapers/nhshealthierfamilies.py b/recipe_scrapers/nhshealthierfamilies.py index 300a9d4c5..daaea329b 100644 --- a/recipe_scrapers/nhshealthierfamilies.py +++ b/recipe_scrapers/nhshealthierfamilies.py @@ -19,48 +19,28 @@ def title(self): title = title[:-7] return title - def _get_recipe_metadata(self): + def _get_recipe_content(self): container = self.soup.find("div", {"class": "bh-recipe__description"}) descriptions = container.findAll("p") - content = "".join([description.get_text() for description in descriptions]) + return "".join([description.get_text() for description in descriptions]) + + def prep_time(self): + content = self._get_recipe_content() prep_time = re.search(r"Prep: (\d+) mins", content) + return get_minutes(prep_time.group(0)) if prep_time else 0 + + def cook_time(self): + content = self._get_recipe_content() cook_time = re.search(r"Cook: (\d+) mins", content) - recipe_yields = re.search(r"Serves (\d+)", content) - return { - "prep_time": get_minutes(prep_time.group(0)) if prep_time else None, - "cook_time": get_minutes(cook_time.group(0)) if cook_time else None, - "yields": get_yields(recipe_yields.group(0)) if recipe_yields else None, - } + return get_minutes(cook_time.group(0)) if cook_time else 0 def total_time(self): - metadata = self._get_recipe_metadata() - return metadata["prep_time"] + metadata["cook_time"] + return self.prep_time() + self.cook_time() def yields(self): - metadata = self._get_recipe_metadata() - return metadata["yields"] - - def image(self): - return self.soup.find("img", {"class": "nhsuk-image__img"})["src"] - - def ingredients(self): - ingredients = [] - instructions_div = self.soup.find("div", {"class": "bh-recipe-instructions"}) - ul = instructions_div.find("ul") - - if ul: - for li in ul.findAll("li"): - ingredients.append(normalize_string(li.get_text())) - - # Stop when encountering an 'ol' element which is where instructions are stored. - for sibling in ul.find_next_siblings(): - if sibling.name == "ol": - break - if sibling.name == "ul": - for li in sibling.findAll("li"): - ingredients.append(normalize_string(li.get_text())) - - return ingredients + content = self._get_recipe_content() + recipe_yields = re.search(r"Serves (\d+)", content) + return get_yields(recipe_yields.group(0)) if recipe_yields else None def ingredient_groups(self): return group_ingredients( @@ -82,7 +62,3 @@ def instructions(self): return "\n".join( [normalize_string(instruction) for instruction in instructions] ) - - def description(self): - description_meta = self.soup.find("meta", {"name": "description"}) - return normalize_string(description_meta["content"]) diff --git a/tests/test_data/nhs.uk/nhshealthierfamilies_1.json b/tests/test_data/nhs.uk/nhshealthierfamilies_1.json index 5213e4e12..99a1944ff 100644 --- a/tests/test_data/nhs.uk/nhshealthierfamilies_1.json +++ b/tests/test_data/nhs.uk/nhshealthierfamilies_1.json @@ -26,9 +26,10 @@ "Meanwhile, cook the rice according to pack instructions.", "Season the chilli with pepper and serve with the boiled rice." ], - "category": null, "yields": "4 servings", - "description": "This classic chilli is packed with flavour. It also freezes well, so is perfect for batch-cooking.", + "description": "This classic chilli is packed with flavour. It also freezes well, so is perfect to batch-cook.", "total_time": 35, - "image": "https://assets.nhs.uk/campaigns-cms-prod/images/Chilli-con-carne_x7m8d91.width-320.jpg" + "cook_time": 25, + "prep_time": 10, + "image": "https://digitalcampaignsstorage.blob.core.windows.net/campaigns-cms-prod/images/Chilli-con-carne_x7m8d91.width-320.jpg" } diff --git a/tests/test_data/nhs.uk/nhshealthierfamilies_1.testhtml b/tests/test_data/nhs.uk/nhshealthierfamilies_1.testhtml index ae6efa5ae..debae96c2 100644 --- a/tests/test_data/nhs.uk/nhshealthierfamilies_1.testhtml +++ b/tests/test_data/nhs.uk/nhshealthierfamilies_1.testhtml @@ -6,7 +6,6 @@ - @@ -22,18 +21,18 @@ Easy Chilli Con Carne - Recipes - Healthier Families - NHS - + + + - - - - - + + + @@ -53,18 +52,18 @@ + + - - - + - + @@ -75,6 +74,8 @@ + + @@ -107,23 +108,23 @@ window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); - + gtag('config', 'G-1K4S0MRD2T'); - + - + Skip to main content - - + + @@ -149,114 +150,114 @@ + - - + + + + - - - - - - + + + - +
- - - - - - + + + + + +
- +
- - + +
- - - + + + @@ -273,8 +274,8 @@

Chilli con carne recipe

This classic chilli is packed with flavour. It also freezes well, so is perfect to batch-cook.

Prep: 10 mins
Cook: 25 mins
Serves 4

- - + +
@@ -282,34 +283,34 @@
- - + +

Per serving:

  • 2,042kJ / 488kcals
  • 30g protein
  • 5g fat, of which 1.5g saturates
  • 82g carbohydrate, of which 12g sugars
  • 7g fibre
  • 0.9g salt
- - + +
- +
Chilli made with beef, mushrooms, peppers and kidney beans served with rice @@ -320,58 +321,58 @@
- +

Ingredients

  • 300g extra-lean minced beef
  • 1 large onion, finely chopped
  • 2 garlic cloves, finely chopped
  • 400g chopped tomatoes
  • 2 tablespoons tomato purée
  • 2 teaspoons chilli powder
  • 1 teaspoon ground cumin
  • 1 red pepper, deseeded and chopped
  • 2 handfuls of cup or button mushrooms, sliced
  • 410g red kidney beans, drained
  • 150ml reduced-salt vegetable or chicken stock
  • 300g easy-cook white or brown rice
  • 1 pinch ground black pepper
- +

Method

    - - + +
  1. - +

    Heat a large saucepan and add the minced beef, a handful at a time, cooking it until browned. Add the onion and garlic, then cook for another 2 to 3 minutes.

    - +
    Information:

    Turkey mince also makes an excellent chilli – and it's lower in fat too. If you want to keep things vegetarian, substitute the minced beef with vegetarian mince, or try our tasty veggie chilli recipe.

    - +
  2. - - + +
  3. - +

    Add the chopped tomatoes, tomato purée, spices, red pepper, mushrooms, kidney beans and stock. Stir well, bring to the boil, then lower the heat and simmer gently for 15 to 20 minutes.

    - +
  4. - - + +
  5. - +

    Meanwhile, cook the rice according to pack instructions.

    - +
  6. - - + +
  7. - +

    Season the chilli with pepper and serve with the boiled rice.

    - +
    Information:

    Chilli makes a really versatile topping or filling, and is also great for reheating leftovers. So instead of rice, try serving with baked potatoes, topped with a spoonful of low-fat plain yoghurt and some chopped cucumber and tomatoes – or in a wholewheat wrap.

    - +
  8. - +
@@ -383,128 +384,128 @@ "@context": "https://schema.org", "@type": "Recipe", "description": "This classic chilli is packed with flavour. It also freezes well, so is perfect to batch-cook.", - "image": "https://assets.nhs.uk/campaigns-cms-prod/images/Chilli-con-carne_x7m8d91.width-320.jpg", + "image": "https://digitalcampaignsstorage.blob.core.windows.net/campaigns-cms-prod/images/Chilli-con-carne_x7m8d91.width-320.jpg", "recipeIngredient": ["300g extra-lean minced beef", "1 large onion, finely chopped", "2 garlic cloves, finely chopped", "400g chopped tomatoes", "2 tablespoons tomato pur\u00e9e", "2 teaspoons chilli powder", "1 teaspoon ground cumin", "1 red pepper, deseeded and chopped", "2 handfuls of cup or button mushrooms, sliced", "410g red kidney beans, drained", "150ml reduced-salt vegetable or chicken stock", "300g easy-cook white or brown rice", "1 pinch ground black pepper"], "name": "Chilli con carne recipe", "recipeInstructions": "Heat a large saucepan and add the minced beef, a handful at a time, cooking it until browned. Add the onion and garlic, then cook for another 2 to 3 minutes.Add the chopped tomatoes, tomato purée, spices, red pepper, mushrooms, kidney beans and stock. Stir well, bring to the boil, then lower the heat and simmer gently for 15 to 20 minutes.Meanwhile, cook the rice according to pack instructions.Season the chilli with pepper and serve with the boiled rice." } - - + +
- +
- - + +
- - + +
- +
- - + +
- - - + + + @@ -518,61 +519,61 @@
- +

- + Sign up for Healthy Steps emails - +

- - + +

Want the recipe for a healthier family? Join today and over 8 weeks you'll get easy tips from nutrition experts and parents, healthy swaps and tasty recipes on a budget.

- +
- - + +
- +
- - - + + +
- + + - - - - + + + diff --git a/tests/test_data/nhs.uk/nhshealthierfamilies_2.json b/tests/test_data/nhs.uk/nhshealthierfamilies_2.json index 74c8b6379..9313f11e6 100644 --- a/tests/test_data/nhs.uk/nhshealthierfamilies_2.json +++ b/tests/test_data/nhs.uk/nhshealthierfamilies_2.json @@ -39,9 +39,10 @@ "Meanwhile, sprinkle the breadcrumbs onto a large plate. Season with a little pepper. Dip each fish fillet in the beaten egg, then coat in the breadcrumbs. Place on the baking sheet, then transfer to the oven when you turn the potatoes, so that it cooks for 15 to 20 minutes. To check that the fish is cooked, it should flake easily when tested with a fork.", "Heat the mushy peas in a saucepan, then serve with the fish and chips." ], - "category": null, "yields": "4 servings", - "description": "Try our recipe for healthier, homemade fish and chips! Click to read the ingredients and a step-by-step guide to making it", + "description": "Make your own healthier version of fish and chips at home!", "total_time": 50, - "image": "https://assets.nhs.uk/campaigns-cms-prod/images/Recipes-square-healthy-fish-and-chips.width-320.jpg" + "cook_time": 40, + "prep_time": 10, + "image": "https://digitalcampaignsstorage.blob.core.windows.net/campaigns-cms-prod/images/Recipes-square-healthy-fish-and-chips.width-320.jpg" } diff --git a/tests/test_data/nhs.uk/nhshealthierfamilies_2.testhtml b/tests/test_data/nhs.uk/nhshealthierfamilies_2.testhtml index 946d1a8c0..448b212f4 100644 --- a/tests/test_data/nhs.uk/nhshealthierfamilies_2.testhtml +++ b/tests/test_data/nhs.uk/nhshealthierfamilies_2.testhtml @@ -6,7 +6,6 @@ - @@ -22,18 +21,18 @@ Healthy homemade fish and chips - Recipes - Healthier Families - NHS - + + + - - - - - + + + @@ -53,18 +52,18 @@ + + - - - + - + @@ -75,6 +74,8 @@ + + @@ -107,23 +108,23 @@ window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); - + gtag('config', 'G-1K4S0MRD2T'); - + - + Skip to main content - - + + @@ -149,114 +150,114 @@ + - - + + + + - - - - - - + + + - +
- - - - - - + + + + + +
- +
- - + +
- - - + + + @@ -273,8 +274,8 @@

Good old fish and chips recipe

Make your own healthier version of fish and chips at home!

Prep: 10 mins
Cook: 40 mins
Serves 4

- - + +
@@ -282,34 +283,34 @@
- - + +

Per serving:

  • 1,682kJ / 402kcal
  • 34g protein
  • 56g carbohydrate of which 3g sugars
  • 6g fat of which 1g saturates
  • 4g fibre
  • 1g salt
- - + +
- +
Homebaked chips and fillets of fish with breadcrumbs @@ -320,58 +321,58 @@
- -

Ingredients

  • 4 potatoes, scrubbed, each cut into 8 wedges
  • 1 tablespoon vegetable oil
  • 75g dried white or wholemeal breadcrumbs
  • 1 egg, beaten with 2 tbsp cold water
  • 4 fillets skinless white fish, like haddock, cod or pollock

Swappable or optional

  • 300g mushy peas
  • 1 pinch ground black pepper (optional)

Swap tip

You can use any frozen, fresh or canned peas if you don't have mushy ones.

- + +

Ingredients

  • 4 potatoes, scrubbed, each cut into 8 wedges
  • 1 tablespoon vegetable oil
  • 75g dried white or wholemeal breadcrumbs
  • 1 egg, beaten with 2 tbsp cold water
  • 4 fillets skinless white fish, like haddock, cod or pollock

Swappable or optional

  • 300g mushy peas
  • 1 pinch ground black pepper (optional)

Swap tip

You can use any frozen, fresh or canned peas if you don't have mushy ones.

+

Method

    - - + +
  1. - +

    Preheat the oven to 200C (fan 180C, gas mark 6). Lightly grease a baking sheet with a little vegetable oil.

    - +
  2. - - + +
  3. - +

    Put the potato wedges into a roasting tin. Add the remaining vegetable oil and toss to coat. Season with black pepper. Transfer to the oven to bake for 35 to 40 minutes, turning them over after 20 minutes.

    - +
    Information:

    Not peeling the potatoes means you get more fibre in your diet – and they're quicker to prepare.

    - +
  4. - - + +
  5. - +

    Meanwhile, sprinkle the breadcrumbs onto a large plate. Season with a little pepper. Dip each fish fillet in the beaten egg, then coat in the breadcrumbs. Place on the baking sheet, then transfer to the oven when you turn the potatoes, so that it cooks for 15 to 20 minutes. To check that the fish is cooked, it should flake easily when tested with a fork.

    - +
    Information:

    Look out for dried breadcrumbs flavoured with lemon or spices to add extra flavour to the fish, or add some finely grated lemon zest or a pinch of paprika to plain dried breadcrumbs.

    - +
  6. - - + +
  7. - +

    Heat the mushy peas in a saucepan, then serve with the fish and chips.

    - +
  8. - +
@@ -383,128 +384,128 @@ "@context": "https://schema.org", "@type": "Recipe", "description": "Make your own healthier version of fish and chips at home!", - "image": "https://assets.nhs.uk/campaigns-cms-prod/images/Recipes-square-healthy-fish-and-chips.width-320.jpg", + "image": "https://digitalcampaignsstorage.blob.core.windows.net/campaigns-cms-prod/images/Recipes-square-healthy-fish-and-chips.width-320.jpg", "recipeIngredient": ["4 potatoes, scrubbed, each cut into 8 wedges", "1 tablespoon vegetable oil", "75g dried white or wholemeal breadcrumbs", "1 egg, beaten with 2 tbsp cold water", "4 fillets skinless white fish, like haddock, cod or pollock", "300g mushy peas", "1 pinch\u00a0ground black pepper (optional)"], "name": "Good old fish and chips recipe", "recipeInstructions": "Preheat the oven to 200C (fan 180C, gas mark 6). Lightly grease a baking sheet with a little vegetable oil.Put the potato wedges into a roasting tin. Add the remaining vegetable oil and toss to coat. Season with black pepper. Transfer to the oven to bake for 35 to 40 minutes, turning them over after 20 minutes.Meanwhile, sprinkle the breadcrumbs onto a large plate. Season with a little pepper. Dip each fish fillet in the beaten egg, then coat in the breadcrumbs. Place on the baking sheet, then transfer to the oven when you turn the potatoes, so that it cooks for 15 to 20 minutes. To check that the fish is cooked, it should flake easily when tested with a fork.Heat the mushy peas in a saucepan, then serve with the fish and chips." } - - + +
- +
- - + +
- - + +
- +
- - + +
- - - + + + @@ -518,61 +519,61 @@
- +

- + Sign up for Healthy Steps emails - +

- - + +

Want the recipe for a healthier family? Join today and over 8 weeks you'll get easy tips from nutrition experts and parents, healthy swaps and tasty recipes on a budget.

- +
- - + +
- +
- - - + + +
- + + - - - - + + +