diff options
Diffstat (limited to 'jeeves/recipes.py')
| -rw-r--r-- | jeeves/recipes.py | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/jeeves/recipes.py b/jeeves/recipes.py new file mode 100644 index 0000000..ae0efbf --- /dev/null +++ b/jeeves/recipes.py @@ -0,0 +1,123 @@ +""" +Sopel plugin: jeeves recipes + +Egyptian, Middle Eastern and Western recipes. + + !breakfast [list] - random breakfast dish or list all + !lunch [list] - random lunch dish or list all + !dinner [list] - random dinner dish or list all + !snack [list] - random snack or list all + !recipe <name> - full recipe (up to 3 lines) +""" + +import os as _os, sys as _sys +_d = _os.path.dirname(_os.path.abspath(__file__)) +if _d not in _sys.path: + _sys.path.insert(0, _d) +import jv_core as jv + +import json +import random + +from sopel import plugin + + +def setup(bot): + jv.ensure_setup(bot) + path = bot.memory["jv_recipes_path"] + try: + with open(path) as f: + bot.memory["jv_recipes"] = json.load(f) + except Exception: + bot.memory["jv_recipes"] = {"breakfast": [], "lunch": [], "dinner": [], "snack": []} + + +def _fmt_random(category, dish): + return ( + f"{category.capitalize()} suggestion: " + f"{dish['name']} | {dish['description']}. " + f"Type !recipe {dish['name'].lower()} for the full recipe." + ) + + +def _fmt_list(category, dishes): + names = ", ".join(d["name"] for d in dishes) + return f"{category.capitalize()} dishes ({len(dishes)}): {names}" + + +def _fmt_recipe(dish): + line1 = f"{dish['name']} | {dish['description']}" + line2 = "Ingredients: " + ", ".join(dish["ingredients"]) + steps = " ".join(f"{i+1}. {s.rstrip('.')}." for i, s in enumerate(dish["steps"])) + return line1, line2, steps + + +def _all_dishes(bot): + for category, dishes in bot.memory.get("jv_recipes", {}).items(): + for dish in dishes: + yield category, dish + + +def _find_dish(bot, query): + q = query.strip().lower() + for _, dish in _all_dishes(bot): + if dish["name"].lower() == q: + return dish + for _, dish in _all_dishes(bot): + if q in dish["name"].lower(): + return dish + return None + + +def _meal_cmd(bot, trigger, category): + arg = (trigger.group(2) or "").strip().lower() + dishes = bot.memory.get("jv_recipes", {}).get(category, []) + + if not dishes: + bot.say(f"no {category} recipes loaded", trigger.sender) + return + + if arg == "list": + bot.say(_fmt_list(category, dishes), trigger.sender) + return + + bot.say(_fmt_random(category, random.choice(dishes)), trigger.sender) + + +@plugin.command("breakfast") +@plugin.require_chanmsg +def cmd_breakfast(bot, trigger): + _meal_cmd(bot, trigger, "breakfast") + + +@plugin.command("lunch") +@plugin.require_chanmsg +def cmd_lunch(bot, trigger): + _meal_cmd(bot, trigger, "lunch") + + +@plugin.command("dinner") +@plugin.require_chanmsg +def cmd_dinner(bot, trigger): + _meal_cmd(bot, trigger, "dinner") + + +@plugin.command("snack") +@plugin.require_chanmsg +def cmd_snack(bot, trigger): + _meal_cmd(bot, trigger, "snack") + + +@plugin.command("recipe") +@plugin.require_chanmsg +def cmd_recipe(bot, trigger): + query = (trigger.group(2) or "").strip() + if not query: + bot.say("usage: !recipe <dish name>", trigger.sender) + return + dish = _find_dish(bot, query) + if not dish: + bot.say(f"no recipe found for '{query}'", trigger.sender) + return + for line in _fmt_recipe(dish): + bot.say(line, trigger.sender) |
