aboutsummaryrefslogtreecommitdiffstats
path: root/jeeves/recipes.py
blob: ae0efbf651c114c031b2feef43964ac2b945141b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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)