Coverage for cookbook/integration/copymethat.py: 14%

94 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-29 00:47 +0100

1from io import BytesIO 

2from zipfile import ZipFile 

3 

4from bs4 import BeautifulSoup, Tag 

5from django.utils.translation import gettext as _ 

6 

7from cookbook.helper.ingredient_parser import IngredientParser 

8from cookbook.helper.recipe_url_import import iso_duration_to_minutes, parse_servings 

9from cookbook.integration.integration import Integration 

10from cookbook.models import Ingredient, Keyword, Recipe, Step 

11from recipes.settings import DEBUG 

12 

13 

14class CopyMeThat(Integration): 

15 

16 def import_file_name_filter(self, zip_info_object): 

17 if DEBUG: 

18 print("testing", zip_info_object.filename, zip_info_object.filename == 'recipes.html') 

19 return zip_info_object.filename == 'recipes.html' 

20 

21 def get_recipe_from_file(self, file): 

22 # 'file' comes is as a beautifulsoup object 

23 try: 

24 source = file.find("a", {"id": "original_link"}).text 

25 except AttributeError: 

26 source = None 

27 

28 recipe = Recipe.objects.create(name=file.find("div", {"id": "name"}).text.strip( 

29 )[:128], source_url=source, created_by=self.request.user, internal=True, space=self.request.space, ) 

30 

31 for category in file.find_all("span", {"class": "recipeCategory"}): 

32 keyword, created = Keyword.objects.get_or_create(name=category.text, space=self.request.space) 

33 recipe.keywords.add(keyword) 

34 

35 try: 

36 recipe.servings = parse_servings(file.find("a", {"id": "recipeYield"}).text.strip()) 

37 recipe.working_time = iso_duration_to_minutes(file.find("span", {"meta": "prepTime"}).text.strip()) 

38 recipe.waiting_time = iso_duration_to_minutes(file.find("span", {"meta": "cookTime"}).text.strip()) 

39 except AttributeError: 

40 pass 

41 

42 try: 

43 if len(file.find("span", {"id": "starred"}).text.strip()) > 0: 

44 recipe.keywords.add(Keyword.objects.get_or_create(space=self.request.space, name=_('Favorite'))[0]) 

45 except AttributeError: 

46 pass 

47 

48 try: 

49 if len(file.find("span", {"id": "made_this"}).text.strip()) > 0: 

50 recipe.keywords.add(Keyword.objects.get_or_create(space=self.request.space, name=_('I made this'))[0]) 

51 except AttributeError: 

52 pass 

53 

54 step = Step.objects.create(instruction='', space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients, ) 

55 

56 ingredient_parser = IngredientParser(self.request, True) 

57 

58 ingredients = file.find("ul", {"id": "recipeIngredients"}) 

59 if isinstance(ingredients, Tag): 

60 for ingredient in ingredients.children: 

61 if not isinstance(ingredient, Tag) or not ingredient.text.strip() or "recipeIngredient_spacer" in ingredient['class']: 

62 continue 

63 if any(x in ingredient['class'] for x in ["recipeIngredient_subheader", "recipeIngredient_note"]): 

64 step.ingredients.add( 

65 Ingredient.objects.create( 

66 is_header=True, 

67 note=ingredient.text.strip()[ 

68 :256], 

69 original_text=ingredient.text.strip(), 

70 space=self.request.space, 

71 )) 

72 else: 

73 amount, unit, food, note = ingredient_parser.parse(ingredient.text.strip()) 

74 f = ingredient_parser.get_food(food) 

75 u = ingredient_parser.get_unit(unit) 

76 step.ingredients.add(Ingredient.objects.create(food=f, unit=u, amount=amount, note=note, original_text=ingredient.text.strip(), space=self.request.space, )) 

77 

78 instructions = file.find("ol", {"id": "recipeInstructions"}) 

79 if isinstance(instructions, Tag): 

80 for instruction in instructions.children: 

81 if not isinstance(instruction, Tag) or instruction.text == "": 

82 continue 

83 if "instruction_subheader" in instruction['class']: 

84 if step.instruction: 

85 step.save() 

86 recipe.steps.add(step) 

87 step = Step.objects.create(instruction='', space=self.request.space, ) 

88 

89 step.name = instruction.text.strip()[:128] 

90 else: 

91 step.instruction += instruction.text.strip() + ' \n\n' 

92 

93 notes = file.find_all("li", {"class": "recipeNote"}) 

94 if notes: 

95 step.instruction += '*Notes:* \n\n' 

96 

97 for n in notes: 

98 if n.text == "": 

99 continue 

100 step.instruction += '*' + n.text.strip() + '* \n\n' 

101 

102 description = '' 

103 try: 

104 description = file.find("div", {"id": "description"}).text.strip() 

105 except AttributeError: 

106 pass 

107 if len(description) <= 512: 

108 recipe.description = description 

109 else: 

110 recipe.description = description[:480] + ' ... (full description below)' 

111 step.instruction += '*Description:* \n\n*' + description + '* \n\n' 

112 

113 step.save() 

114 recipe.steps.add(step) 

115 

116 # import the Primary recipe image that is stored in the Zip 

117 try: 

118 for f in self.files: 

119 if '.zip' in f['name']: 

120 import_zip = ZipFile(f['file']) 

121 self.import_recipe_image(recipe, BytesIO(import_zip.read(file.find("img", class_="recipeImage").get("src"))), filetype='.jpeg') 

122 except Exception as e: 

123 print(recipe.name, ': failed to import image ', str(e)) 

124 

125 recipe.save() 

126 return recipe 

127 

128 def split_recipe_file(self, file): 

129 soup = BeautifulSoup(file, "html.parser") 

130 return soup.find_all("div", {"class": "recipe"})