Fix Weissman recipe time parsing

This commit is contained in:
Fizzlepoof
2026-05-22 14:23:34 +00:00
parent 9a24dd11ad
commit 1631dcd1c1
3 changed files with 49 additions and 1 deletions

View File

@@ -20,6 +20,10 @@ UNIT_WORDS = {
STOPWORDS = {"fresh", "large", "small", "medium", "optional", "to", "taste", "for", "the", "a", "an"}
TAG_KEYS = ("recipeCategory", "recipeCuisine", "keywords")
DURATION_RE = re.compile(r"^P(?:T(?:(?P<hours>\d+)H)?(?:(?P<minutes>\d+)M)?(?:(?P<seconds>\d+)S)?)?$")
TEXT_DURATION_PART_RE = re.compile(
r"(?P<value>\d+(?:\.\d+)?)\s*(?P<unit>hours?|hrs?|hr|minutes?|mins?|min|seconds?|secs?|sec|s)\b",
re.IGNORECASE,
)
class ScriptExtractor(HTMLParser):
@@ -410,6 +414,20 @@ def parse_duration_minutes(value: Any) -> int | None:
seconds = int(match.group("seconds") or 0)
total = hours * 60 + minutes + (1 if seconds >= 30 else 0)
return total or None
textual_matches = list(TEXT_DURATION_PART_RE.finditer(text))
if textual_matches:
total_minutes = 0.0
for part in textual_matches:
amount = float(part.group("value"))
unit = part.group("unit").lower()
if unit.startswith(("hour", "hr")):
total_minutes += amount * 60
elif unit.startswith(("minute", "min")):
total_minutes += amount
else:
total_minutes += amount / 60
rounded = int(total_minutes + 0.5)
return rounded or None
number = re.search(r"(\d+)", text)
return int(number.group(1)) if number else None