Skip to content

Commit

Permalink
improve nutrients output
Browse files Browse the repository at this point in the history
  • Loading branch information
jknndy committed Oct 2, 2024
1 parent 71a5c3c commit acfb0f5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
45 changes: 35 additions & 10 deletions recipe_scrapers/barefootinthepines.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,41 @@ def ingredient_groups(self):
)

def nutrients(self):
nutrition_container = self.soup.select_one(".mv-create-nutrition-box")
if not nutrition_container:
nutrition_section = self.soup.select_one(".mv-create-nutrition-box")
if not nutrition_section:
return None

nutrition_data = {}
for item in nutrition_container.select(".mv-create-nutrition-item"):
label = item.select_one(".mv-create-nutrition-label")
if label:
label_text = label.get_text(strip=True)
quantity = item.get_text(strip=True).replace(label_text, "").strip()
nutrition_data[label_text] = quantity
raw_nutrition_data = {
item.select_one(".mv-create-nutrition-label")
.get_text(strip=True)
.lower(): item.get_text(strip=True)
.replace(
item.select_one(".mv-create-nutrition-label").get_text(strip=True), ""
)
.strip()
for item in nutrition_section.select(".mv-create-nutrition-item")
if item.select_one(".mv-create-nutrition-label")
}

return nutrition_data
nutrition_label_mapping = {
"calories:": "calories",
"carbohydrates:": "carbohydrateContent",
"cholesterol:": "cholesterolContent",
"total fat:": "fatContent",
"fiber:": "fiberContent",
"protein:": "proteinContent",
"saturated fat:": "saturatedFatContent",
"serving size:": "servingSize",
"sodium:": "sodiumContent",
"sugar:": "sugarContent",
"trans fat:": "transFatContent",
"unsaturated fat:": "unsaturatedFatContent",
}

standardized_nutrition_data = {
schema_label: raw_nutrition_data[custom_label.lower()]
for custom_label, schema_label in nutrition_label_mapping.items()
if custom_label.lower() in raw_nutrition_data
}

return standardized_nutrition_data
25 changes: 12 additions & 13 deletions tests/test_data/barefootinthepines.com/barefootinthepines_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,18 @@
"prep_time": 10,
"cuisine": "Side Dishes",
"nutrients": {
"Yield:": "4",
"Serving Size:": "1",
"Calories:": "139",
"Total Fat:": "7g",
"Saturated Fat:": "2g",
"Trans Fat:": "0g",
"Unsaturated Fat:": "5g",
"Cholesterol:": "9mg",
"Sodium:": "334mg",
"Carbohydrates:": "15g",
"Fiber:": "3g",
"Sugar:": "8g",
"Protein:": "6g"
"servingSize": "1",
"calories": "139",
"fatContent": "7g",
"saturatedFatContent": "2g",
"unsaturatedFatContent": "5g",
"transFatContent": "0g",
"carbohydrateContent": "15g",
"sugarContent": "8g",
"proteinContent": "6g",
"sodiumContent": "334mg",
"fiberContent": "3g",
"cholesterolContent": "9mg"
},
"image": "https://barefootinthepines.com/wp-content/uploads/2024/09/Maple-Bacon-Brussels-Sprouts-4-720x720.jpg",
"keywords": [
Expand Down
25 changes: 12 additions & 13 deletions tests/test_data/barefootinthepines.com/barefootinthepines_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,18 @@
"prep_time": 25,
"cuisine": "Thanksgiving Desserts",
"nutrients": {
"Yield:": "12",
"Serving Size:": "1 Slice",
"Calories:": "434",
"Total Fat:": "30g",
"Saturated Fat:": "17g",
"Trans Fat:": "0g",
"Unsaturated Fat:": "11g",
"Cholesterol:": "129mg",
"Sodium:": "131mg",
"Carbohydrates:": "41g",
"Fiber:": "2g",
"Sugar:": "30g",
"Protein:": "6g"
"servingSize": "1 Slice",
"calories": "434",
"fatContent": "30g",
"saturatedFatContent": "17g",
"unsaturatedFatContent": "11g",
"transFatContent": "0g",
"carbohydrateContent": "41g",
"sugarContent": "30g",
"proteinContent": "6g",
"sodiumContent": "131mg",
"fiberContent": "2g",
"cholesterolContent": "129mg"
},
"image": "https://barefootinthepines.com/wp-content/uploads/2024/09/Chocolate-Pie-With-Graham-Cracker-Crust-7-720x720.jpg",
"keywords": [
Expand Down

0 comments on commit acfb0f5

Please sign in to comment.