From 3b1c837bfa1945e8b0abfe2d8d28ff7263e3850e Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Tue, 29 Oct 2024 04:28:37 +0000 Subject: [PATCH] Import prizebot --- docker-compose.jsonnet | 18 ++++++++++ zulip_bots/zulip_bots/prizebot.py | 59 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 zulip_bots/zulip_bots/prizebot.py diff --git a/docker-compose.jsonnet b/docker-compose.jsonnet index 3408ef9..216475a 100644 --- a/docker-compose.jsonnet +++ b/docker-compose.jsonnet @@ -36,6 +36,7 @@ twitchbot: false, pubbot: false, blogbot: false, + prizebot: false, bus_analyzer: false, graphs: false, }, @@ -348,6 +349,15 @@ prize_ids: [], }, + prizebot:: { + email: "blog-bot@chat.videostrike.team", + api_key: "", + state: "/prizebot_state.json", + // Path in host fs for the state file. + // Must exist and be initialized to "{}" + state_path:: "./prizebot_state.json", + } + // template for donation data urls donation_url_template:: "https://example.com/DB{}/DB{}.json", @@ -787,6 +797,14 @@ }, ["--save-dir", "/mnt/blogs"]) + { volumes: ["%s:/mnt" % $.segments_path], }, + + [if $.enabled.prizebot then "prizebot"]: + bot_service("prizebot", $.prizebot + { + url: $.zulip_url, + }) + { + volumes: ["%s:%s" % [$.prizebot.state_path, $.prizebot.state]], + }, + }, } diff --git a/zulip_bots/zulip_bots/prizebot.py b/zulip_bots/zulip_bots/prizebot.py new file mode 100644 index 0000000..716e8a7 --- /dev/null +++ b/zulip_bots/zulip_bots/prizebot.py @@ -0,0 +1,59 @@ + +import json +import time + +import argh +import requests + +from .config import get_config +from .zulip import Client + + +def get_prizes(type): + resp = requests.get("https://desertbus.org/wapi/prizes/{}".format(type)) + resp.raise_for_status() + return resp.json()['prizes'] + + +def send_message(client, prize, test=False, giveaway=False): + message = "[{title}](https://desertbus.org/prize/{id}) won by {bidder} - raised ${bid:.2f}".format(**prize) + if giveaway: + message += "\n@*editors* Remember to go back and edit the giveaway video" + if test: + print(message) + else: + client.send_to_stream("bot-spam", "Prize Winners", message) + + +def main(config_file, test=False, all=False, once=False, interval=60): + """ + Config: + url, email, api_key: zulip creds + state: path to state file + """ + config = get_config(config_file) + with open(config['state']) as f: + # state is {id: last seen state} + state = json.load(f) + client = Client(config['url'], config['email'], config['api_key']) + while True: + start = time.time() + for type in ('live_auction', 'silent_auction', 'giveaway'): + prizes = get_prizes(type) + for prize in prizes: + id = str(prize['id']) + if prize['state'] == "sold" and (all or state.get(id, "sold") != "sold"): + send_message(client, prize, test=test, giveaway=(type == "giveaway")) + state[id] = prize['state'] + if not test: + with open(config['state'], 'w') as f: + f.write(json.dumps(state) + '\n') + if once: + break + remaining = start + interval - time.time() + if remaining > 0: + time.sleep(remaining) + + +if __name__ == '__main__': + argh.dispatch_command(main)