Post new trades and messages to the channel

This commit is contained in:
Mark Veidemanis 2021-12-28 01:00:02 +00:00
parent 3de4f16622
commit 45abcfc7dc
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 35 additions and 6 deletions

View File

@ -24,8 +24,13 @@ class Agora(object):
self.log = Logger("agora")
self.agora = AgoraDesk(settings.Agora.Token)
self.cr = CurrencyRates()
# Cache for detecting new trades
self.last_dash = set()
# Cache for detecting new messages
self.last_messages = {}
def set_irc(self, irc):
self.irc = irc
@ -50,33 +55,43 @@ class Agora(object):
dash_tmp.append(contact_id)
return dash_tmp
def dashboard(self):
def dashboard(self, send_irc=True):
"""
Get information about our open trades.
Post new trades to IRC and cache trades for the future.
:return: human readable list of strings about our trades
:rtype: list
"""
dash = self.agora.dashboard_seller()
dash_tmp = []
current_trades = []
if dash["response"]["data"]["contact_count"] > 0:
for contact in dash["response"]["data"]["contact_list"]:
contact_id = contact["data"]["contact_id"]
current_trades.append(contact_id)
buyer = contact["data"]["buyer"]["username"]
amount = contact["data"]["amount"]
amount_xmr = contact["data"]["amount_xmr"]
currency = contact["data"]["currency"]
if not contact["data"]["is_selling"]:
continue
if contact_id not in self.last_dash:
self.irc.client.msg(self.irc.client.channel, f"AUTO {contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR")
if send_irc:
if contact_id not in self.last_dash:
self.irc.client.msg(self.irc.client.channel, f"AUTO {contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR")
dash_tmp.append(f"{contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR")
self.last_dash.add(contact_id)
# Purge old trades from cache
for trade in self.last_dash:
if trade not in current_trades:
self.last_dash.remove(trade)
return dash_tmp
def get_messages(self, contact_id):
def get_messages(self, contact_id, send_irc=True):
"""
Get messages for a certain trade ID.
Post new messages to IRC and cache messages for the future.
:param contact_id: trade/contact ID
:return: list of messages
:rtype: list
@ -85,6 +100,15 @@ class Agora(object):
messages_tmp = []
for message in messages["response"]["data"]["message_list"]:
messages_tmp.append(f"({message['sender']['username']}): {message['msg']}")
if send_irc:
if contact_id in self.last_messages:
if not len(self.last_messages[contact_id]) == len(messages_tmp):
difference = set(messages_tmp) ^ self.last_messages[contact_id]
for x in difference:
self.irc.client.msg(self.irc.client.channel, x)
else:
for x in messages_tmp:
self.irc.client.msg(self.irc.client.channel, x)
return messages_tmp
def get_all_messages(self):
@ -99,6 +123,11 @@ class Agora(object):
messages = self.get_messages(contact_id)
messages_tmp[contact_id] = messages
# Purge old trades from cache
for trade in self.last_messages:
if trade not in messages_tmp:
del self.last_messages[trade]
return messages_tmp
def get_ads(self):

View File

@ -58,7 +58,7 @@ class IRCBot(irc.IRCClient):
if cmd == "trades" and host in self.admins:
# Get details of open trades and post on IRC
trades = self.agora.dashboard()
trades = self.agora.dashboard(send_irc=False)
for trade_id in trades:
self.msg(channel, trade_id)
@ -81,7 +81,7 @@ class IRCBot(irc.IRCClient):
elif cmd == "messages" and host in self.admins and len(spl) == 2:
# Get all messages for a given trade
messages = self.agora.get_messages(spl[1])
messages = self.agora.get_messages(spl[1], send_irc=False)
for message in messages:
self.msg(channel, f"{spl[1]}: {message}")