From 58c5b2c301dfb417ca0267aabdf934ea2de16673 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 9 Feb 2021 16:10:28 +0100 Subject: [PATCH] add paprika import image support --- cookbook/integration/mealie.py | 4 ---- cookbook/integration/nextcloud_cookbook.py | 4 ---- cookbook/integration/paprika.py | 20 ++++++++++++++++++++ docs/features/import_export.md | 20 ++++++++++++++++---- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/cookbook/integration/mealie.py b/cookbook/integration/mealie.py index 1153791aa..8099f6658 100644 --- a/cookbook/integration/mealie.py +++ b/cookbook/integration/mealie.py @@ -3,18 +3,14 @@ import re from io import BytesIO from zipfile import ZipFile -from rest_framework.renderers import JSONRenderer - from cookbook.helper.ingredient_parser import parse from cookbook.integration.integration import Integration from cookbook.models import Recipe, Step, Food, Unit, Ingredient -from cookbook.serializer import RecipeExportSerializer class Mealie(Integration): def import_file_name_filter(self, zip_info_object): - print("testing", zip_info_object.filename) return re.match(r'^recipes/([A-Za-z\d-])+.json$', zip_info_object.filename) def get_recipe_from_file(self, file): diff --git a/cookbook/integration/nextcloud_cookbook.py b/cookbook/integration/nextcloud_cookbook.py index 80cdff3ca..7d5f57934 100644 --- a/cookbook/integration/nextcloud_cookbook.py +++ b/cookbook/integration/nextcloud_cookbook.py @@ -3,18 +3,14 @@ import re from io import BytesIO from zipfile import ZipFile -from rest_framework.renderers import JSONRenderer - from cookbook.helper.ingredient_parser import parse from cookbook.integration.integration import Integration from cookbook.models import Recipe, Step, Food, Unit, Ingredient -from cookbook.serializer import RecipeExportSerializer class NextcloudCookbook(Integration): def import_file_name_filter(self, zip_info_object): - print("testing", zip_info_object.filename) return re.match(r'^Recipes/([A-Za-z\d\s])+/recipe.json$', zip_info_object.filename) def get_recipe_from_file(self, file): diff --git a/cookbook/integration/paprika.py b/cookbook/integration/paprika.py index 2b3a8b264..520c90b68 100644 --- a/cookbook/integration/paprika.py +++ b/cookbook/integration/paprika.py @@ -1,6 +1,10 @@ import json +import re +from io import BytesIO +from zipfile import ZipFile import microdata +from bs4 import BeautifulSoup from cookbook.helper.recipe_url_import import find_recipe_json from cookbook.integration.integration import Integration @@ -9,6 +13,10 @@ from cookbook.models import Recipe, Step, Food, Ingredient, Unit class Paprika(Integration): + def import_file_name_filter(self, zip_info_object): + print("testing", zip_info_object.filename) + return re.match(r'^Recipes/([A-Za-z\s])+.html$', zip_info_object.filename) + def get_file_from_recipe(self, recipe): raise NotImplementedError('Method not implemented in storage integration') @@ -33,4 +41,16 @@ class Paprika(Integration): )) recipe.steps.add(step) + + soup = BeautifulSoup(html_text, "html.parser") + image = soup.find('img') + image_name = image.attrs['src'].strip().replace('Images/', '') + + for f in self.files: + if '.zip' in f.name: + import_zip = ZipFile(f.file) + for z in import_zip.filelist: + if re.match(f'^Recipes/Images/{image_name}$', z.filename): + self.import_recipe_image(recipe, BytesIO(import_zip.read(z.filename))) + return recipe diff --git a/docs/features/import_export.md b/docs/features/import_export.md index 650aec2a6..d80bd91bc 100644 --- a/docs/features/import_export.md +++ b/docs/features/import_export.md @@ -61,7 +61,19 @@ To migrate your recipes Paprika can create two types of export. The first is a proprietary `.paprikarecipes` file in some kind of binarized format. The second one is HTML files containing at least a bit of microdata. -If you want to import your Paprika recipes create a html export. Then import the individual recipes HTML files. -Due to the lack of structure not all fields can be imported. -Even tough images are present in the export they cannot be imported atm. This is technically possible and might be -added in the future. \ No newline at end of file +If you want to import your Paprika recipes follow these steps + +1. create a html export +2. Create a `.zip` file from the `Recipes` folder (next to the `index.html`) the structure should look like this +``` +Recipes.zip/ + └── Recipes/ + ├── recipe one.html + ├── recipe two.thml + └── Images/ + ├── 5D5E09CD-8F88-4F61-8121-0727DD3E0E89/ + │ └── 5D5E09CD-8F88-4F61-8121-0727DD3E0E89.jpg + └── 7CEE2AC6-DF60-4464-9B61-4F5E347EB90C/ + └── 7CEE2AC6-DF60-4464-9B61-4F5E347EB90C.jpg +``` +3. Upload the zip file in the import module and import it \ No newline at end of file