From 090e18e405da56a67b8740ebed72c3d74b737a38 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 4 Apr 2022 21:30:00 +0200 Subject: [PATCH] paprika importer improvements --- cookbook/helper/recipe_url_import.py | 13 +++++++++++-- cookbook/integration/paprika.py | 8 ++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index ae6fecc4..e303989f 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -31,7 +31,7 @@ def get_from_scraper(scrape, request): recipe_json['name'] = '' try: - description = scrape.description() or None + description = scrape.description() or None except Exception: description = None if not description: @@ -130,7 +130,7 @@ def get_from_scraper(scrape, request): if len(recipe_json['steps']) == 0: recipe_json['steps'].append({'instruction': '', 'ingredients': [], }) - if len(parse_description(description)) > 256: # split at 256 as long descriptions dont look good on recipe cards + if len(parse_description(description)) > 256: # split at 256 as long descriptions dont look good on recipe cards recipe_json['steps'][0]['instruction'] = f'*{parse_description(description)}* \n\n' + recipe_json['steps'][0]['instruction'] else: recipe_json['description'] = parse_description(description)[:512] @@ -255,6 +255,15 @@ def parse_servings(servings): return servings +def parse_servings_text(servings): + if type(servings) == str: + try: + servings = re.sub("\d+", '', servings).strip() + except Exception: + servings = '' + return servings + + def parse_time(recipe_time): if type(recipe_time) not in [int, float]: try: diff --git a/cookbook/integration/paprika.py b/cookbook/integration/paprika.py index 7a125500..9a82c4aa 100644 --- a/cookbook/integration/paprika.py +++ b/cookbook/integration/paprika.py @@ -6,6 +6,7 @@ from gettext import gettext as _ from io import BytesIO from cookbook.helper.ingredient_parser import IngredientParser +from cookbook.helper.recipe_url_import import parse_servings, parse_servings_text from cookbook.integration.integration import Integration from cookbook.models import Ingredient, Keyword, Recipe, Step @@ -26,10 +27,9 @@ class Paprika(Integration): recipe.description = '' if len(recipe_json['description'].strip()) > 500 else recipe_json['description'].strip() try: - if re.match(r'([0-9])+\s(.)*', recipe_json['servings']): - s = recipe_json['servings'].split(' ') - recipe.servings = s[0] - recipe.servings_text = s[1] + if 'servings' in recipe_json['servings']: + recipe.servings = parse_servings(recipe_json['servings']) + recipe.servings_text = parse_servings_text(recipe_json['servings']) if len(recipe_json['cook_time'].strip()) > 0: recipe.waiting_time = re.findall(r'\d+', recipe_json['cook_time'])[0]