Coverage for cookbook/integration/paprika.py: 21%

70 statements  

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

1import base64 

2import gzip 

3import json 

4import re 

5from gettext import gettext as _ 

6from io import BytesIO 

7 

8import requests 

9import validators 

10 

11from cookbook.helper.ingredient_parser import IngredientParser 

12from cookbook.helper.recipe_url_import import parse_servings, parse_servings_text 

13from cookbook.integration.integration import Integration 

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

15 

16 

17class Paprika(Integration): 

18 

19 def get_file_from_recipe(self, recipe): 

20 raise NotImplementedError('Method not implemented in storage integration') 

21 

22 def get_recipe_from_file(self, file): 

23 with gzip.open(file, 'r') as recipe_zip: 

24 recipe_json = json.loads(recipe_zip.read().decode("utf-8")) 

25 

26 recipe = Recipe.objects.create( 

27 name=recipe_json['name'].strip(), created_by=self.request.user, internal=True, space=self.request.space) 

28 

29 if 'description' in recipe_json: 

30 recipe.description = '' if len(recipe_json['description'].strip()) > 500 else recipe_json['description'].strip() 

31 

32 try: 

33 if 'servings' in recipe_json: 

34 recipe.servings = parse_servings(recipe_json['servings']) 

35 recipe.servings_text = parse_servings_text(recipe_json['servings']) 

36 

37 if len(recipe_json['cook_time'].strip()) > 0: 

38 recipe.waiting_time = re.findall(r'\d+', recipe_json['cook_time'])[0] 

39 

40 if len(recipe_json['prep_time'].strip()) > 0: 

41 recipe.working_time = re.findall(r'\d+', recipe_json['prep_time'])[0] 

42 except Exception: 

43 pass 

44 

45 recipe.save() 

46 

47 instructions = recipe_json['directions'] 

48 if recipe_json['notes'] and len(recipe_json['notes'].strip()) > 0: 

49 instructions += '\n\n### ' + _('Notes') + ' \n' + recipe_json['notes'] 

50 

51 if recipe_json['nutritional_info'] and len(recipe_json['nutritional_info'].strip()) > 0: 

52 instructions += '\n\n### ' + _('Nutritional Information') + ' \n' + recipe_json['nutritional_info'] 

53 

54 try: 

55 if len(recipe_json['source'].strip()) > 0 or len(recipe_json['source_url'].strip()) > 0: 

56 instructions += '\n\n### ' + _('Source') + ' \n' + recipe_json['source'].strip() + ' \n' + recipe_json['source_url'].strip() 

57 except AttributeError: 

58 pass 

59 

60 step = Step.objects.create( 

61 instruction=instructions, space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients, 

62 ) 

63 

64 if 'description' in recipe_json and len(recipe_json['description'].strip()) > 500: 

65 step.instruction = recipe_json['description'].strip() + '\n\n' + step.instruction 

66 

67 if 'categories' in recipe_json: 

68 for c in recipe_json['categories']: 

69 keyword, created = Keyword.objects.get_or_create(name=c.strip(), space=self.request.space) 

70 recipe.keywords.add(keyword) 

71 

72 ingredient_parser = IngredientParser(self.request, True) 

73 try: 

74 for ingredient in recipe_json['ingredients'].split('\n'): 

75 if len(ingredient.strip()) > 0: 

76 amount, unit, food, note = ingredient_parser.parse(ingredient) 

77 f = ingredient_parser.get_food(food) 

78 u = ingredient_parser.get_unit(unit) 

79 step.ingredients.add(Ingredient.objects.create( 

80 food=f, unit=u, amount=amount, note=note, original_text=ingredient, space=self.request.space, 

81 )) 

82 except AttributeError: 

83 pass 

84 

85 recipe.steps.add(step) 

86 

87 try: 

88 if recipe_json.get("image_url", None): 

89 url = recipe_json.get("image_url", None) 

90 if validators.url(url, public=True): 

91 response = requests.get(url) 

92 self.import_recipe_image(recipe, BytesIO(response.content)) 

93 except Exception: 

94 if recipe_json.get("photo_data", None): 

95 self.import_recipe_image(recipe, BytesIO(base64.b64decode(recipe_json['photo_data'])), filetype='.jpeg') 

96 

97 return recipe