mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-01-25 18:13:11 -05:00
init 2
This commit is contained in:
40
mealie/services/scrape_services.py
Normal file
40
mealie/services/scrape_services.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from scrape_schema_recipe import scrape_url
|
||||
from slugify import slugify
|
||||
|
||||
from services.image_services import scrape_image
|
||||
from services.recipe_services import Recipe
|
||||
|
||||
|
||||
def create_from_url(url: str) -> dict:
|
||||
recipe_data = process_recipe_url(url)
|
||||
recipe = Recipe(**recipe_data)
|
||||
|
||||
return recipe.save_to_db()
|
||||
|
||||
|
||||
def process_recipe_url(url: str) -> dict:
|
||||
new_recipe: dict = scrape_url(url, python_objects=True)[0]
|
||||
|
||||
if not new_recipe:
|
||||
return "fail" # TODO: Return Better Error Here
|
||||
|
||||
slug = slugify(new_recipe["name"])
|
||||
mealie_tags = {
|
||||
"slug": slug,
|
||||
"orgURL": url,
|
||||
"categories": [],
|
||||
"tags": [],
|
||||
"dateAdded": None,
|
||||
"notes": [],
|
||||
"extras": [],
|
||||
}
|
||||
|
||||
new_recipe.update(mealie_tags)
|
||||
|
||||
try:
|
||||
img_path = scrape_image(new_recipe.get("image"), slug)
|
||||
new_recipe["image"] = img_path.name
|
||||
except:
|
||||
new_recipe["image"] = None
|
||||
|
||||
return new_recipe
|
||||
Reference in New Issue
Block a user