Improve parsing of OpenEats imports

This commit is contained in:
Thomas Preece 2023-03-20 18:18:11 +00:00
parent 87db9124d0
commit a1b8f736c2
2 changed files with 60 additions and 6 deletions

View File

@ -2,24 +2,48 @@ import json
from cookbook.helper.ingredient_parser import IngredientParser from cookbook.helper.ingredient_parser import IngredientParser
from cookbook.integration.integration import Integration from cookbook.integration.integration import Integration
from cookbook.models import Ingredient, Recipe, Step from cookbook.models import Ingredient, Recipe, Step, Keyword, Comment, CookLog
class OpenEats(Integration): class OpenEats(Integration):
def get_recipe_from_file(self, file): def get_recipe_from_file(self, file):
recipe = Recipe.objects.create(name=file['name'].strip(), created_by=self.request.user, internal=True, recipe = Recipe.objects.create(name=file['name'].strip(), description=file['info'], created_by=self.request.user, internal=True,
servings=file['servings'], space=self.request.space, waiting_time=file['cook_time'], working_time=file['prep_time']) servings=file['servings'], space=self.request.space, waiting_time=file['cook_time'], working_time=file['prep_time'])
instructions = '' instructions = ''
if file["info"] != '':
instructions += file["info"]
if file["directions"] != '': if file["directions"] != '':
instructions += file["directions"] instructions += file["directions"]
if file["source"] != '': if file["source"] != '':
instructions += file["source"] instructions += f'\nRecipe source: [{file["source"]}]({file["source"]})'
cuisine_keyword, created = Keyword.objects.get_or_create(name="Cuisine", space=self.request.space)
if file["cuisine"] != '':
keyword, created = Keyword.objects.get_or_create(name=file["cuisine"].strip(), space=self.request.space)
if created:
keyword.move(cuisine_keyword, pos="last-child")
recipe.keywords.add(keyword)
course_keyword, created = Keyword.objects.get_or_create(name="Course", space=self.request.space)
if file["course"] != '':
keyword, created = Keyword.objects.get_or_create(name=file["course"].strip(), space=self.request.space)
if created:
keyword.move(course_keyword, pos="last-child")
recipe.keywords.add(keyword)
for tag in file["tags"]:
keyword, created = Keyword.objects.get_or_create(name=tag.strip(), space=self.request.space)
recipe.keywords.add(keyword)
for comment in file['comments']:
Comment.objects.create(recipe=recipe, text=comment['text'], created_by=self.request.user)
CookLog.objects.create(recipe=recipe, rating=comment['rating'], created_by=self.request.user, space=self.request.space)
if file["photo"] != '':
recipe.image = f'recipes/openeats-import/{file["photo"]}'
recipe.save()
step = Step.objects.create(instruction=instructions, space=self.request.space,) step = Step.objects.create(instruction=instructions, space=self.request.space,)
@ -38,6 +62,9 @@ class OpenEats(Integration):
recipe_json = json.loads(file.read()) recipe_json = json.loads(file.read())
recipe_dict = {} recipe_dict = {}
ingredient_group_dict = {} ingredient_group_dict = {}
cuisine_group_dict = {}
course_group_dict = {}
tag_group_dict = {}
for o in recipe_json: for o in recipe_json:
if o['model'] == 'recipe.recipe': if o['model'] == 'recipe.recipe':
@ -50,11 +77,27 @@ class OpenEats(Integration):
'cook_time': o['fields']['cook_time'], 'cook_time': o['fields']['cook_time'],
'servings': o['fields']['servings'], 'servings': o['fields']['servings'],
'ingredients': [], 'ingredients': [],
'photo': o['fields']['photo'],
'cuisine': o['fields']['cuisine'],
'course': o['fields']['course'],
'tags': o['fields']['tags'],
'comments': [],
} }
if o['model'] == 'ingredient.ingredientgroup': if o['model'] == 'ingredient.ingredientgroup':
ingredient_group_dict[o['pk']] = o['fields']['recipe'] ingredient_group_dict[o['pk']] = o['fields']['recipe']
if o['model'] == 'recipe_groups.cuisine':
cuisine_group_dict[o['pk']] = o['fields']['title']
if o['model'] == 'recipe_groups.course':
course_group_dict[o['pk']] = o['fields']['title']
if o['model'] == 'recipe_groups.tag':
tag_group_dict[o['pk']] = o['fields']['title']
for o in recipe_json: for o in recipe_json:
if o['model'] == 'rating.rating':
recipe_dict[o['fields']['recipe']]["comments"].append({
"text": o['fields']['comment'],
"rating": o['fields']['rating']
})
if o['model'] == 'ingredient.ingredient': if o['model'] == 'ingredient.ingredient':
ingredient = { ingredient = {
'food': o['fields']['title'], 'food': o['fields']['title'],
@ -63,6 +106,15 @@ class OpenEats(Integration):
} }
recipe_dict[ingredient_group_dict[o['fields']['ingredient_group']]]['ingredients'].append(ingredient) recipe_dict[ingredient_group_dict[o['fields']['ingredient_group']]]['ingredients'].append(ingredient)
for k, r in recipe_dict.items():
if r["cuisine"] in cuisine_group_dict:
r["cuisine"] = cuisine_group_dict[r["cuisine"]]
if r["course"] in course_group_dict:
r["course"] = course_group_dict[r["course"]]
for index in range(len(r["tags"])):
if r["tags"][index] in tag_group_dict:
r["tags"][index] = tag_group_dict[r["tags"][index]]
return list(recipe_dict.values()) return list(recipe_dict.values())
def get_file_from_recipe(self, recipe): def get_file_from_recipe(self, recipe):

View File

@ -178,7 +178,7 @@ This zip file can simply be imported into Tandoor.
OpenEats does not provide any way to export the data using the interface. Luckily it is relatively easy to export it from the command line. OpenEats does not provide any way to export the data using the interface. Luckily it is relatively easy to export it from the command line.
You need to run the command `python manage.py dumpdata recipe ingredient` inside of the application api container. You need to run the command `python manage.py dumpdata recipe ingredient` inside of the application api container.
If you followed the default installation method you can use the following command `docker-compose -f docker-prod.yml run --rm --entrypoint 'sh' api ./manage.py dumpdata recipe ingredient`. If you followed the default installation method you can use the following command `docker-compose -f docker-prod.yml run --rm --entrypoint 'sh' api ./manage.py dumpdata recipe ingredient`.
This command might also work `docker exec -it openeats_api_1 ./manage.py dumpdata recipe ingredient > recipe_ingredients.json` This command might also work `docker exec -it openeats_api_1 ./manage.py dumpdata recipe ingredient rating recipe_groups > recipe_ingredients.json`
Store the outputted json string in a `.json` file and simply import it using the importer. The file should look something like this Store the outputted json string in a `.json` file and simply import it using the importer. The file should look something like this
```json ```json
@ -216,6 +216,8 @@ Store the outputted json string in a `.json` file and simply import it using the
``` ```
To import your images you'll need to create the folder `openeats-import` in your Tandoor's `recipes` media folder (which is usually found inside `/opt/recipes/mediafiles`). After that you'll need to copy the `/code/site-media/upload` folder from the openeats API docker container to the `openeats` folder you created. You should now have the file path `/opt/recipes/mediafiles/recipes/openeats-import/upload/...` in Tandoor.
## Plantoeat ## Plantoeat
Plan to eat allows you to export a text file containing all your recipes. Simply upload that text file to Tandoor to import all recipes Plan to eat allows you to export a text file containing all your recipes. Simply upload that text file to Tandoor to import all recipes