Skip to content

Commit

Permalink
Updates get_yields util functionality to handle additional metrics (#963
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jknndy authored Dec 10, 2023
1 parent ac101d7 commit 253013a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 12 deletions.
44 changes: 38 additions & 6 deletions recipe_scrapers/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@

SERVE_REGEX_TO = re.compile(r"\d+(\s+to\s+|-)\d+", flags=re.I | re.X)

RECIPE_YIELD_TYPES = (
("dozen", "dozens"),
("batch", "batches"),
("cake", "cakes"),
("sandwich", "sandwiches"),
("bun", "buns"),
("cookie", "cookies"),
("muffin", "muffins"),
("cupcake", "cupcakes"),
("loaf", "loaves"),
("pie", "pies"),
("cup", "cups"),
("pint", "pints"),
("gallon", "gallons"),
("ounce", "ounces"),
("pound", "pounds"),
("gram", "grams"),
("liter", "liters"),
("piece", "pieces"),
("layer", "layers"),
("scoop", "scoops"),
("bar", "bars"),
("patty", "patties"),
# ... add more types as needed, in (singular, plural) format ...
)


def get_minutes(element, return_zero_on_not_found=False): # noqa: C901: TODO
if element is None:
Expand Down Expand Up @@ -114,12 +140,16 @@ def get_yields(element):
"""
Will return a string of servings or items, if the recipe is for number of items and not servings
the method will return the string "x item(s)" where x is the quantity.
Returns a string of servings or items. If the recipe is for a number of items (not servings),
it returns "x item(s)" where x is the quantity. This function handles cases where the yield is in dozens,
such as "4 dozen cookies", returning "4 dozen" instead of "4 servings". Additionally
accommodates yields specified in batches (e.g., "2 batches of brownies"), returning the yield as stated.
:param element: Should be BeautifulSoup.TAG, in some cases not feasible and will then be text.
:return: The number of servings or items.
:return: The number of servings, items, dozen, batches, etc...
"""
if element is None:
raise ElementNotFoundInHtml(element)

if isinstance(element, str):
serve_text = element
else:
Expand All @@ -129,14 +159,16 @@ def get_yields(element):
serve_text = serve_text.split(SERVE_REGEX_TO.split(serve_text, 2)[1], 2)[1]

matched = SERVE_REGEX_NUMBER.search(serve_text).groupdict().get("items") or 0
servings = "{} serving{}".format(matched, "" if int(matched) == 1 else "s")
serve_text_lower = serve_text.lower()

for singular, plural in RECIPE_YIELD_TYPES:
if singular in serve_text_lower:
return "{} {}".format(matched, singular if int(matched) == 1 else plural)

if SERVE_REGEX_ITEMS.search(serve_text) is not None:
# This assumes if object(s), like sandwiches, it is 1 person.
# Issue: "Makes one 9-inch pie, (realsimple-testcase, gives "9 items")
servings = "{} item{}".format(matched, "" if int(matched) == 1 else "s")
return "{} item{}".format(matched, "" if int(matched) == 1 else "s")

return servings
return "{} serving{}".format(matched, "" if int(matched) == 1 else "s")


def normalize_string(string):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data/hersheyland.com/herseyland.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@
"site_name": null,
"title": "HERSHEY'S \"Perfectly Chocolate\" Chocolate Cake Recipe | Hersheyland",
"total_time": 45,
"yields": "1 serving"
"yields": "1 cake"
}
2 changes: 1 addition & 1 deletion tests/test_data/kingarthurbaking.com/kingarthur.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@
"site_name": "King Arthur Baking",
"title": "Spiced Rye Ginger Cookies",
"total_time": 35,
"yields": "22 items"
"yields": "22 cookies"
}
2 changes: 1 addition & 1 deletion tests/test_data/myplate.gov/usdamyplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@
"site_name": null,
"title": "Fabulous Fig Bars",
"total_time": 75,
"yields": "24 servings"
"yields": "24 bars"
}
2 changes: 1 addition & 1 deletion tests/test_data/myrecipes.com/myrecipes.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
"site_name": "MyRecipes",
"title": "Cacio e Pepe",
"total_time": 20.0,
"yields": "2 items"
"yields": "2 cups"
}
2 changes: 1 addition & 1 deletion tests/test_data/ohsheglows.com/ohsheglows.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@
"site_name": "Oh She Glows",
"title": "Obsession-Worthy Peanut Butter Cookie Ice Cream",
"total_time": 22,
"yields": "8 servings"
"yields": "8 cups"
}
2 changes: 1 addition & 1 deletion tests/test_data/paninihappy.com/paninihappy.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@
"site_name": null,
"title": "Grilled Mac & Cheese with BBQ Pulled Pork",
"total_time": 30,
"yields": "4 items"
"yields": "4 sandwiches"
}

0 comments on commit 253013a

Please sign in to comment.