Import prizebot

pull/428/head
Mike Lang 2 weeks ago
parent 8020df8e60
commit d83ac78f74

@ -35,6 +35,7 @@
tootbot: false,
twitchbot: false,
pubbot: false,
prizebot: false,
bus_analyzer: false,
graphs: false,
},
@ -320,6 +321,15 @@
zulip_api_key: "",
},
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",
@ -749,6 +759,13 @@
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]],
},
},
}

@ -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)
Loading…
Cancel
Save