diff --git a/recipe_scrapers/__init__.py b/recipe_scrapers/__init__.py index 361c378a..9b67ce5d 100644 --- a/recipe_scrapers/__init__.py +++ b/recipe_scrapers/__init__.py @@ -5,6 +5,7 @@ __all__ = ( "ElementNotFoundInHtml", "FieldNotProvidedByWebsiteException", "NoSchemaFoundInWildMode", + "RecipeSchemaNotFound", "StaticValueException", "WebsiteNotImplementedError", "scrape_html", @@ -26,6 +27,7 @@ from ._exceptions import ( ElementNotFoundInHtml, FieldNotProvidedByWebsiteException, NoSchemaFoundInWildMode, + RecipeSchemaNotFound, StaticValueException, WebsiteNotImplementedError, ) diff --git a/recipe_scrapers/_exceptions.py b/recipe_scrapers/_exceptions.py index 458e791e..6ee05228 100644 --- a/recipe_scrapers/_exceptions.py +++ b/recipe_scrapers/_exceptions.py @@ -64,6 +64,15 @@ class StaticValueException(RecipeScrapersExceptions): super().__init__(message) +class RecipeSchemaNotFound(SchemaOrgException): + """No schema.org recipe metadata found on the page.""" + + def __init__(self, url): + self.url = url + message = f"No Recipe Schema found at {url}." + super().__init__(message) + + class FieldNotProvidedByWebsiteException(StaticValueException): """Error when, as far as we know, the website does not provide this info for any recipes.""" diff --git a/recipe_scrapers/plugins/schemaorg_fill.py b/recipe_scrapers/plugins/schemaorg_fill.py index d7e05e69..6e2b0c40 100644 --- a/recipe_scrapers/plugins/schemaorg_fill.py +++ b/recipe_scrapers/plugins/schemaorg_fill.py @@ -1,7 +1,7 @@ import functools import logging -from recipe_scrapers._exceptions import FillPluginException +from recipe_scrapers._exceptions import FillPluginException, RecipeSchemaNotFound from recipe_scrapers.settings import settings from ._interface import PluginInterface @@ -56,8 +56,10 @@ class SchemaOrgFillPlugin(PluginInterface): try: return decorated(self, *args, **kwargs) except (FillPluginException, NotImplementedError) as e: + if not self.schema.data: + raise RecipeSchemaNotFound(self.url) function = getattr(self.schema, decorated.__name__) - if self.schema.data and function: + if function: logger.info( f"{class_name}.{method_name}() seems to not be implemented but .schema is available! Attempting to return result from SchemaOrg." )