improved cookbook app importer
This commit is contained in:
parent
08aa5cc36e
commit
66794d619f
@ -4,9 +4,12 @@ import json
|
|||||||
import re
|
import re
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
|
import requests
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from cookbook.helper.ingredient_parser import IngredientParser
|
from cookbook.helper.ingredient_parser import IngredientParser
|
||||||
|
from cookbook.helper.recipe_html_import import get_recipe_from_source
|
||||||
|
from cookbook.helper.recipe_url_import import iso_duration_to_minutes
|
||||||
from cookbook.integration.integration import Integration
|
from cookbook.integration.integration import Integration
|
||||||
from cookbook.models import Recipe, Step, Ingredient, Keyword
|
from cookbook.models import Recipe, Step, Ingredient, Keyword
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _
|
||||||
@ -15,53 +18,51 @@ from gettext import gettext as _
|
|||||||
class CookBookApp(Integration):
|
class CookBookApp(Integration):
|
||||||
|
|
||||||
def import_file_name_filter(self, zip_info_object):
|
def import_file_name_filter(self, zip_info_object):
|
||||||
return zip_info_object.filename.endswith('.yml')
|
return zip_info_object.filename.endswith('.html')
|
||||||
|
|
||||||
def get_recipe_from_file(self, file):
|
def get_recipe_from_file(self, file):
|
||||||
recipe_yml = yaml.safe_load(file.getvalue().decode("utf-8"))
|
recipe_html = file.getvalue().decode("utf-8")
|
||||||
|
|
||||||
|
recipe_json, recipe_tree, html_data, images = get_recipe_from_source(recipe_html, 'CookBookApp', self.request)
|
||||||
|
|
||||||
recipe = Recipe.objects.create(
|
recipe = Recipe.objects.create(
|
||||||
name=recipe_yml['name'].strip(),
|
name=recipe_json['name'].strip(),
|
||||||
created_by=self.request.user, internal=True,
|
created_by=self.request.user, internal=True,
|
||||||
space=self.request.space)
|
space=self.request.space)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
recipe.servings = re.findall('([0-9])+', recipe_yml['recipeYield'])[0]
|
recipe.servings = re.findall('([0-9])+', recipe_json['recipeYield'])[0]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
recipe.working_time = recipe_yml['prep_time'].replace(' minutes', '')
|
recipe.working_time = iso_duration_to_minutes(recipe_json['prep_time'])
|
||||||
recipe.waiting_time = recipe_yml['cook_time'].replace(' minutes', '')
|
recipe.waiting_time = iso_duration_to_minutes(recipe_json['cook_time'])
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if recipe_yml['on_favorites']:
|
step = Step.objects.create(instruction=recipe_json['recipeInstructions'], space=self.request.space, )
|
||||||
recipe.keywords.add(Keyword.objects.get_or_create(name=_('Favorites'), space=self.request.space))
|
|
||||||
|
|
||||||
step = Step.objects.create(instruction=recipe_yml['directions'], space=self.request.space, )
|
if 'nutrition' in recipe_json:
|
||||||
|
step.instruction = step.instruction + '\n\n' + recipe_json['nutrition']
|
||||||
if 'notes' in recipe_yml and recipe_yml['notes'].strip() != '':
|
|
||||||
step.instruction = step.instruction + '\n\n' + recipe_yml['notes']
|
|
||||||
|
|
||||||
if 'nutritional_info' in recipe_yml:
|
|
||||||
step.instruction = step.instruction + '\n\n' + recipe_yml['nutritional_info']
|
|
||||||
|
|
||||||
if 'source' in recipe_yml and recipe_yml['source'].strip() != '':
|
|
||||||
step.instruction = step.instruction + '\n\n' + recipe_yml['source']
|
|
||||||
|
|
||||||
step.save()
|
step.save()
|
||||||
recipe.steps.add(step)
|
recipe.steps.add(step)
|
||||||
|
|
||||||
ingredient_parser = IngredientParser(self.request, True)
|
ingredient_parser = IngredientParser(self.request, True)
|
||||||
for ingredient in recipe_yml['ingredients'].split('\n'):
|
for ingredient in recipe_json['recipeIngredient']:
|
||||||
if ingredient.strip() != '':
|
f = ingredient_parser.get_food(ingredient['ingredient']['text'])
|
||||||
amount, unit, ingredient, note = ingredient_parser.parse(ingredient)
|
u = ingredient_parser.get_unit(ingredient['unit']['text'])
|
||||||
f = ingredient_parser.get_food(ingredient)
|
|
||||||
u = ingredient_parser.get_unit(unit)
|
|
||||||
step.ingredients.add(Ingredient.objects.create(
|
step.ingredients.add(Ingredient.objects.create(
|
||||||
food=f, unit=u, amount=amount, note=note, space=self.request.space,
|
food=f, unit=u, amount=ingredient['amount'], note=ingredient['note'], space=self.request.space,
|
||||||
))
|
))
|
||||||
|
|
||||||
|
if len(images) > 0:
|
||||||
|
try:
|
||||||
|
response = requests.get(images[0])
|
||||||
|
self.import_recipe_image(recipe, BytesIO(response.content))
|
||||||
|
except Exception as e:
|
||||||
|
print('failed to import image ', str(e))
|
||||||
|
|
||||||
recipe.save()
|
recipe.save()
|
||||||
return recipe
|
return recipe
|
||||||
|
@ -269,7 +269,8 @@ class Integration:
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError('Method not implemented in integration')
|
raise NotImplementedError('Method not implemented in integration')
|
||||||
|
|
||||||
def handle_exception(self, exception, log=None, message=''):
|
@staticmethod
|
||||||
|
def handle_exception(exception, log=None, message=''):
|
||||||
if log:
|
if log:
|
||||||
if message:
|
if message:
|
||||||
log.msg += message
|
log.msg += message
|
||||||
|
@ -36,7 +36,7 @@ Overview of the capabilities of the different integrations.
|
|||||||
| RezKonv | ✔️ | ❌ | ❌ |
|
| RezKonv | ✔️ | ❌ | ❌ |
|
||||||
| OpenEats | ✔️ | ❌ | ⌚ |
|
| OpenEats | ✔️ | ❌ | ⌚ |
|
||||||
| Plantoeat | ✔️ | ❌ | ✔ |
|
| Plantoeat | ✔️ | ❌ | ✔ |
|
||||||
| CookBookApp | ✔️ | ⌚ | ❌ |
|
| CookBookApp | ✔️ | ⌚ | ✔️ |
|
||||||
|
|
||||||
✔ = implemented, ❌ = not implemented and not possible/planned, ⌚ = not yet implemented
|
✔ = implemented, ❌ = not implemented and not possible/planned, ⌚ = not yet implemented
|
||||||
|
|
||||||
@ -217,4 +217,4 @@ Plan to eat allow to export a text file containing all your recipes. Simply uplo
|
|||||||
|
|
||||||
## CookBookApp
|
## CookBookApp
|
||||||
|
|
||||||
CookBookApp can export .zip files containing .yml files. Upload the entire ZIP to Tandoor to import all conluded recipes.
|
CookBookApp can export .zip files containing .html files. Upload the entire ZIP to Tandoor to import all included recipes.
|
Loading…
Reference in New Issue
Block a user