Skip to content

Commit

Permalink
Add better nutrition parsing for cookwell.com (#1459)
Browse files Browse the repository at this point in the history
* add better nutrition parsing for cookwell.com

* update tests

* linting

* Apply suggestions from code review

Co-authored-by: Joey <[email protected]>

* remove unused variable

---------

Co-authored-by: Joey <[email protected]>
  • Loading branch information
eric-hoffmann and jknndy authored Jan 14, 2025
1 parent a5937f7 commit f77fbd3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
20 changes: 20 additions & 0 deletions recipe_scrapers/cookwell.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
from ._abstract import AbstractScraper
import re


class CookWell(AbstractScraper):
@classmethod
def host(cls):
return "cookwell.com"

def nutrients(self):
nutrition_pattern = re.compile(
r'{\\"calories\\":(?P<calories>\d+),\\"carbohydrates\\":(?P<carbohydrates>\d+),\\"fat\\":(?P<fat>\d+),\\"protein\\":(?P<protein>\d+)}'
)

nutrition_script = self.soup.find("script", string=nutrition_pattern)
if nutrition_script:
match = nutrition_pattern.search(nutrition_script.string)
if match:
nutrition_info = match.groupdict()
return {
"calories": nutrition_info["calories"],
"carbohydrateContent": f'{nutrition_info["carbohydrates"]} g',
"fatContent": f'{nutrition_info["fat"]} g',
"proteinContent": f'{nutrition_info["protein"]} g',
}

return self.schema.nutrients()
5 changes: 4 additions & 1 deletion tests/test_data/cookwell.com/cookwell_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
"total_time": 75,
"cuisine": "American",
"nutrients": {
"calories": "970 calories"
"calories": "970",
"fatContent": "60 g",
"carbohydrateContent": "100 g",
"proteinContent": "12 g"
},
"image": "https://cdn.sanity.io/images/g1s4qnmz/production/ba9d3d0d5ff9a0f9f17f8e11df7e93b7f3806a48-1000x1000.jpg",
"keywords": [
Expand Down
5 changes: 4 additions & 1 deletion tests/test_data/cookwell.com/cookwell_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
"total_time": 30,
"cuisine": "Middle Eastern",
"nutrients": {
"calories": "2558 calories"
"calories": "2558",
"fatContent": "49 g",
"carbohydrateContent": "402 g",
"proteinContent": "132 g"
},
"image": "https://cdn.sanity.io/images/g1s4qnmz/production/3b2a77f0f746cf90c4f7f49a92b39885ada6e9de-3543x3543.jpg",
"keywords": [
Expand Down

0 comments on commit f77fbd3

Please sign in to comment.