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

1from dataclasses import dataclass 1a

2 

3from slugify import slugify 1a

4 

5from mealie.repos.repository_factory import AllRepositories 1a

6from mealie.schema.recipe import TagOut 1a

7from mealie.schema.recipe.recipe_category import TagSave 1a

8 

9 

10class NoContextException(Exception): 1a

11 pass 1a

12 

13 

14@dataclass(slots=True) 1a

15class ScraperContext: 1a

16 repos: AllRepositories 1a

17 

18 

19class ScrapedExtras: 1a

20 def __init__(self) -> None: 1a

21 self._tags: list[str] = [] 

22 

23 def set_tags(self, tags: list[str]) -> None: 1a

24 self._tags = tags 

25 

26 def use_tags(self, ctx: ScraperContext) -> list[TagOut]: 1a

27 if not self._tags: 

28 return [] 

29 

30 repo = ctx.repos.tags 

31 

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 

38 

39 seen_tag_slugs.add(slugify_tag) 

40 

41 # Check if tag exists 

42 if db_tag := repo.get_one(slugify_tag, "slug"): 

43 tags.append(db_tag) 

44 continue 

45 

46 save_data = TagSave(name=tag, group_id=ctx.repos.group_id) 

47 db_tag = repo.create(save_data) 

48 

49 tags.append(db_tag) 

50 

51 return tags