From 85ecd3b9da49a2e49c1e2bd68b099a40ed78862f Mon Sep 17 00:00:00 2001 From: Joey Date: Fri, 29 Sep 2023 13:18:41 -0400 Subject: [PATCH 1/4] Updated total_time to include prep_time --- recipe_scrapers/hellofresh.py | 19 ++++++++++++++++++- tests/test_hellofresh.py | 2 +- tests/test_hellofresh_adhoc.py | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/recipe_scrapers/hellofresh.py b/recipe_scrapers/hellofresh.py index 2b620fdd4..db32a09e7 100644 --- a/recipe_scrapers/hellofresh.py +++ b/recipe_scrapers/hellofresh.py @@ -1,5 +1,8 @@ # mypy: disallow_untyped_defs=False +import re + from ._abstract import AbstractScraper +from ._utils import get_minutes class HelloFresh(AbstractScraper): @@ -11,7 +14,21 @@ def title(self): return self.schema.title() def total_time(self): - return self.schema.total_time() + script_tag = self.soup.find("script", {"id": "__NEXT_DATA__"}) + + if script_tag: + script_content = script_tag.string + total_time_match = re.search(r'"totalTime":"(PT\d+M)"', script_content) + prep_time_match = re.search(r'"prepTime":"(PT\d+M)"', script_content) + + if total_time_match and prep_time_match: + total_time_str = total_time_match.group(1) + prep_time_str = prep_time_match.group(1) + + total_time_minutes = get_minutes(total_time_str) + prep_time_minutes = get_minutes(prep_time_str) + + return total_time_minutes + prep_time_minutes def yields(self): return self.schema.yields() diff --git a/tests/test_hellofresh.py b/tests/test_hellofresh.py index bc749a4f4..5c10ef7ee 100644 --- a/tests/test_hellofresh.py +++ b/tests/test_hellofresh.py @@ -27,7 +27,7 @@ def test_author(self): self.assertEqual(self.harvester_class.author(), "HelloFresh") def test_total_time(self): - self.assertEqual(35, self.harvester_class.total_time()) + self.assertEqual(50, self.harvester_class.total_time()) def test_yields(self): self.assertEqual("2 servings", self.harvester_class.yields()) diff --git a/tests/test_hellofresh_adhoc.py b/tests/test_hellofresh_adhoc.py index d0ad47cf8..c30f612fa 100644 --- a/tests/test_hellofresh_adhoc.py +++ b/tests/test_hellofresh_adhoc.py @@ -23,7 +23,7 @@ def test_author(self): self.assertEqual(self.harvester_class.author(), "HelloFresh") def test_total_time(self): - self.assertEqual(35, self.harvester_class.total_time()) + self.assertEqual(65, self.harvester_class.total_time()) def test_yields(self): self.assertEqual("2 servings", self.harvester_class.yields()) From 02b9f48c100783474a26617d42e81421540673bb Mon Sep 17 00:00:00 2001 From: Joey Date: Sat, 30 Sep 2023 16:09:11 -0400 Subject: [PATCH 2/4] Refactor total_time method and update comments for HelloFresh metadata --- recipe_scrapers/hellofresh.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/recipe_scrapers/hellofresh.py b/recipe_scrapers/hellofresh.py index db32a09e7..7bac7b078 100644 --- a/recipe_scrapers/hellofresh.py +++ b/recipe_scrapers/hellofresh.py @@ -13,22 +13,30 @@ def host(cls, domain="com"): def title(self): return self.schema.title() - def total_time(self): + def cook_time(self): script_tag = self.soup.find("script", {"id": "__NEXT_DATA__"}) - if script_tag: script_content = script_tag.string total_time_match = re.search(r'"totalTime":"(PT\d+M)"', script_content) - prep_time_match = re.search(r'"prepTime":"(PT\d+M)"', script_content) - - if total_time_match and prep_time_match: + if total_time_match: total_time_str = total_time_match.group(1) - prep_time_str = prep_time_match.group(1) + return get_minutes(total_time_str) + return 0 - total_time_minutes = get_minutes(total_time_str) - prep_time_minutes = get_minutes(prep_time_str) + def prep_time(self): + script_tag = self.soup.find("script", {"id": "__NEXT_DATA__"}) + if script_tag: + script_content = script_tag.string + prep_time_match = re.search(r'"prepTime":"(PT\d+M)"', script_content) + if prep_time_match: + prep_time_str = prep_time_match.group(1) + return get_minutes(prep_time_str) + return 0 - return total_time_minutes + prep_time_minutes + # Note: HelloFresh uses the 'totalTime' metadata field to represent only the cook time. + # To get the actual total time, the 'prepTime' and 'totalTime' (which is the cook time) need to be added. + def total_time(self): + return self.cook_time() + self.prep_time() def yields(self): return self.schema.yields() From d6621e2c22ac698f97436e6519c93166fd2f4f85 Mon Sep 17 00:00:00 2001 From: Joey Date: Sat, 30 Sep 2023 20:20:15 -0400 Subject: [PATCH 3/4] Update recipe_scrapers/hellofresh.py Co-authored-by: James Addison <55152140+jayaddison@users.noreply.github.com> --- recipe_scrapers/hellofresh.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/recipe_scrapers/hellofresh.py b/recipe_scrapers/hellofresh.py index 7bac7b078..79dcfd68c 100644 --- a/recipe_scrapers/hellofresh.py +++ b/recipe_scrapers/hellofresh.py @@ -36,7 +36,9 @@ def prep_time(self): # Note: HelloFresh uses the 'totalTime' metadata field to represent only the cook time. # To get the actual total time, the 'prepTime' and 'totalTime' (which is the cook time) need to be added. def total_time(self): - return self.cook_time() + self.prep_time() + prep_time, cook_time = self.prep_time(), self.cook_time() + if prep_time or cook_time: + return (prep_time or 0) + (cook_time or 0) def yields(self): return self.schema.yields() From f876d1ea3dda70715723ff5c693491e4a33dda5f Mon Sep 17 00:00:00 2001 From: Joey Date: Sat, 30 Sep 2023 20:22:00 -0400 Subject: [PATCH 4/4] Update hellofresh.py Removed "return 0" and implemented recommended adjustments. --- recipe_scrapers/hellofresh.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/recipe_scrapers/hellofresh.py b/recipe_scrapers/hellofresh.py index 7bac7b078..d83686462 100644 --- a/recipe_scrapers/hellofresh.py +++ b/recipe_scrapers/hellofresh.py @@ -21,7 +21,6 @@ def cook_time(self): if total_time_match: total_time_str = total_time_match.group(1) return get_minutes(total_time_str) - return 0 def prep_time(self): script_tag = self.soup.find("script", {"id": "__NEXT_DATA__"}) @@ -31,12 +30,13 @@ def prep_time(self): if prep_time_match: prep_time_str = prep_time_match.group(1) return get_minutes(prep_time_str) - return 0 # Note: HelloFresh uses the 'totalTime' metadata field to represent only the cook time. # To get the actual total time, the 'prepTime' and 'totalTime' (which is the cook time) need to be added. def total_time(self): - return self.cook_time() + self.prep_time() + prep_time, cook_time = self.prep_time(), self.cook_time() + if prep_time or cook_time: + return (prep_time or 0) + (cook_time or 0) def yields(self): return self.schema.yields()