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

Add lookup for ratings in schema.org 'aggregateRating' entities listed on page #913

Merged
merged 2 commits into from
Oct 25, 2023
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
33 changes: 23 additions & 10 deletions recipe_scrapers/_schemaorg.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def __init__(self, page_data, raw=False):
self.format = "raw"
self.data = page_data
self.people = {}
self.ratingsdata = {}
return
self.format = None
self.data = {}
self.people = {}
self.ratingsdata = {}

data = extruct.extract(
page_data,
Expand All @@ -57,6 +59,16 @@ def __init__(self, page_data, raw=False):
if key:
self.people[key] = person

# Extract ratings data
for syntax in SYNTAXES:
syntax_data = data.get(syntax, [])
for item in syntax_data:
rating = self._find_entity(item, "AggregateRating")
if rating:
Comment on lines +66 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point we could optionally reduce the filesize by a few lines by using the walrus operator here -- it looks like I missed one of out two opporunties to do that previously here. It should be safe to do that because this library requires Python 3.8 or greater.

rating_id = rating.get("@id")
if rating_id:
self.ratingsdata[rating_id] = rating

for syntax in SYNTAXES:
# make sure entries of type Recipe are always parsed first
syntax_data = data.get(syntax, [])
Expand Down Expand Up @@ -247,17 +259,18 @@ def instructions(self):
return instructions

def ratings(self):
ratings = self.data.get("aggregateRating")
if ratings is None:
raise SchemaOrgException("No ratings data in SchemaOrg.")

if isinstance(ratings, dict):
ratings = self.data.get("aggregateRating") or self._find_entity(
self.data, "AggregateRating"
)
if ratings and isinstance(ratings, dict):
rating_id = ratings.get("@id")
if rating_id and rating_id in self.ratingsdata:
ratings = self.ratingsdata[rating_id]
if ratings and isinstance(ratings, dict):
ratings = ratings.get("ratingValue")

if ratings is None:
raise SchemaOrgException("No ratingValue in SchemaOrg.")

return round(float(ratings), 2)
if ratings:
return round(float(ratings), 2)
raise SchemaOrgException("No ratingValue in SchemaOrg.")

def cuisine(self):
cuisine = self.data.get("recipeCuisine")
Expand Down
5 changes: 2 additions & 3 deletions recipe_scrapers/maangchi.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ def image(self):
return self.schema.image()

def ingredients(self):
before = self.soup.find("h4", string="Directions").find_all_previous("li")
after = self.soup.find("h4", string="Ingredients:").find_all_next("li")
before = self.soup.find("h2", string="Ingredients").find_all_next("li")
after = self.soup.find("h2", string="Directions").find_all_previous("li")
list_before = [normalize_string(b.get_text()) for b in before]
list_after = [normalize_string(a.get_text()) for a in after]
list_before.reverse()
return [x for x in list_before if x in list_after]

def instructions(self):
Expand Down
Loading