Handle Weissman day and overnight times

This commit is contained in:
Fizzlepoof
2026-05-22 09:27:46 -05:00
parent 1631dcd1c1
commit 4b1e3d4061
3 changed files with 19 additions and 6 deletions

View File

@@ -21,9 +21,10 @@ STOPWORDS = {"fresh", "large", "small", "medium", "optional", "to", "taste", "fo
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",
r"(?P<value>\d+(?:\.\d+)?)\s*(?P<unit>days?|day|hours?|hrs?|hr|minutes?|mins?|min|seconds?|secs?|sec|s)\b",
re.IGNORECASE,
)
OVERNIGHT_RE = re.compile(r"\bovernight\b", re.IGNORECASE)
class ScriptExtractor(HTMLParser):
@@ -415,17 +416,21 @@ def parse_duration_minutes(value: Any) -> int | None:
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:
overnight_count = len(OVERNIGHT_RE.findall(text))
if textual_matches or overnight_count:
total_minutes = 0.0
for part in textual_matches:
amount = float(part.group("value"))
unit = part.group("unit").lower()
if unit.startswith(("hour", "hr")):
if unit.startswith("day"):
total_minutes += amount * 24 * 60
elif unit.startswith(("hour", "hr")):
total_minutes += amount * 60
elif unit.startswith(("minute", "min")):
total_minutes += amount
else:
total_minutes += amount / 60
total_minutes += overnight_count * 12 * 60
rounded = int(total_minutes + 0.5)
return rounded or None
number = re.search(r"(\d+)", text)