Skip to content

Commit

Permalink
fix: handle Recipe Times as dicts and lists (#2764)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Kuchenpirat authored Dec 3, 2023
1 parent a2d9387 commit f32444b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion mealie/services/scraper/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

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) """

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit f32444b

Please sign in to comment.