[foods] Add catch for common domain exclusions
This commit is contained in:
3
vrobbler/apps/foods/constants.py
Normal file
3
vrobbler/apps/foods/constants.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
EXCLUDED_FOOD_DOMAINS = ["bbc.co.uk", "bbc.com", "www.bbc.com"]
|
||||||
@ -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 import scrape_html
|
||||||
from recipe_scrapers._exceptions import (
|
from recipe_scrapers._exceptions import (
|
||||||
NoSchemaFoundInWildMode,
|
NoSchemaFoundInWildMode,
|
||||||
SchemaOrgException,
|
SchemaOrgException,
|
||||||
)
|
)
|
||||||
import requests
|
|
||||||
from typing import Dict, Optional, List
|
logger = logging.getLogger(__name__)
|
||||||
from django.core.files.base import ContentFile
|
|
||||||
|
|
||||||
|
|
||||||
class RecipeScraperService:
|
class RecipeScraperService:
|
||||||
@ -73,6 +79,10 @@ class RecipeScraperService:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def is_recipe_url(cls, url: str) -> bool:
|
def is_recipe_url(cls, url: str) -> bool:
|
||||||
"""Check if a URL points to a recipe by fetching and checking the HTML."""
|
"""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:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
url,
|
url,
|
||||||
@ -86,7 +96,6 @@ class RecipeScraperService:
|
|||||||
return False
|
return False
|
||||||
return cls.is_recipe(response.text, url)
|
return cls.is_recipe(response.text, url)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _safe_call(self, scraper, method, default=None):
|
def _safe_call(self, scraper, method, default=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user