Last active
March 3, 2025 23:08
-
-
Save wsargent/1d2da5d5834cef1dbcf5c72c8ffe542e to your computer and use it in GitHub Desktop.
Find recipes in mealie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
from urllib.parse import urljoin | |
def find_recipes_in_mealie( | |
searchTerm: str, | |
categories_csv: str = None, | |
tags_csv: str = None) -> str: | |
""" | |
Search for recipes in Mealie using various filters. | |
Parameters | |
---------- | |
searchTerm : str | |
The string to search for, i.e. "chicken" | |
categories_csv : str, optional | |
Comma separated list of category names or slugs to filter by | |
tags_csv : str, optional | |
Comma seperated list of tag names or slugs to filter by | |
Returns: | |
str: The list of recipes, or "No recipes found" if no recipes are found. | |
""" | |
def parse_recipe_json(recipe) -> str: | |
# print(json.dumps(recipe, indent=4)) | |
#id = recipe["id"] | |
name = recipe["name"] | |
slug = recipe["slug"] | |
total_time = recipe["totalTime"] | |
prep_time = recipe["prepTime"] | |
description = recipe["description"] | |
recipe_categories = ",".join([category['name'] for category in recipe["recipeCategory"]]) | |
recipe_tags = ",".join([tag['name'] for tag in recipe["tags"]]) | |
perform_time = recipe["performTime"] | |
recipe_servings = recipe["recipeServings"] | |
recipe_yield_quantity = recipe["recipeYieldQuantity"] | |
recipe_original_url = recipe["orgURL"] | |
return f"""--- | |
Name: {name} | |
Slug: {slug} | |
Original URL: {recipe_original_url} | |
Prep Time: {prep_time} | |
Perform Time: {perform_time} | |
Total Time: {total_time} | |
Categories: {recipe_categories} | |
Tags: {recipe_tags} | |
Servings: {recipe_servings} | |
Yield: {recipe_yield_quantity} | |
Description: | |
{description} | |
""" | |
base_url = os.getenv("MEALIE_ENDPOINT") | |
api_key = os.getenv("MEALIE_API_KEY") | |
headers = { | |
"accept": "application/json", | |
"Authorization": "Bearer " + api_key | |
} | |
endpoint = urljoin(base_url, '/api/recipes') | |
params = { | |
'page': 1, | |
'perPage': 10, | |
} | |
if searchTerm: | |
strippedTerm = searchTerm.strip() | |
params['search'] = strippedTerm | |
if categories_csv: | |
params['categories'] = [category.strip() for category in categories_csv.split(",")] | |
if tags_csv: | |
params['tags'] = [tag.strip() for tag in tags_csv.split(",")] | |
response = requests.get(endpoint, headers=headers, params=params) | |
response.raise_for_status() | |
recipes_json = response.json() | |
items = recipes_json["items"] | |
if (len(items) == 0): | |
return "No recipes found" | |
else: | |
recipes = "" | |
for item in items: | |
recipes += parse_recipe_json(item) | |
return recipes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment