Repository: hhursev/recipe-scrapers
Pull request #249 (2020-10-24 14:24:26)

Issue 246
Closes https://github.com/hhursev/recipe-scrapers/issues/246


Patch:
diff --git a/recipe_scrapers/_schemaorg.py b/recipe_scrapers/_schemaorg.py
index 91baaa8e..a0730f0e 100644
--- a/recipe_scrapers/_schemaorg.py
+++ b/recipe_scrapers/_schemaorg.py
@@ -33,16 +33,19 @@ class SchemaOrg:
                     return
                 elif in_context and "@graph" in item:
                     for graph_item in item.get("@graph", ""):
-                        in_graph = SCHEMA_ORG_HOST in graph_item.get("@context", "")
-                        if (
-                            in_graph
-                            and graph_item.get("@type", "").lower() in low_schema
-                        ):
+                        graph_item_type = graph_item.get("@type", "")
+                        if type(graph_item_type) != str:
+                            continue
+                        if graph_item_type.lower() in low_schema:
+                            in_graph = SCHEMA_ORG_HOST in graph_item.get("@context", "")
                             self.format = syntax
-                            self.data = graph_item
-                            if graph_item.get("@type").lower() == "webpage":
+                            if graph_item_type.lower() == "webpage" and in_graph:
                                 self.data = self.data.get("mainEntity")
-                            return
+                                return
+                            elif graph_item_type.lower() == "recipe":
+                                self.data = graph_item
+                                return
+                            continue
 
     def language(self):
         return self.data.get("inLanguage") or self.data.get("language")


Test patch:
diff --git a/tests/test_data/innit.testhtml b/tests/test_data/innit.testhtml
index 48783e02..0cee4e2a 100644
--- a/tests/test_data/innit.testhtml
+++ b/tests/test_data/innit.testhtml
@@ -221,7 +221,7 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
         ],
         "recipeCuisine": [
         ],
-        "recipeCategory": "Salads",
+        "recipeCategory": "Salads"
     }
 </script></head><body>
 <div class="wrapper">
diff --git a/tests/test_data/schemaorg_graph.testhtml b/tests/test_data/schemaorg_graph.testhtml
new file mode 100644
index 00000000..f6d96805
--- /dev/null
+++ b/tests/test_data/schemaorg_graph.testhtml
@@ -0,0 +1,68 @@
+<html>
+    <head>
+        <script type="application/ld+json">
+{
+    "@context": "https://schema.org",
+    "@graph": [
+        {
+            "@type": "Recipe",
+            "name": "Gluten-Free Pizza Crust",
+            "keywords": "Pizza, Gluten-free",
+            "description": "Crisp and chewy, this is a wonderful base for your favorite toppings.\nNote: you must use a stand mixer or electric hand mixer to make this dough; mixing by hand doesn\u0027t do a thorough enough job.",
+            "recipeYield": "one 9\u0022 to 12\u0022 pizza",
+            "prepTime": "PT15M",
+            "datePublished": "February 23, 2010 at 7:00pm",
+            "totalTime": "PT1H5M",
+            "cookTime": "PT12M",
+            "recipeIngredient": [
+                "1 1/2 cups (224g) King Arthur Gluten-Free All-Purpose Flour",
+                "2 tablespoons (18g) buttermilk powder or 2 tablespoons (14g) nonfat dry milk powder",
+                "1 teaspoon baking powder",
+                "3/4 teaspoon salt",
+                "1 teaspoon xanthan gum",
+                "1 1/2 teaspoons instant yeast",
+                "1 tablespoon sugar or honey optional",
+                "1 cup (227g) warm water",
+                "1/4 cup (50g) olive oil divided"
+            ],
+            "recipeInstructions": "Place the dry ingredients (except the yeast and sugar or honey, if you\u0027re using it) into a large mixing bowl; the bowl of your stand mixer is perfect. Mix until thoroughly blended., Place the sugar or honey (if you\u0027re using it), warm water, 2 tablespoons (25g) olive oil, yeast, and about 1/2 cup (78g) of the dry mixture into a small bowl. Stir to combine; a few lumps are OK. Set aside for 30 minutes or so, until the mixture is bubbly and smells yeasty., Add this mixture to the dry ingredients, and beat on medium-high speed for 4 minutes. The mixture will be thick and sticky; if you\u0027ve ever applied spackling compound to a wall, that\u0027s exactly what it\u0027ll look and feel like. Note: you must use a stand mixer or electric hand mixer to make this dough; mixing by hand doesn\u0027t do a thorough enough job., Cover the bowl, and let the dough rest for 30 minutes or so., Preheat the oven to 425°F., Drizzle 2 tablespoons (25g) olive oil onto the center of a baking sheet or 12\u0022 round pizza pan. Scrape the dough from the bowl onto the puddle of oil., Using your wet fingers, start at the center of the dough and work outwards, pressing it into a 12\u0022 to 14\u0022 circle., Let the dough rest, uncovered, for 15 minutes., Bake the crust for 8 to 10 minutes, just until it\u0027s set; the surface will look opaque, rather than shiny., Remove from the oven and top with whatever you like. Return to the oven to finish baking, about 10 to 15 minutes depending on the toppings you\u0027ve chosen., Remove from the oven, and serve warm.",
+            "author": {
+                "@type": "Organization",
+                "name": "King Arthur Flour",
+                "url": "https://www.kingarthurflour.com/",
+                "logo": {
+                    "@type": "ImageObject",
+                    "url": "https://www.kingarthurflour.com/themes/custom/kaf_nextgenweb/images/logo--full.svg"
+                }
+            },
+            "nutrition": {
+                "@type": "NutritionInformation",
+                "servingSize": [
+                    "1/8 of recipe",
+                    "no to"
+                ],
+                "calories": "152",
+                "carbohydrateContent": "25g",
+                "fiberContent": "1g",
+                "proteinContent": "2g",
+                "sodiumContent": "284mg",
+                "sugarContent": "3g",
+                "fatContent": "5g",
+                "saturatedFatContent": "1g",
+                "transFatContent": "0g"
+            },
+            "image": {
+                "@type": "ImageObject",
+                "url": "https://www.kingarthurbaking.com/sites/default/files/recipe_legacy/1418-3-large.jpg"
+            },
+            "aggregateRating": {
+                "@type": "AggregateRating",
+                "ratingValue": "4.70",
+                "ratingCount": "204"
+            }
+        }
+    ]
+}
+        </script>
+    </head>
+</html>
diff --git a/tests/test_schemaorg.py b/tests/test_schemaorg.py
index bb2c37bc..41137f57 100644
--- a/tests/test_schemaorg.py
+++ b/tests/test_schemaorg.py
@@ -14,3 +14,10 @@ class TestSchemaOrg(unittest.TestCase):
             if k in self.schema.data:
                 del self.schema.data[k]
         self.assertEqual(self.schema.total_time(), 0)
+
+    def test_graph_schema_without_context(self):
+        with open(
+            "tests/test_data/schemaorg_graph.testhtml", encoding="utf-8"
+        ) as pagedata:
+            schema = SchemaOrg(pagedata.read())
+        self.assertNotEqual(schema.data, {})