[foods] Add catch for common domain exclusions
All checks were successful
build & deploy / test (push) Successful in 1m42s
build & deploy / deploy (push) Successful in 23s

This commit is contained in:
2026-03-14 13:45:15 -04:00
parent 74252b8759
commit a5ff6abf56
2 changed files with 16 additions and 4 deletions

View File

@ -0,0 +1,3 @@
#!/usr/bin/env python3
EXCLUDED_FOOD_DOMAINS = ["bbc.co.uk", "bbc.com", "www.bbc.com"]

View File

@ -1,11 +1,17 @@
import logging
from typing import Dict, List, Optional
from urllib.parse import urlparse
import requests
from django.core.files.base import ContentFile
from foods.constants import EXCLUDED_FOOD_DOMAINS
from recipe_scrapers import scrape_html
from recipe_scrapers._exceptions import (
NoSchemaFoundInWildMode,
SchemaOrgException,
)
import requests
from typing import Dict, Optional, List
from django.core.files.base import ContentFile
logger = logging.getLogger(__name__)
class RecipeScraperService:
@ -73,6 +79,10 @@ class RecipeScraperService:
@classmethod
def is_recipe_url(cls, url: str) -> bool:
"""Check if a URL points to a recipe by fetching and checking the HTML."""
domain = urlparse(url).netloc
if domain in EXCLUDED_FOOD_DOMAINS and "food" not in url:
logger.info("[food] domain found in EXCLUDED_FOOD_DOMAINS")
return False
try:
response = requests.get(
url,
@ -86,7 +96,6 @@ class RecipeScraperService:
return False
return cls.is_recipe(response.text, url)
except Exception as e:
print(e)
return False
def _safe_call(self, scraper, method, default=None):