@ -11,12 +11,11 @@ from .zulip import Client
import requests
import requests
session = requests . Session ( )
session = requests . Session ( )
def stream ( ) :
def stream ( channels ) :
channels = " , " . join ( channels )
tt = 0
tt = 0
tr = 0
tr = 0
while True :
while True :
channels = [ " total:RZZQRDQNLNLW " ]
channels = " , " . join ( channels )
resp = session . get ( f " https://ps8.pndsn.com/v2/subscribe/sub-cbd7f5f5-1d3f-11e2-ac11-877a976e347c/ { channels } /0 " ,
resp = session . get ( f " https://ps8.pndsn.com/v2/subscribe/sub-cbd7f5f5-1d3f-11e2-ac11-877a976e347c/ { channels } /0 " ,
params = {
params = {
" tt " : tt ,
" tt " : tt ,
@ -31,12 +30,6 @@ def stream():
tr = data [ " t " ] [ " r " ]
tr = data [ " t " ] [ " r " ]
def get_prize ( prize_id ) :
resp = session . get ( " https://desertbus.org/wapi/prize/ {} " . format ( prize_id ) )
resp . raise_for_status ( )
return resp . json ( ) [ " prize " ]
giveaway_cache = [ None , None ]
giveaway_cache = [ None , None ]
def get_giveaway ( ) :
def get_giveaway ( ) :
REFRESH_RISING = 30
REFRESH_RISING = 30
@ -58,6 +51,8 @@ def main(conf_file, message_log_file, name=socket.gethostname()):
zulip_url
zulip_url
zulip_email
zulip_email
zulip_api_key
zulip_api_key
total_id : id for donation total channel
prize_ids : list of ids for prizes to watch bids for
"""
"""
logging . basicConfig ( level = " INFO " )
logging . basicConfig ( level = " INFO " )
@ -75,9 +70,12 @@ def main(conf_file, message_log_file, name=socket.gethostname()):
" time " : time . time ( ) ,
" time " : time . time ( ) ,
} )
} )
prizes = { }
total_channel = f " total: { config [ ' total_id ' ] } "
channels = [ total_channel ] + [
f " bid: { prize_id } " for prize_id in config [ " prize_ids " ]
]
total = None
total = None
for msg in stream ( ) :
for msg in stream ( channels ) :
log = {
log = {
" type " : " unknown " ,
" type " : " unknown " ,
" host " : name ,
" host " : name ,
@ -94,7 +92,7 @@ def main(conf_file, message_log_file, name=socket.gethostname()):
log [ " message_time " ] = message_time
log [ " message_time " ] = message_time
if msg [ " c " ] . startswith ( " total " ) :
if msg [ " c " ] == total_channel :
log [ " type " ] == " total "
log [ " type " ] == " total "
increase = None if total is None else msg [ " d " ] - total
increase = None if total is None else msg [ " d " ] - total
log [ " increase " ] = increase
log [ " increase " ] = increase
@ -114,25 +112,20 @@ def main(conf_file, message_log_file, name=socket.gethostname()):
client . send_to_stream ( " bot-spam " , " Notable Donations " , " Large donation of $ {:.2f} (total $ {:.2f} ) {} " . format ( increase , msg [ ' d ' ] , entries_str ) )
client . send_to_stream ( " bot-spam " , " Notable Donations " , " Large donation of $ {:.2f} (total $ {:.2f} ) {} " . format ( increase , msg [ ' d ' ] , entries_str ) )
total = msg [ " d " ]
total = msg [ " d " ]
elif msg [ " c " ] == " db_vue " and msg [ " d " ] . get ( " channel " ) . startswith ( " prize :" ) :
elif msg [ " c " ] . startswith ( " bid :" ) :
log [ " type " ] = " prize "
log [ " type " ] = " prize "
d = msg [ " d " ]
prize_id = msg [ " c " ] . removeprefix ( " bid: " )
prize_id = msg [ " d " ] [ " channel " ] . split ( " : " ) [ 1 ]
data = d [ " data " ]
logging . info ( f " Prize update for { prize_id } : { data } " )
if prize_id not in prizes :
prizes [ prize_id ] = get_prize ( prize_id )
log [ " prize_id " ] = prize_id
log [ " prize_id " ] = prize_id
if " bidder " in data and " bid " in data :
data = msg [ " d " ]
log [ " bidder " ] = data [ " bidder " ]
logging . info ( f " Prize update for { prize_id } : { data } " )
log [ " bid " ] = data [ " bid " ]
if " name " in data and " amount " in data :
client. send_to_stream ( " bot-spam " , " Bids " , " At <time: {time} >, {bidder} bid $ {bid:.2f} for [ {title} ](https://desertbus.org/prize/ {prize_id} ) " . format (
log [ " bidder " ] = data [ " name " ]
time = message_time ,
log [ " bid " ] = data [ " amount " ]
bidder = data [ " bidder " ] ,
client . send_to_stream (
bid = data [ " bid " ] ,
" bot-spam " ,
title = prizes [ prize_id ] [ " title " ] ,
" Bids " ,
prize_id = prize_id ,
" At <time: {message_time} >, {data['name']} ( {data['donorID']} ) has the high bid of $ {data['amount']:.2f} for prize [ {prize_id} ](https://desertbus.org/prize/ {prize_id} ) " ,
) )
)
else :
else :
logging . warning ( " Unknown message: {} " . format ( msg ) )
logging . warning ( " Unknown message: {} " . format ( msg ) )