Coverage for opt/mealie/lib/python3.12/site-packages/mealie/services/scraper/recipe_scraper.py: 70%
34 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:48 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:48 +0000
1from mealie.core.root_logger import get_logger 1b
2from mealie.lang.providers import Translator 1b
3from mealie.schema.recipe.recipe import Recipe 1b
4from mealie.services.scraper import cleaner 1b
5from mealie.services.scraper.scraped_extras import ScrapedExtras 1b
7from .scraper_strategies import ( 1b
8 ABCScraperStrategy,
9 RecipeScraperOpenAI,
10 RecipeScraperOpenGraph,
11 RecipeScraperPackage,
12 safe_scrape_html,
13)
15DEFAULT_SCRAPER_STRATEGIES: list[type[ABCScraperStrategy]] = [ 1b
16 RecipeScraperPackage,
17 RecipeScraperOpenAI,
18 RecipeScraperOpenGraph,
19]
22class RecipeScraper: 1b
23 """
24 Scrapes recipes from the web.
25 """
27 # List of recipe scrapers. Note that order matters
28 scrapers: list[type[ABCScraperStrategy]] 1b
30 def __init__(self, translator: Translator, scrapers: list[type[ABCScraperStrategy]] | None = None) -> None: 1b
31 if scrapers is None: 31 ↛ 34line 31 didn't jump to line 34 because the condition on line 31 was always true1ca
32 scrapers = DEFAULT_SCRAPER_STRATEGIES 1ca
34 self.scrapers = scrapers 1ca
35 self.translator = translator 1ca
36 self.logger = get_logger() 1ca
38 async def scrape(self, url: str, html: str | None = None) -> tuple[Recipe, ScrapedExtras] | tuple[None, None]: 1b
39 """
40 Scrapes a recipe from the web.
41 Skips the network request if `html` is provided.
42 """
44 raw_html = html or await safe_scrape_html(url)
45 for scraper_type in self.scrapers:
46 scraper = scraper_type(url, self.translator, raw_html=raw_html)
48 try:
49 result = await scraper.parse()
50 except Exception:
51 self.logger.exception(f"Failed to scrape HTML with {scraper.__class__.__name__}")
52 result = None
54 if result is None or result[0] is None: 54 ↛ 57line 54 didn't jump to line 57 because the condition on line 54 was always true
55 continue
57 recipe_result, extras = result
58 try:
59 recipe = cleaner.clean(recipe_result, self.translator)
60 except Exception:
61 self.logger.exception(f"Failed to clean recipe data from {scraper.__class__.__name__}")
62 continue
64 return recipe, extras
66 return None, None