Improve recipe import for non schema.org recipes

This commit is contained in:
Daniel Schulz 2022-03-03 23:46:56 +01:00
parent 8d3419952c
commit cfcf354d59
No known key found for this signature in database
GPG Key ID: 907BA47E4F808EF2

View File

@ -27,6 +27,11 @@ def get_from_scraper(scrape, request):
except Exception: except Exception:
recipe_json['name'] = '' recipe_json['name'] = ''
try:
description = scrape.description() or None
except Exception:
description = None
if not description:
try: try:
description = scrape.schema.data.get("description") or '' description = scrape.schema.data.get("description") or ''
except Exception: except Exception:
@ -50,10 +55,16 @@ def get_from_scraper(scrape, request):
servings = 1 servings = 1
recipe_json['servings'] = max(servings, 1) recipe_json['servings'] = max(servings, 1)
try:
recipe_json['prepTime'] = get_minutes(scrape.prep_time()) or 0
except Exception:
try: try:
recipe_json['prepTime'] = get_minutes(scrape.schema.data.get("prepTime")) or 0 recipe_json['prepTime'] = get_minutes(scrape.schema.data.get("prepTime")) or 0
except Exception: except Exception:
recipe_json['prepTime'] = 0 recipe_json['prepTime'] = 0
try:
recipe_json['cookTime'] = get_minutes(scrape.cook_time()) or 0
except Exception:
try: try:
recipe_json['cookTime'] = get_minutes(scrape.schema.data.get("cookTime")) or 0 recipe_json['cookTime'] = get_minutes(scrape.schema.data.get("cookTime")) or 0
except Exception: except Exception:
@ -84,11 +95,19 @@ def get_from_scraper(scrape, request):
keywords += listify_keywords(scrape.schema.data.get("keywords")) keywords += listify_keywords(scrape.schema.data.get("keywords"))
except Exception: except Exception:
pass pass
try:
if scrape.category():
keywords += listify_keywords(scrape.category())
except Exception:
try: try:
if scrape.schema.data.get('recipeCategory'): if scrape.schema.data.get('recipeCategory'):
keywords += listify_keywords(scrape.schema.data.get("recipeCategory")) keywords += listify_keywords(scrape.schema.data.get("recipeCategory"))
except Exception: except Exception:
pass pass
try:
if scrape.cuisine():
keywords += listify_keywords(scrape.cuisine())
except Exception:
try: try:
if scrape.schema.data.get('recipeCuisine'): if scrape.schema.data.get('recipeCuisine'):
keywords += listify_keywords(scrape.schema.data.get("recipeCuisine")) keywords += listify_keywords(scrape.schema.data.get("recipeCuisine"))
@ -146,9 +165,9 @@ def get_from_scraper(scrape, request):
except Exception: except Exception:
recipe_json['recipeInstructions'] = "" recipe_json['recipeInstructions'] = ""
if scrape.url: if scrape.canonical_url():
recipe_json['url'] = scrape.url recipe_json['url'] = scrape.canonical_url()
recipe_json['recipeInstructions'] += "\n\n" + _("Imported from") + ": " + scrape.url recipe_json['recipeInstructions'] += "\n\n" + _("Imported from") + ": " + scrape.canonical_url()
return recipe_json return recipe_json