Skip to content

Commit

Permalink
Reapply "Language: consistency: retrieve dynamically from HTML where …
Browse files Browse the repository at this point in the history
…possible. (#1112)"

This reverts commit d12320f.
  • Loading branch information
jayaddison committed Jul 11, 2024
1 parent d368a68 commit 453b5c9
Show file tree
Hide file tree
Showing 7 changed files with 796 additions and 33 deletions.
9 changes: 7 additions & 2 deletions recipe_scrapers/_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from recipe_scrapers.settings import settings

from ._exceptions import ElementNotFoundInHtml
from ._grouping_utils import IngredientGroup
from ._schemaorg import SchemaOrg

Expand Down Expand Up @@ -117,7 +118,8 @@ def language(self):
"""Language the recipe is written in."""
candidate_languages = OrderedDict()
html = self.soup.find("html", {"lang": True})
candidate_languages[html.get("lang")] = True
if html:
candidate_languages[html.get("lang")] = True

# Deprecated: check for a meta http-equiv header
# See: https://www.w3.org/International/questions/qa-http-and-lang
Expand All @@ -138,7 +140,10 @@ def language(self):
candidate_languages.pop("en", None)

# Return the first candidate language
return candidate_languages.popitem(last=False)[0]
if candidate_languages:
return candidate_languages.popitem(last=False)[0]
else:
raise ElementNotFoundInHtml("Could not find language.")

def ingredients(self):
"""Ingredients of the recipe."""
Expand Down
2 changes: 1 addition & 1 deletion recipe_scrapers/kuchynalidla.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ def instructions_list(self):
return il

def language(self):
return "sk"
return super().language()
2 changes: 1 addition & 1 deletion recipe_scrapers/lekkerensimpel.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ def description(self):
return description["content"] if description else None

def language(self):
return "nl-NL"
return super().language()
3 changes: 1 addition & 2 deletions recipe_scrapers/woolworths.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import requests

from ._abstract import HEADERS, AbstractScraper
from ._exceptions import StaticValueException
from ._schemaorg import SchemaOrg
from ._utils import url_path_to_dict

Expand Down Expand Up @@ -40,7 +39,7 @@ def nutrients(self):
return self.schema.nutrients()

def language(self):
raise StaticValueException(return_value="en-AU")
return super().language()

def site_name(self):
return "Woolworths | Fresh Ideas For You"
757 changes: 756 additions & 1 deletion tests/legacy/test_data/woolworths.testhtml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions tests/legacy/test_data/woolworths.testjson

Large diffs are not rendered by default.

54 changes: 28 additions & 26 deletions tests/legacy/test_woolworths.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,80 +9,82 @@ class TestWoolworthsScraper(ScraperTest):

@classmethod
def expected_requests(cls):
yield GET, "https://www.woolworths.com.au/shop/recipes/asparagus-salad-with-lemon-vinaigrette", "tests/legacy/test_data/woolworths.testhtml"
yield GET, "https://foodhub.woolworths.com.au/content/woolworths-foodhub/en/asparagus-salad-with-lemon-vinaigrette.model.json", "tests/legacy/test_data/woolworths.testhtml"
yield GET, "https://www.woolworths.com.au/shop/recipes/smoothie-fruit-cubes", "tests/legacy/test_data/woolworths.testhtml"
yield GET, "https://foodhub.woolworths.com.au/content/woolworths-foodhub/en/smoothie-fruit-cubes.model.json", "tests/legacy/test_data/woolworths.testjson"

def test_host(self):
self.assertEqual("woolworths.com.au", self.harvester_class.host())

def test_canonical_url(self):
self.assertEqual(
"https://www.woolworths.com.au/shop/recipes/asparagus-salad-with-lemon-vinaigrette",
"https://www.woolworths.com.au/shop/recipes/smoothie-fruit-cubes",
self.harvester_class.canonical_url(),
)

def test_author(self):
self.assertEqual("Fresh Ideas", self.harvester_class.author())

def test_category(self):
self.assertEqual("Salads", self.harvester_class.category())
self.assertEqual("Drinks", self.harvester_class.category())

def test_title(self):
self.assertEqual(
"Asparagus Salad With Lemon Vinaigrette Recipe | Woolworths",
"Smoothie Fruit Cubes Recipe | Woolworths",
self.harvester_class.title(),
)

def test_total_time(self):
self.assertEqual(15, self.harvester_class.total_time())

def test_prep_time(self):
self.assertEqual(10, self.harvester_class.prep_time())
self.assertEqual(15, self.harvester_class.prep_time())

def test_yields(self):
self.assertEqual("4 servings", self.harvester_class.yields())
self.assertEqual("12 servings", self.harvester_class.yields())

def test_image(self):
self.assertEqual(
"https://foodhub.scene7.com/is/image/woolworthsltdprod/2010-asparagus-salad-with-lemon-vinaigrette:Square-1300x1300",
"https://foodhub.scene7.com/is/image/woolworthsltdprod/fi-2401-smoothie-fruit-cubes:Square-1300x1300",
self.harvester_class.image(),
)

def test_ingredients(self):
self.assertEqual(
[
"2 bunches asparagus, trimmed, halved diagonally",
"100g snow peas, trimmed, halved diagonally",
"2 gem lettuce, trimmed, leaves separated",
"2 radishes, trimmed, thinly sliced into rounds",
"80g feta, crumbled",
"0.5 cup small basil leaves",
"2 tbs natural sliced almonds, toasted",
"1 tbs white wine vinegar",
"2 tsp Dijon mustard",
"0.25 tsp sea salt flakes",
"0.25 tsp caster sugar",
"0.25 cup extra virgin olive oil",
"300g fruit of choice (see tip)",
],
self.harvester_class.ingredients(),
)

def test_instructions(self):
self.assertEqual(
"""Bring a medium saucepan of water to the boil over high heat. Cook asparagus and snow peas for 2 minutes or until just tender. Drain. Refresh under cold water. Drain.
Meanwhile, to make the vinaigrette, whisk all the ingredients together in a small jug until combined, then season.
Arrange lettuce, asparagus, snow peas and radish on a platter. Scatter over feta, drizzle with dressing, top with basil and almonds, then serve.""",
(
"Place fruit in a blender and blitz until smooth. Transfer to a small jug.\n"
"Pour mixture equally into a 12-hole (1 tbs-/20ml-capacity) ice-cube tray and freeze for 6 hours or overnight until set.\n"
"Transfer cubes to an airtight container and store in the freezer until required. (Once ready to use, place cubes in a blender with yoghurt and milk, then blitz until smooth.)"
),
self.harvester_class.instructions(),
)

def test_cuisine(self):
self.assertEqual("French", self.harvester_class.cuisine())
self.assertEqual("Australian", self.harvester_class.cuisine())

def test_cook_time(self):
self.assertEqual(5, self.harvester_class.cook_time())
self.assertEqual(None, self.harvester_class.cook_time())

def test_nutrients(self):
self.assertEqual({}, self.harvester_class.nutrients())
self.assertEqual(
{
"calories": "6 calories",
"carbohydrateContent": "1.1 grams",
"fatContent": "0.1 grams",
"fiberContent": "0.2 grams",
"proteinContent": "0.2 grams",
"sodiumContent": "10 milligrams",
"sugarContent": "1.1 grams",
},
self.harvester_class.nutrients(),
)

def test_language(self):
self.assertEqual("en-AU", self.harvester_class.language())
Expand Down

0 comments on commit 453b5c9

Please sign in to comment.