itte/ramenkan.py

180 lines
6.5 KiB
Python
Raw Normal View History

2018-09-17 23:17:00 +00:00
from mastodon import Mastodon
from random import randint
2018-09-16 04:48:50 +00:00
from itte import IRC, Util
class Ramen:
2018-09-14 05:54:21 +00:00
"""Requests with a ramen theme."""
2018-09-14 05:54:21 +00:00
def main(self):
2018-09-14 23:37:47 +00:00
"""Instantiate an IRC object and attach the listeners."""
2018-09-16 04:48:50 +00:00
# Load yaml sources used by request handlers
self.util = Util()
2018-09-17 03:51:09 +00:00
self.rand = self.util.rand
2018-09-17 23:17:00 +00:00
self.misc = self.util.yml("ramenkan/misc.yml")
2018-09-16 04:48:50 +00:00
self.links = self.util.yml("ramenkan/links.yml")
2018-09-17 16:27:05 +00:00
self.photos = self.util.yml("ramenkan/photos.yml")
2018-09-17 03:51:09 +00:00
self.dishes = self.util.yml("ramenkan/dishes.yml")
2018-09-16 04:48:50 +00:00
# Init irc object
2018-09-14 05:54:21 +00:00
self.irc = IRC()
2018-09-17 16:27:05 +00:00
self.cfg = self.irc.config("ramenkan/config.yml")
2018-09-17 23:17:00 +00:00
# Init mastodon object
self.masto = Mastodon(
api_base_url=self.cfg["mastodon"]["base_url"],
access_token=self.cfg["mastodon"]["access_token"],
client_id=self.cfg["mastodon"]["client_id"],
client_secret=self.cfg["mastodon"]["client_secret"]
)
# Init request listeners
2018-09-15 23:18:44 +00:00
self.irc.run(self.add_listeners)
2018-09-14 23:37:47 +00:00
def add_listeners(self, cxt):
"""Map triggers to handlers."""
2018-09-15 23:18:44 +00:00
self.irc.listen(cxt, "exit " + self.cfg["admin"]["code"], self.quit, \
admin=True)
2018-09-14 23:37:47 +00:00
self.irc.listen(cxt, "rollcall", self.rollcall)
self.irc.listen(cxt, "help", self.rollcall)
2018-09-15 23:18:44 +00:00
self.irc.listen(cxt, "water " + self.cfg["bot_nick"], self.water)
self.irc.listen(cxt, "botsnack " + self.cfg["bot_nick"], self.botsnack)
2018-09-17 03:51:09 +00:00
self.irc.listen(cxt, "ramen", self.ramen)
2018-09-17 23:23:34 +00:00
self.irc.listen(cxt, "vramen", self.ramen_veggie)
2018-09-17 03:51:09 +00:00
self.irc.listen(cxt, "rk", self.ramen)
self.irc.listen(cxt, "rkveg", self.ramen_veggie)
2018-09-16 04:48:50 +00:00
self.irc.listen(cxt, "rklink", self.link)
2018-09-17 16:27:05 +00:00
self.irc.listen(cxt, "rkselfie", self.selfie)
2018-09-17 23:17:00 +00:00
self.irc.listen(cxt, "rktoot", self.toot)
2018-09-17 23:23:34 +00:00
self.irc.listen(cxt, "rkvtoot", self.toot_veggie)
2018-09-14 23:37:47 +00:00
def quit(self, cxt):
"""Disconnect from the server and quit."""
self.irc.disconnect("Okay, okay, I'll leave. (´・ω・`)", "noodling off")
2018-09-14 23:37:47 +00:00
def rollcall(self, cxt):
"""Handle request for app info."""
2018-09-17 23:17:00 +00:00
self.irc.reply(cxt, self.misc["rollcall"])
2018-09-14 23:37:47 +00:00
def water(self, cxt):
"""Handle water offer."""
2018-09-17 23:17:00 +00:00
resp = self.misc["water"]
for index, r in enumerate(resp):
if "{{ nick }}" in r:
resp.append(r.replace("{{ nick }}", cxt["msg"]["nick"]))
resp.pop(index)
2018-09-17 03:51:09 +00:00
self.irc.reply(cxt, self.rand(resp))
2018-09-14 23:37:47 +00:00
def botsnack(self, cxt):
"""Handle snack offer."""
2018-09-17 23:17:00 +00:00
self.irc.reply(cxt, self.rand(self.misc["botsnack"]))
2018-09-17 03:51:09 +00:00
def make_ramen_combo(self, *args, **kwargs):
"""Generate a ramen dish. Optionally pass `veggie=True` for a
vegetarian dish."""
2018-09-17 03:51:09 +00:00
dish = self.dishes
# Check vegetarian flag
is_veggie = kwargs.get("veggie", False)
2018-09-17 03:51:09 +00:00
combo = "ramen in "
# Noodle type and broth richness
if randint(0, 1):
combo = self.rand(dish["noodle-shape"]) + ", " + \
self.rand(dish["noodle-broth-type"]) + " "
combo = combo.capitalize()
# Broth type
if is_veggie:
combo += self.rand(dish["broth-veggie"])
else:
combo += self.rand(dish["broth"] + dish["broth-veggie"])
combo += " broth"
2018-09-17 03:51:09 +00:00
# Topping
2018-10-05 15:31:06 +00:00
n_top = randint(2, 5)
2018-09-17 03:51:09 +00:00
for n in range(2, n_top+1):
if n == 2:
if is_veggie:
prev_top = self.rand(dish["topping"])
else:
2018-09-17 16:27:05 +00:00
prev_top = self.rand(dish["topping"] + dish["meat"])
2018-09-17 03:51:09 +00:00
combo += " with " + prev_top
else:
if n > 2 and n < n_top:
combo += ", "
else:
combo += " and "
if is_veggie:
next_top = self.rand(dish["topping"])
else:
next_top = self.rand(dish["topping"])
2018-09-17 03:51:09 +00:00
# Check for duplicate
if next_top == prev_top:
2018-09-17 16:27:05 +00:00
next_top = self.rand(dish["topping"])
2018-09-17 03:51:09 +00:00
combo += next_top
prev_top = next_top
# Condiment and side dish
if randint(0, 1):
2018-09-17 16:27:05 +00:00
combo += ", sprinkled with " + self.rand(dish["condiment"]) + "."
2018-09-17 03:51:09 +00:00
else:
combo += "."
if randint(0, 1):
combo += " Served with "
if is_veggie:
combo += self.rand(dish["tapa-veggie"]) + "."
else:
combo += self.rand(dish["tapa"] + dish["tapa-veggie"]) + "."
2018-09-17 03:51:09 +00:00
return combo
2018-09-17 23:17:00 +00:00
def pick_ramen(self, *args, **kwargs):
"""Pick a ramen dish. Optionally set vegetarian selection with
`veggie=True`."""
2018-09-17 03:51:09 +00:00
roll = randint(1, 100)
2018-09-17 23:17:00 +00:00
veggie = kwargs.get("veggie", False)
2018-09-17 03:51:09 +00:00
# 1% possibility of regional preset
2018-09-17 23:17:00 +00:00
if roll == 100 and veggie:
pick = (self.dishes["set-veggie"] + ".").capitalize()
elif roll == 100 and not veggie:
pick = (self.rand(self.dishes["set"] + \
2018-09-17 03:51:09 +00:00
self.dishes["set-veggie"]) + ".").capitalize()
2018-09-17 23:17:00 +00:00
elif roll <= 99 and veggie:
pick = self.make_ramen_combo(veggie=True)
2018-09-17 03:51:09 +00:00
else:
2018-09-17 23:17:00 +00:00
pick = self.make_ramen_combo()
return pick
def ramen(self, cxt):
"""Handle ramen request."""
self.irc.reply(cxt, self.pick_ramen())
def ramen_veggie(self, cxt):
"""Handle vegetarian ramen request."""
2018-09-17 23:17:00 +00:00
self.irc.reply(cxt, self.pick_ramen(veggie=True))
2018-09-16 04:48:50 +00:00
def link(self, cxt):
2018-09-17 16:27:05 +00:00
"""Handle to display a titled link."""
2018-09-17 03:51:09 +00:00
index = randint(0, len(self.links)-1)
self.irc.reply(cxt, self.links[index]["title"] + " " + \
self.links[index]["link"])
2018-09-16 04:48:50 +00:00
2018-09-17 16:27:05 +00:00
def selfie(self, cxt):
"""Handle to display a photo link."""
self.irc.reply(cxt, self.rand(self.photos["ticket"]))
2018-10-10 16:48:05 +00:00
def toot_wrapper(self, cxt, txt):
"""Wrap around the Mastodon library's toot command for basic error
handling."""
try:
self.masto.toot(txt)
self.irc.reply(cxt, txt + " " + self.misc["toot"]["success"])
except:
self.irc.reply(cxt, self.misc["toot"]["error"])
pass
2018-09-17 23:17:00 +00:00
def toot(self, cxt):
"""Handle post ramen to Mastodon."""
2018-10-10 16:48:05 +00:00
self.toot_wrapper(cxt, self.pick_ramen())
2018-09-17 23:17:00 +00:00
def toot_veggie(self, cxt):
"""Handle post veggie ramen to Mastodon."""
2018-10-10 16:48:05 +00:00
self.toot_wrapper(cxt, self.pick_ramen(veggie=True))
2018-09-17 23:17:00 +00:00
app = Ramen()
2018-09-14 05:54:21 +00:00
app.main()