Fix Weissman recipe time parsing
This commit is contained in:
@@ -295,6 +295,10 @@ def enrich_recipe_node_from_dom(recipe_node: dict[str, Any], html: str) -> dict[
|
||||
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
def parse_duration_minutes(value: Any) -> int | None:
|
||||
@@ -314,6 +318,20 @@ def parse_duration_minutes(value: Any) -> int | None:
|
||||
seconds = int(m.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(value))
|
||||
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
|
||||
num = re.search(r'(\d+)', value)
|
||||
return int(num.group(1)) if num else None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user