Coverage for cookbook/integration/plantoeat.py: 12%
78 statements
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 00:47 +0100
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 00:47 +0100
1from io import BytesIO
3import requests
4import validators
6from cookbook.helper.ingredient_parser import IngredientParser
7from cookbook.integration.integration import Integration
8from cookbook.models import Ingredient, Keyword, Recipe, Step
11class Plantoeat(Integration):
13 def get_recipe_from_file(self, file):
14 ingredient_mode = False
15 direction_mode = False
17 image_url = None
18 tags = None
19 ingredients = []
20 directions = []
21 description = ''
22 for line in file.replace('\r', '').split('\n'):
23 if line.strip() != '':
24 if 'Title:' in line:
25 title = line.replace('Title:', '').replace('"', '').strip()
26 if 'Description:' in line:
27 description = line.replace('Description:', '').strip()
28 if 'Source:' in line or 'Serves:' in line or 'Prep Time:' in line or 'Cook Time:' in line:
29 directions.append(line.strip() + '\n')
30 if 'Photo Url:' in line:
31 image_url = line.replace('Photo Url:', '').strip()
32 if 'Tags:' in line:
33 tags = line.replace('Tags:', '').strip()
34 if ingredient_mode:
35 if len(line) > 2 and 'Instructions:' not in line:
36 ingredients.append(line.strip())
37 if direction_mode:
38 if len(line) > 2:
39 directions.append(line.strip() + '\n')
40 if 'Ingredients:' in line:
41 ingredient_mode = True
42 if 'Directions:' in line:
43 ingredient_mode = False
44 direction_mode = True
46 recipe = Recipe.objects.create(name=title, description=description, created_by=self.request.user, internal=True, space=self.request.space)
48 step = Step.objects.create(
49 instruction='\n'.join(directions) + '\n\n', space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients,
50 )
52 if tags:
53 tags = tags.replace('^',',')
54 for k in tags.split(','):
55 keyword, created = Keyword.objects.get_or_create(name=k.strip(), space=self.request.space)
56 recipe.keywords.add(keyword)
58 ingredient_parser = IngredientParser(self.request, True)
59 for ingredient in ingredients:
60 if len(ingredient.strip()) > 0:
61 amount, unit, food, note = ingredient_parser.parse(ingredient)
62 f = ingredient_parser.get_food(food)
63 u = ingredient_parser.get_unit(unit)
64 step.ingredients.add(Ingredient.objects.create(
65 food=f, unit=u, amount=amount, note=note, original_text=ingredient, space=self.request.space,
66 ))
67 recipe.steps.add(step)
69 if image_url:
70 try:
71 if validators.url(image_url, public=True):
72 response = requests.get(image_url)
73 self.import_recipe_image(recipe, BytesIO(response.content))
74 except Exception as e:
75 print('failed to import image ', str(e))
77 return recipe
79 def split_recipe_file(self, file):
80 recipe_list = []
81 current_recipe = ''
83 for fl in file.readlines():
84 try:
85 line = fl.decode("utf-8")
86 except UnicodeDecodeError:
87 line = fl.decode("windows-1250")
89 if line.startswith('--------------'):
90 if current_recipe != '':
91 recipe_list.append(current_recipe)
92 current_recipe = ''
93 else:
94 current_recipe = ''
95 else:
96 current_recipe += line + '\n'
98 if current_recipe != '':
99 recipe_list.append(current_recipe)
101 return recipe_list