From 8ccb6a6e60febbdd3c5f5f028430a80afa7082c5 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Wed, 13 Nov 2024 05:41:05 +1100 Subject: [PATCH] pubbot: Post prize names --- zulip_bots/zulip_bots/pubbot.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/zulip_bots/zulip_bots/pubbot.py b/zulip_bots/zulip_bots/pubbot.py index 8bece91..1bdfb62 100644 --- a/zulip_bots/zulip_bots/pubbot.py +++ b/zulip_bots/zulip_bots/pubbot.py @@ -72,6 +72,26 @@ def _get_giveaway(): return None +prize_cache = {} +def get_prize_name(id): + if id not in prize_cache: + try: + prize_cache[id] = _get_prize_name(id) + except Exception: + logging.warning(f"Failed to get prize title for {id}", exc_info=True) + return "Unknown prize" + return prize_cache[id] + + +def _get_prize_name(id): + resp = requests.get(f"https://desertbus.org/2024/prize/{id}", {"User-Agent": ""}) + resp.raise_for_status() + html = BeautifulSoup(resp.content.decode(), "html.parser") + div = html.body.main.find("div", **{"class": lambda cl: "text-brand-gold" in cl}) + # These divs have format "Silent Auction: NAME", "Giveaway: NAME", etc. Split after first ": ". + return div.string.split(": ", 1)[-1].strip() + + def main(conf_file, message_log_file, name=socket.gethostname()): """Config: zulip_url @@ -143,6 +163,8 @@ def main(conf_file, message_log_file, name=socket.gethostname()): log["type"] = "prize" prize_id = msg["c"].removeprefix("bid:") log["prize_id"] = prize_id + prize_name = get_prize_name(prize_id) + log["prize_name"] = prize_name data = msg["d"] logging.info(f"Prize update for {prize_id}: {data}") if "name" in data and "amount" in data: @@ -152,7 +174,7 @@ def main(conf_file, message_log_file, name=socket.gethostname()): client.send_to_stream( "bot-spam", "Bids", - f"At , {data['name']} ({data['donorID']}) has the high bid of ${amount:.2f} for prize [{prize_id}](https://desertbus.org/prize/{prize_id})", + f"At , {data['name']} ({data['donorID']}) has the high bid of ${amount:.2f} for prize [{prize_name}](https://desertbus.org/prize/{prize_id})", ) else: