fixed response error

This commit is contained in:
hay-kot 2021-02-13 12:26:05 -09:00
commit 6d4fde2f77
2 changed files with 22 additions and 8 deletions

View file

@ -36,4 +36,4 @@ async def delete_recipe_category(
db.categories.delete(session, category)
return SnackResponse(f"Category Deleted: {category}")
return SnackResponse.error(f"Category Deleted: {category}")

View file

@ -40,7 +40,11 @@ def normalize_instructions(instructions) -> List[dict]:
# One long string split by (possibly multiple) new lines
if type(instructions) == str:
return [{"text": normalize_instruction(line)} for line in instructions.splitlines() if line]
return [
{"text": normalize_instruction(line)}
for line in instructions.splitlines()
if line
]
# Plain strings in a list
elif type(instructions) == list and type(instructions[0]) == str:
@ -64,7 +68,9 @@ def normalize_instructions(instructions) -> List[dict]:
]
return [
{"text": normalize_instruction(step["text"])} for step in instructions if step["@type"] == "HowToStep"
{"text": normalize_instruction(step["text"])}
for step in instructions
if step["@type"] == "HowToStep"
]
except Exception as e:
# Not "@type", try "type"
@ -111,8 +117,12 @@ def normalize_data(recipe_data: dict) -> dict:
recipe_data["prepTime"] = normalize_time(recipe_data.get("prepTime"))
recipe_data["performTime"] = normalize_time(recipe_data.get("performTime"))
recipe_data["recipeYield"] = normalize_yield(recipe_data.get("recipeYield"))
recipe_data["recipeIngredient"] = normalize_ingredient(recipe_data.get("recipeIngredient"))
recipe_data["recipeInstructions"] = normalize_instructions(recipe_data["recipeInstructions"])
recipe_data["recipeIngredient"] = normalize_ingredient(
recipe_data.get("recipeIngredient")
)
recipe_data["recipeInstructions"] = normalize_instructions(
recipe_data["recipeInstructions"]
)
recipe_data["image"] = normalize_image_url(recipe_data["image"])
return recipe_data
@ -136,11 +146,15 @@ def process_recipe_data(new_recipe: dict, url=None) -> dict:
def extract_recipe_from_html(html: str, url: str) -> dict:
try:
scraped_recipes: List[dict] = scrape_schema_recipe.loads(html, python_objects=True)
scraped_recipes: List[dict] = scrape_schema_recipe.loads(
html, python_objects=True
)
dump_last_json(scraped_recipes)
if not scraped_recipes:
scraped_recipes: List[dict] = scrape_schema_recipe.scrape_url(url, python_objects=True)
scraped_recipes: List[dict] = scrape_schema_recipe.scrape_url(
url, python_objects=True
)
except Exception as e:
# trying without python_objects
scraped_recipes: List[dict] = scrape_schema_recipe.loads(html)
@ -170,7 +184,7 @@ def download_image_for_recipe(recipe: dict) -> dict:
img_path = scrape_image(recipe.get("image"), recipe.get("slug"))
recipe["image"] = img_path.name
except:
recipe["image"] = None
recipe["image"] = "no image"
return recipe