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

View File

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