Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New test and refactor for Rewe #1469

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 13 additions & 53 deletions recipe_scrapers/rewe.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,32 @@
import ast
import re

from ._abstract import AbstractScraper
from ._exceptions import StaticValueException
from ._utils import get_minutes, normalize_string
from ._utils import normalize_string


class Rewe(AbstractScraper):
@classmethod
def host(cls):
return "rewe.de"

def language(self):
return self.soup.find("html")["lang"]

def author(self):
return self.schema.author()

def title(self):
return self.soup.find("h1", {"class": "ld-rds"}).get_text()

def category(self):
category = self.soup.find(
"a", {"id": "nutrition-information-mobile"}
).get_text()
return normalize_string(category)

def total_time(self):
x_data = self.soup.select_one(
"#recipe-basics>.ld-rds:first-child:first-child div:last-child span:first-child"
).get("x-data")

# Transform the `x-data` string into a dictionary
data_dict = ast.literal_eval(
re.sub(r"(\w+):", r"'\1':", x_data.replace("`", "'"))
)

# Return the "displayText" field
return get_minutes(data_dict["displayText"])

def yields(self):
return self.soup.find("span", {"x-text": "currentServings"}).get_text().strip()

def ingredients(self):
ingredient_list = self.soup.find("ul", {"id": "ingredient_list"})
list_items = ingredient_list.find_all("li", {"class": "ingredient_list_item"})
ingredients = []

for item in list_items:
amount = normalize_string(
item.find("div", class_="formattedAmountDiv").get_text()
)
ingredient = item.find(
"span", {"class": "ld-rds break-words leading-6"}
).get_text(" ", strip=True)
ingredients.append(
normalize_string(f"{amount + ' ' if amount != '0' else ''}{ingredient}")
amount_div = item.find("div", {"class": "formattedAmountDiv"})
amount = (
normalize_string(amount_div.get_text(strip=True)) if amount_div else ""
)

name_span = item.find("span", {"class": "break-words"})
name = normalize_string(name_span.get_text(strip=True)) if name_span else ""

if amount == "0":
ingredients.append(name)
else:
ingredient_text = f"{amount} {name}".strip()
ingredients.append(ingredient_text)

return ingredients

def instructions(self):
Expand All @@ -75,12 +44,3 @@ def instructions(self):
steps.append(instruction.get_text().strip())

return "\n".join(steps)

def description(self):
return self.soup.find("meta", {"property": "og:description"})["content"]

def canonical_url(self):
return self.soup.find("meta", {"property": "og:url"})["content"]

def site_name(self):
raise StaticValueException(return_value="REWE")
6 changes: 3 additions & 3 deletions tests/test_data/rewe.de/rewe.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Anne Lippert",
"canonical_url": "https://www.rewe.de/rezepte/wintergemuese-gnocchi-pfanne-kraeuterdip/",
"site_name": "REWE",
"site_name": "Rewe.de",
"host": "rewe.de",
"language": "de",
"title": "Wintergemüse-Gnocchi-Pfanne mit Kräuterdip",
Expand Down Expand Up @@ -32,8 +32,8 @@
"2 EL Öl in einer großen Pfanne erhitzen, das Gemüse portionsweise, außer der roten Bete, unter Rühren 5 Minuten garen, sodass es gar ist aber noch leichten Biss hat. Mit Salz, Pfeffer und Muskatnuss würzen.",
"Gemüse aus der Pfanne nehmen und Gnocchi im restlichen Öl kurz goldbraun braten. Dann das Gemüse, inklusive Rote Bete, wieder zugeben, mischen und nochmals mit Salz und Pfeffer würzen. Mit Walnüssen und Dip servieren."
],
"category": "Vegetarisch",
"yields": "4",
"category": "Abendessen, Hauptspeise, Mittagessen, Kindergerichte, Pfannengericht",
"yields": "4 servings",
"description": "Wir treiben es gerne bunt! Starte auch du mit einer ausgewogenen Familien-Mahlzeit durch. Hol dir das REWE Rezept für Wintergemüse-Gnocchi-Pfanne & Kräuterdip.",
"total_time": 45,
"ratings": 4.69,
Expand Down
Loading