From 4f3cfdc3829ce57f3cc8cba02ca69c29d761635e Mon Sep 17 00:00:00 2001 From: Kjell Pettersson Date: Thu, 4 Aug 2022 21:50:44 +0200 Subject: [PATCH 1/3] Add support for coop.se --- README.rst | 1 + recipe_scrapers/__init__.py | 2 + recipe_scrapers/coop.py | 63 ++ tests/test_coop.py | 64 ++ tests/test_data/coop.testhtml | 1592 +++++++++++++++++++++++++++++++++ 5 files changed, 1722 insertions(+) create mode 100644 recipe_scrapers/coop.py create mode 100644 tests/test_coop.py create mode 100644 tests/test_data/coop.testhtml diff --git a/README.rst b/README.rst index d4e3e4b1b..8294e9787 100644 --- a/README.rst +++ b/README.rst @@ -118,6 +118,7 @@ Scrapers available for: - `https://cookinglight.com/ `_ - `https://cookpad.com/ `_ - `https://cookstr.com/ `_ +- `https://www.coop.se/ `_ - `https://countryliving.com/ `_ - `https://cucchiaio.it/ `_ diff --git a/recipe_scrapers/__init__.py b/recipe_scrapers/__init__.py index f4976dc0f..e714ce077 100644 --- a/recipe_scrapers/__init__.py +++ b/recipe_scrapers/__init__.py @@ -40,6 +40,7 @@ from .cookingcircle import CookingCircle from .cookinglight import CookingLight from .cookpad import CookPad +from .coop import Coop from .cookstr import Cookstr from .copykat import CopyKat from .countryliving import CountryLiving @@ -246,6 +247,7 @@ CookingCircle.host(): CookingCircle, CookingLight.host(): CookingLight, Cookstr.host(): Cookstr, + Coop.host(): Coop, CopyKat.host(): CopyKat, CountryLiving.host(): CountryLiving, Cucchiaio.host(): Cucchiaio, diff --git a/recipe_scrapers/coop.py b/recipe_scrapers/coop.py new file mode 100644 index 000000000..1f15e8d7f --- /dev/null +++ b/recipe_scrapers/coop.py @@ -0,0 +1,63 @@ +from ._abstract import AbstractScraper + + +class Coop(AbstractScraper): + @classmethod + def host(cls): + return "coop.se" + + def author(self): + author = self.schema.author() + if author is None: + author = self.schema.data.get("Author") + if ( + author + and isinstance(author, list) + and len(author) >= 1 + and isinstance(author[0], dict) + ): + author = author[0] + if author and isinstance(author, dict): + author = author.get("name") + return author + + def title(self): + return self.schema.title() + + def category(self): + return self.schema.category() + + def cook_time(self): + return self.schema.cook_time() + + def yields(self): + return self.schema.yields() + + def image(self): + image = self.schema.image() + if image is None or image == '': + image = self.schema.data.get("image") + if ( + image + and isinstance(image, list) + and len(image) >= 1 + and isinstance(image[0], str) + and image[0].startswith("//") + ): + image = "https:" + image[0] + return image + + def ingredients(self): + return self.schema.ingredients() + + def instructions(self): + return self.schema.instructions() + + def ratings(self): + return self.schema.ratings() + + def cuisine(self): + return self.schema.cuisine() + + def description(self): + return self.schema.description() diff --git a/tests/test_coop.py b/tests/test_coop.py new file mode 100644 index 000000000..a4e68b41e --- /dev/null +++ b/tests/test_coop.py @@ -0,0 +1,64 @@ +from recipe_scrapers.coop import Coop +from tests import ScraperTest + + +class TestTemplateScraper(ScraperTest): + + scraper_class = Coop + + def test_host(self): + self.assertEqual("coop.se", self.harvester_class.host()) + + def test_author(self): + self.assertEqual("Sara Begner och Ulrika Brydling", self.harvester_class.author()) + + def test_title(self): + self.assertEqual("Grillat kött med lime- och chilismör", self.harvester_class.title()) + + def test_category(self): + self.assertEqual("Huvudrätt", self.harvester_class.category()) + + def test_cook_time(self): + self.assertEqual(15, self.harvester_class.cook_time()) + + def test_total_time(self): + self.assertEqual(15, self.harvester_class.total_time()) + + def test_yields(self): + self.assertEqual("4 servings", self.harvester_class.yields()) + + def test_image(self): + self.assertEqual("https://res.cloudinary.com/coopsverige/image/upload/w_1200,h_1200/v1651049395/cloud/251336.jpg", self.harvester_class.image()) + + def test_ingredients(self): + self.assertEqual( + [ + '600.0 g flapsteak, flankstek, entrecôte eller mörad högrev', + '1.0 tsk flingsalt', + '100.0 g rumsvarmt smör', + '1.0 röd chili, skivad', + '2.0 lime', + '4.0 hjärtsalladshuvud', + '0.0 flingsalt', + '0.0 grovmalen svartpeppar' + ], + self.harvester_class.ingredients() + ) + + def test_instructions(self): + self.assertEqual( + """Tänd grillen med kol/briketter på ena sidan. Salta köttet. Vispa smöret fluffigt och smaksätt med chili och finrivet skal av 1 lime.\nVid fin glöd, grilla köttet direkt över värmen ca 1–2 min på varje sida. Köttet ska få fin yta och en innertemp på 52–55°. Låt vila 5 min.\nDela lime och hjärtsallad och grilla ca 5 min. Skär köttet i skivor, strö över flingsalt och svartpeppar och servera med hjärtsallad, smör och pressad lime.""", + self.harvester_class.instructions() + ) + + def test_ratings(self): + self.assertEqual(4.71, self.harvester_class.ratings()) + + def test_cuisine(self): + self.assertEqual("Mer Smak", self.harvester_class.cuisine()) + + def test_description(self): + self.assertEqual( + "Grillat kött som toppas med ett smakrikt smör på chili och pressad lime. Servera med grillad hjärtsallad.", + self.harvester_class.description() + ) diff --git a/tests/test_data/coop.testhtml b/tests/test_data/coop.testhtml new file mode 100644 index 000000000..ab5bd5ea8 --- /dev/null +++ b/tests/test_data/coop.testhtml @@ -0,0 +1,1592 @@ + + + + + + Grillat kött med lime- och chilismör | Recept | Coop + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + +
+ + + + + + + + + + + +
+ + +
+ +
+
+ + + + +
+ + + +
+
+ + +
+ + + + + + +
+ +
+
+
+
+
+ +
+
+
+
+
+ + + +
+ + + + +
+
+ + +
+ Grillat kött med lime- och chilismör +
+
+ +
+
+

Grillat kött med lime- och chilismör

+
+
Betyg (1-5):
+
+
+ + + Betyg + + +
+
+ + + Betyg + + +
+
+ + (7) + + +
+ +
+
+
+
+ +
+
+
+ + +

Grillat kött som toppas med ett smakrikt smör på chili och pressad lime. Servera med grillad hjärtsallad.

+ +
+
+
+ Tillagningstid: 15 min +
+
+ 4 portioner +
+
+
+
+ +
+
+ +
+ + +
+
+
+ + +
+
+
+
+ + + +
+
+
+
    +
  • + + +
  • + 600 g + flapsteak, flankstek, entrecôte eller mörad högrev +
  • +
  • + 1 tsk + flingsalt +
  • +
  • + 100 g + rumsvarmt smör +
  • +
  • + 1 + röd chili, skivad +
  • +
  • + 2 + lime +
  • +
  • + 4 + hjärtsalladshuvuden +
  • +
  • + flingsalt +
  • +
  • + grovmalen svartpeppar +
  • +
+
+
+
+
+
+
+

Gör så här

+ +
+
    +
  1. Tänd grillen med kol/briketter på ena sidan. Salta köttet. Vispa smöret fluffigt och smaksätt med chili och finrivet skal av 1 lime.
  2. +
  3. Vid fin glöd, grilla köttet direkt över värmen ca 1–2 min på varje sida. Köttet ska få fin yta och en innertemp på 52–55°. Låt vila 5 min.
  4. +
  5. Dela lime och hjärtsallad och grilla ca 5 min. Skär köttet i skivor, strö över flingsalt och svartpeppar och servera med hjärtsallad, smör och pressad lime.
  6. +
+
+
+ + +
+ + + + + + +
+
+
+ + +
+
+ +
+

- kommentarer

+
+ +
+

+ Logga in för att skriva en kommentar +

+ +
+
+
+

Tack för din kommentar!

+
+
+
+ +
+
+
+ +
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+ +
+ +
+ +
+ +
+
+ + +
+ + +
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+ Grillat kött med lime- och chilismör +
+
+
+
+
+
+
+
+
+ +
+ +
+
+ + 8 st + + + +
    +
  • + Ingredienser + + (8) +
  • +
  • + + + + +
  • + 600 g + flapsteak, flankstek, entrecôte eller mörad högrev +
  • +
  • + 1 tsk + flingsalt +
  • +
  • + 100 g + rumsvarmt smör +
  • +
  • + 1 + röd chili, skivad +
  • +
  • + 2 + lime +
  • +
  • + 4 + hjärtsalladshuvuden +
  • +
  • + flingsalt +
  • +
  • + grovmalen svartpeppar +
  • +
+
+
+
+
+
+
+ +
+
+
+
+
+

Grillat kött med lime- och chilismör

+
+
Betyg (1-5):
+
+
+ + + Betyg + + +
+
+ + + Betyg + + +
+
+ + (7) + + +
+ +
+
+
+
+ +
+
+
+ + +

Grillat kött som toppas med ett smakrikt smör på chili och pressad lime. Servera med grillad hjärtsallad.

+ +
+
+
+ Tillagningstid: 15 min +
+
+ 4 portioner +
+
+
+
+ +
+

Gör så här

+ +
+
    +
  1. Tänd grillen med kol/briketter på ena sidan. Salta köttet. Vispa smöret fluffigt och smaksätt med chili och finrivet skal av 1 lime.
  2. +
  3. Vid fin glöd, grilla köttet direkt över värmen ca 1–2 min på varje sida. Köttet ska få fin yta och en innertemp på 52–55°. Låt vila 5 min.
  4. +
  5. Dela lime och hjärtsallad och grilla ca 5 min. Skär köttet i skivor, strö över flingsalt och svartpeppar och servera med hjärtsallad, smör och pressad lime.
  6. +
+
+
+ + + + + +
+ + + +
+
+ +
+

- kommentarer

+
+ +
+

+ Logga in för att skriva en kommentar +

+ +
+
+
+

Tack för din kommentar!

+
+
+
+ +
+
+
+ +
+
+
+ +
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+ +
+ +
+ +
+ +
+
+ + +
+ + + +
+
+
+
+
+
+
+ +
+ +
+ + +
+ +
+ + + +
+ + + +
+ + + + + + + + + \ No newline at end of file From e0f27335be6561f9fcc3d689289dd5a335d4c95a Mon Sep 17 00:00:00 2001 From: Kjell Pettersson Date: Sun, 7 Aug 2022 13:02:11 +0200 Subject: [PATCH 2/3] Update name of test class --- tests/test_coop.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_coop.py b/tests/test_coop.py index a4e68b41e..24e974054 100644 --- a/tests/test_coop.py +++ b/tests/test_coop.py @@ -2,7 +2,7 @@ from tests import ScraperTest -class TestTemplateScraper(ScraperTest): +class TestCoopScraper(ScraperTest): scraper_class = Coop From 932976701d073264fcf7d2f42ad02d6351b490b8 Mon Sep 17 00:00:00 2001 From: Kjell Pettersson Date: Tue, 9 Aug 2022 10:59:09 +0200 Subject: [PATCH 3/3] Handle Author with capital A in SchemaOrg instead of Coop parser --- recipe_scrapers/_schemaorg.py | 2 +- recipe_scrapers/coop.py | 14 +------------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/recipe_scrapers/_schemaorg.py b/recipe_scrapers/_schemaorg.py index 49c997726..779e78c00 100644 --- a/recipe_scrapers/_schemaorg.py +++ b/recipe_scrapers/_schemaorg.py @@ -82,7 +82,7 @@ def category(self): return category def author(self): - author = self.data.get("author") + author = self.data.get("author") or self.data.get("Author") if ( author and isinstance(author, list) diff --git a/recipe_scrapers/coop.py b/recipe_scrapers/coop.py index 1f15e8d7f..ff7aeadad 100644 --- a/recipe_scrapers/coop.py +++ b/recipe_scrapers/coop.py @@ -7,19 +7,7 @@ def host(cls): return "coop.se" def author(self): - author = self.schema.author() - if author is None: - author = self.schema.data.get("Author") - if ( - author - and isinstance(author, list) - and len(author) >= 1 - and isinstance(author[0], dict) - ): - author = author[0] - if author and isinstance(author, dict): - author = author.get("name") - return author + return self.schema.author() def title(self): return self.schema.title()