Coverage for opt/mealie/lib/python3.12/site-packages/mealie/services/scraper/scraped_extras.py: 34%
33 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 dataclasses import dataclass 1a
3from slugify import slugify 1a
5from mealie.repos.repository_factory import AllRepositories 1a
6from mealie.schema.recipe import TagOut 1a
7from mealie.schema.recipe.recipe_category import TagSave 1a
10class NoContextException(Exception): 1a
11 pass 1a
14@dataclass(slots=True) 1a
15class ScraperContext: 1a
16 repos: AllRepositories 1a
19class ScrapedExtras: 1a
20 def __init__(self) -> None: 1a
21 self._tags: list[str] = []
23 def set_tags(self, tags: list[str]) -> None: 1a
24 self._tags = tags
26 def use_tags(self, ctx: ScraperContext) -> list[TagOut]: 1a
27 if not self._tags:
28 return []
30 repo = ctx.repos.tags
32 tags = []
33 seen_tag_slugs: set[str] = set()
34 for tag in self._tags:
35 slugify_tag = slugify(tag)
36 if slugify_tag in seen_tag_slugs:
37 continue
39 seen_tag_slugs.add(slugify_tag)
41 # Check if tag exists
42 if db_tag := repo.get_one(slugify_tag, "slug"):
43 tags.append(db_tag)
44 continue
46 save_data = TagSave(name=tag, group_id=ctx.repos.group_id)
47 db_tag = repo.create(save_data)
49 tags.append(db_tag)
51 return tags