Skip to content

Commit

Permalink
modernproper: removes nutrient titles from output (#1304)
Browse files Browse the repository at this point in the history
Co-authored-by: James Addison <55152140+jayaddison@users.noreply.github.com>
  • Loading branch information
jknndy and jayaddison authored Oct 18, 2024
1 parent 097c601 commit bc8cb72
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
28 changes: 28 additions & 0 deletions recipe_scrapers/themodernproper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
from ._abstract import AbstractScraper
from ._exceptions import ElementNotFoundInHtml


class TheModernProper(AbstractScraper):
@classmethod
def host(cls):
return "themodernproper.com"

def nutrients(self):
container = self.schema.nutrients()
if not container:
raise ElementNotFoundInHtml("Could not find nutritional info container")

results = {}
nutrient_mapping = {
"calories": "calories",
"fatContent": "fat",
"saturatedFatContent": "saturated fat",
"carbohydrateContent": "carbohydrates",
"sugarContent": "sugar",
"proteinContent": "protein",
"sodiumContent": "sodium",
"fiberContent": "fiber",
"cholesterolContent": "cholesterol",
}

for key, value in container.items():
if key not in nutrient_mapping:
continue
redundant_suffix = nutrient_mapping[key]
if value.endswith(redundant_suffix):
results[key] = value.removesuffix(redundant_suffix).strip()

return results
18 changes: 9 additions & 9 deletions tests/test_data/themodernproper.com/themodernproper.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@
"ratings": 5.0,
"ratings_count": 82,
"nutrients": {
"calories": "463 calories",
"fatContent": "35 grams fat",
"saturatedFatContent": "15 grams saturated fat",
"carbohydrateContent": "11 grams carbohydrates",
"sugarContent": "1 grams sugar",
"proteinContent": "27 grams protein",
"sodiumContent": "1050 milligrams sodium",
"fiberContent": "1 grams fiber",
"cholesterolContent": "157 milligrams cholesterol"
"calories": "463",
"fatContent": "35 grams",
"saturatedFatContent": "15 grams",
"carbohydrateContent": "11 grams",
"sugarContent": "1 grams",
"proteinContent": "27 grams",
"sodiumContent": "1050 milligrams",
"fiberContent": "1 grams",
"cholesterolContent": "157 milligrams"
},
"image": "https://images.themodernproper.com/billowy-turkey/production/posts/2018/swedish-meatballs-13.jpg?w=960&h=540&q=82&fm=jpg&fit=crop&dm=1599768329&s=a12d3adcc54b1d30b5aa25dc6b922e3f",
"keywords": [
Expand Down

0 comments on commit bc8cb72

Please sign in to comment.