Repository: hhursev/recipe-scrapers
Pull request #342 (2021-02-26 18:03:11)

Enhancing Site: Southernliving.com
greatly simplified the code by using self.schema in 6 methods

instructions is now a \n-delimited list


Patch:
diff --git a/recipe_scrapers/southernliving.py b/recipe_scrapers/southernliving.py
index 323c13f8..fed44e99 100644
--- a/recipe_scrapers/southernliving.py
+++ b/recipe_scrapers/southernliving.py
@@ -13,82 +13,38 @@ class SouthernLiving(AbstractScraper):
         return "southernliving.com"
 
     def title(self):
-        return normalize_string(self.soup.find("h1").get_text())
+        return self.schema.title()
 
     def total_time(self):
-        total_time = 0
-        try:
-            tt1 = self.soup.findAll("div", {"class": "recipe-meta-item"})
-            for tt in tt1:
-                # Find the entry that has 'Total'
-                header = normalize_string(
-                    tt.find("div", {"class": "recipe-meta-item-header"}).get_text()
-                )
-                if "total" in header.lower():
-                    total_time = normalize_string(
-                        tt.find("div", {"class": "recipe-meta-item-body"}).get_text()
-                    )
-                    break
-
-        except Exception:
-            tt1 = 0
-        total = get_minutes(total_time)
-        return total
+        return get_minutes(self.schema.total_time())
 
     def yields(self):
-        try:
-            yield1 = self.soup.findAll("div", {"class": "recipe-meta-item"})
-            for y in yield1:
-                # Find the entry that has 'Yield'
-                header = normalize_string(
-                    y.find("div", {"class": "recipe-meta-item-header"}).get_text()
-                )
-                if header == "Yield":
-                    y = normalize_string(
-                        y.find("div", {"class": "recipe-meta-item-body"}).get_text()
-                    )
-                    break
-        except Exception:
-            y = 0
-        total = get_yields(y)
-        return total
+        return get_yields(self.schema.yields())
 
     def image(self):
-        try:
-            image = self.soup.find(
-                "div", {"class": "image-container"}
-            )  # , 'src': True})
-            im = image.find(
-                "div",
-                attrs={"class": lambda e: e.startswith("component") if e else False},
-            )
-
-            lnk = im["data-src"]
-        except Exception:
-            lnk = None
-
-        return lnk  # if image else None
+        return self.schema.image()
 
     def ingredients(self):
-        ingredientsOuter = self.soup.find("ul", {"class": "ingredients-section"})
-        lines = ingredientsOuter.findAll("li")
-
-        return [normalize_string(li.label.get_text()) for li in lines]
+        return self.schema.ingredients()
 
     def instructions(self):
-        instructions = self.soup.find("ul", {"class": "instructions-section"})
-
-        data = []
-        instruction = instructions.findAll("li", {"class": "instructions-section-item"})
-        for ins in instruction:
-            line = normalize_string(ins.find("div", {"class": "paragraph"}).get_text())
-            data.append(line)
-        return data
+        instructions = self.soup.find("ul", {"class": "instructions-section"}).findAll(
+            "li", {"class": "instructions-section-item"}
+        )
+        return "\n".join(
+            [
+                normalize_string(
+                    instruction.find("div", {"class": "paragraph"}).get_text()
+                )
+                for instruction in instructions
+            ]
+        )
 
     def ratings(self):
-        # This site does not support ratings at this time
-        r1 = None
-        return r1
+        try:
+            self.schema.ratings()
+        except Exception:
+            return None
 
     def description(self):
         des = self.soup.find(


Test patch:
diff --git a/tests/test_southernliving.py b/tests/test_southernliving.py
index 82be5ee6..e6731488 100644
--- a/tests/test_southernliving.py
+++ b/tests/test_southernliving.py
@@ -34,7 +34,7 @@ class TestSouthernLiving(ScraperTest):
         )
 
     def test_ingredients(self):
-        self.assertCountEqual(
+        self.assertEqual(
             [
                 "1 cup whole milk",
                 "3/4 cup salted butter, divided",
@@ -52,14 +52,16 @@ class TestSouthernLiving(ScraperTest):
 
     def test_instructions(self):
         return self.assertEqual(
-            [
-                "Combine whole milk and 1⁄2 cup of the butter in a small saucepan. Cook over medium-low, stirring occasionally, until butter melts, about 5 minutes. Transfer mixture to a medium bowl, and cool slightly to 100°F to 110°F, about 10 minutes.",
-                "Stir together warm water, active dry yeast, and 1 teaspoon of the granulated sugar in a small bowl. Let stand until foamy, about 5 minutes.",
-                "Combine eggs, salt, and remaining 1⁄3 cup granulated sugar in the bowl of a heavy-duty electric stand mixer fitted with a dough hook attachment. Beat on medium-low speed just until combined. Stir in milk mixture and yeast mixture. Gradually add 4 1⁄2 cups of the bread flour, beating just until incorporated. With mixer running on low speed, gradually add up to 3⁄4 cup more flour, 1⁄4 cup at a time, just until dough pulls away from sides of bowl.",
-                "Turn dough out onto a lightly floured work surface. Knead until smooth and elastic, about 10 minutes, adding up to 3⁄4 cup flour, in very small amounts, if necessary to keep dough workable. Place dough in a lightly greased bowl, turning to coat all sides. Cover and let stand in a warm place until dough doubles in size, about 1 hour.",
-                "Cut sausage into 20 to 22 (2-inch-long) straight pieces, reserving curved pieces for another use. Divide dough into 20 to 22 (2-ounce) balls (a little larger than a golf ball). Roll each ball into a 4-inch-wide circle on a lightly floured surface. (Keep remaining dough balls covered while working.) Place about 1 tablespoon of the cheese on lower third of each dough circle, and top with 1 sausage piece. Fold dough over filling, folding in sides; pinch to seal. Place kolaches, seam side down, on 2 baking sheets lined with parchment paper, leaving 1 inch between them. Keep covered while working. Cover loosely with plastic wrap, and let stand in a warm place until dough doubles in size, about 45 minutes.",
-                "Preheat oven to 375°F. Microwave remaining 1⁄4 cup butter in a small microwavable bowl on HIGH until melted, about 45 seconds. Brush kolaches with half of melted butter. Bake in preheated oven until golden brown, 14 to 16 minutes. Brush with remaining melted butter, and serve hot.",
-            ],
+            "\n".join(
+                [
+                    "Combine whole milk and 1⁄2 cup of the butter in a small saucepan. Cook over medium-low, stirring occasionally, until butter melts, about 5 minutes. Transfer mixture to a medium bowl, and cool slightly to 100°F to 110°F, about 10 minutes.",
+                    "Stir together warm water, active dry yeast, and 1 teaspoon of the granulated sugar in a small bowl. Let stand until foamy, about 5 minutes.",
+                    "Combine eggs, salt, and remaining 1⁄3 cup granulated sugar in the bowl of a heavy-duty electric stand mixer fitted with a dough hook attachment. Beat on medium-low speed just until combined. Stir in milk mixture and yeast mixture. Gradually add 4 1⁄2 cups of the bread flour, beating just until incorporated. With mixer running on low speed, gradually add up to 3⁄4 cup more flour, 1⁄4 cup at a time, just until dough pulls away from sides of bowl.",
+                    "Turn dough out onto a lightly floured work surface. Knead until smooth and elastic, about 10 minutes, adding up to 3⁄4 cup flour, in very small amounts, if necessary to keep dough workable. Place dough in a lightly greased bowl, turning to coat all sides. Cover and let stand in a warm place until dough doubles in size, about 1 hour.",
+                    "Cut sausage into 20 to 22 (2-inch-long) straight pieces, reserving curved pieces for another use. Divide dough into 20 to 22 (2-ounce) balls (a little larger than a golf ball). Roll each ball into a 4-inch-wide circle on a lightly floured surface. (Keep remaining dough balls covered while working.) Place about 1 tablespoon of the cheese on lower third of each dough circle, and top with 1 sausage piece. Fold dough over filling, folding in sides; pinch to seal. Place kolaches, seam side down, on 2 baking sheets lined with parchment paper, leaving 1 inch between them. Keep covered while working. Cover loosely with plastic wrap, and let stand in a warm place until dough doubles in size, about 45 minutes.",
+                    "Preheat oven to 375°F. Microwave remaining 1⁄4 cup butter in a small microwavable bowl on HIGH until melted, about 45 seconds. Brush kolaches with half of melted butter. Bake in preheated oven until golden brown, 14 to 16 minutes. Brush with remaining melted butter, and serve hot.",
+                ]
+            ),
             self.harvester_class.instructions(),
         )