1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-05 05:25:26 +02:00

fix: handle Recipe Times as dicts and lists (#2764)

* handle dicts

* 🧹

* handle arrays

* change default case & add warning logger

* lint

* typo

* update dict case

* update list case

* add timedelta to cases

* remove timedelta so mypy is happy
This commit is contained in:
Kuchenpirat 2023-12-03 06:15:44 +01:00 committed by GitHub
parent a2d9387077
commit f32444b91d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -9,6 +9,11 @@ from datetime import datetime, timedelta
from slugify import slugify
from mealie.core.root_logger import get_logger
logger = get_logger("recipe-scraper")
MATCH_DIGITS = re.compile(r"\d+([.,]\d+)?")
""" Allow for commas as decimals (common in Europe) """
@ -335,6 +340,7 @@ def clean_time(time_entry: str | timedelta | None) -> None | str:
- `"PT1H"` - returns "1 hour"
- `"PT1H30M"` - returns "1 hour 30 minutes"
- `timedelta(hours=1, minutes=30)` - returns "1 hour 30 minutes"
- `{"minValue": "PT1H30M"}` - returns "1 hour 30 minutes"
Raises:
TypeError: if the type is not supported a TypeError is raised
@ -357,11 +363,16 @@ def clean_time(time_entry: str | timedelta | None) -> None | str:
return str(time_entry)
case timedelta():
return pretty_print_timedelta(time_entry)
case {"minValue": str(value)}:
return clean_time(value)
case [str(), *_]:
return clean_time(time_entry[0])
case datetime():
# TODO: Not sure what to do here
return str(time_entry)
case _:
raise TypeError(f"Unexpected type for time: {type(time_entry)}, {time_entry}")
logger.warning("[SCRAPER] Unexpected type or structure for time_entrys")
return None
def parse_duration(iso_duration: str) -> timedelta: