diff --git a/ramenkan.py b/ramenkan.py index 9089a73..39e1757 100755 --- a/ramenkan.py +++ b/ramenkan.py @@ -1,3 +1,4 @@ +from mastodon import Mastodon from random import randint from itte import IRC, Util @@ -11,13 +12,21 @@ class Ramen: # Load yaml sources used by request handlers self.util = Util() self.rand = self.util.rand - self.std = self.util.yml("ramenkan/standard.yml") + self.misc = self.util.yml("ramenkan/misc.yml") self.links = self.util.yml("ramenkan/links.yml") self.photos = self.util.yml("ramenkan/photos.yml") self.dishes = self.util.yml("ramenkan/dishes.yml") # Init irc object self.irc = IRC() self.cfg = self.irc.config("ramenkan/config.yml") + # 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 self.irc.run(self.add_listeners) def add_listeners(self, cxt): @@ -34,6 +43,7 @@ class Ramen: self.irc.listen(cxt, "rkveg", self.ramen_veggie) self.irc.listen(cxt, "rklink", self.link) self.irc.listen(cxt, "rkselfie", self.selfie) + self.irc.listen(cxt, "rktoot", self.toot) def quit(self, cxt): """Disconnect from the server and quit.""" @@ -41,11 +51,11 @@ class Ramen: def rollcall(self, cxt): """Handle request for app info.""" - self.irc.reply(cxt, self.std["rollcall"]) + self.irc.reply(cxt, self.misc["rollcall"]) def water(self, cxt): """Handle water offer.""" - resp = self.std["water"] + resp = self.misc["water"] for index, r in enumerate(resp): if "{{ nick }}" in r: resp.append(r.replace("{{ nick }}", cxt["msg"]["nick"])) @@ -54,7 +64,7 @@ class Ramen: def botsnack(self, cxt): """Handle snack offer.""" - self.irc.reply(cxt, self.rand(self.std["botsnack"])) + self.irc.reply(cxt, self.rand(self.misc["botsnack"])) def make_ramen_combo(self, *args, **kwargs): """Generate a ramen dish. Optionally pass `veggie=True` for a @@ -110,25 +120,30 @@ class Ramen: combo += self.rand(dish["tapa"] + dish["tapa-veggie"]) + "." return combo + def pick_ramen(self, *args, **kwargs): + """Pick a ramen dish. Optionally set vegetarian selection with + `veggie=True`.""" + roll = randint(1, 100) + veggie = kwargs.get("veggie", False) + # 1% possibility of regional preset + if roll == 100 and veggie: + pick = (self.dishes["set-veggie"] + ".").capitalize() + elif roll == 100 and not veggie: + pick = (self.rand(self.dishes["set"] + \ + self.dishes["set-veggie"]) + ".").capitalize() + elif roll <= 99 and veggie: + pick = self.make_ramen_combo(veggie=True) + else: + pick = self.make_ramen_combo() + return pick + def ramen(self, cxt): """Handle ramen request.""" - roll = randint(1, 100) - # 1% possibility of regional preset - if roll == 100: - resp = (self.rand(self.dishes["set"] + \ - self.dishes["set-veggie"]) + ".").capitalize() - else: - resp = self.make_ramen_combo() - self.irc.reply(cxt, resp) + self.irc.reply(cxt, self.pick_ramen()) def ramen_veggie(self, cxt): """Handle vegetarian ramen request.""" - roll = randint(1, 100) - if roll == 100: - resp = (self.dishes["set-veggie"] + ".").capitalize() - else: - resp = self.make_ramen_combo(veggie=True) - self.irc.reply(cxt, resp) + self.irc.reply(cxt, self.pick_ramen(veggie=True)) def link(self, cxt): """Handle to display a titled link.""" @@ -140,6 +155,19 @@ class Ramen: """Handle to display a photo link.""" self.irc.reply(cxt, self.rand(self.photos["ticket"])) + def toot(self, cxt): + """Handle post ramen to Mastodon.""" + pick = self.pick_ramen() + self.masto.toot(pick) + self.irc.reply(cxt, pick + " " + self.misc["toot"]) + + def toot_veggie(self, cxt): + """Handle post veggie ramen to Mastodon.""" + pick = self.pick_ramen(veggie=True) + self.masto.toot(pick) + self.irc.reply(cxt, pick + " " + self.misc["toot"]) + + app = Ramen() app.main() diff --git a/ramenkan/dishes.yml b/ramenkan/dishes.yml index f7a522a..4758ed4 100644 --- a/ramenkan/dishes.yml +++ b/ramenkan/dishes.yml @@ -33,7 +33,7 @@ condiment: - butter - chili oil - rayu # Japanese chili oil - - chili pepper + - chili peppers - chili sauce - sesame oil - dashi # fish and seaweed stock diff --git a/ramenkan/standard.yml b/ramenkan/misc.yml similarity index 89% rename from ramenkan/standard.yml rename to ramenkan/misc.yml index 1c2b175..80bf739 100644 --- a/ramenkan/standard.yml +++ b/ramenkan/misc.yml @@ -1,7 +1,7 @@ rollcall: "一、二、三、らーめん缶! Hello, I am a ramen vending machine. Please type a code for service: - !help !ramen !veggieramen !rklink !rkselfie + !help !ramen !veggieramen !rklink !rkselfie !rktoot - Support: +81 012-700-1MIO どうぞめしあがれ。" water: @@ -21,3 +21,5 @@ water: botsnack: - "CHIKIN RAAAAAMEN━━━(゜∀゜)━━━!!!!!" - "Ramen time anytime! 自o(´▽` )/" + +toot: "Now shared with Mastodon! (^v^)"