Handle dashbord smarter and poll messages

This commit is contained in:
Mark Veidemanis 2021-12-29 14:25:07 +00:00
parent d5c272d3f9
commit 64bf5a77ee
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 52 additions and 38 deletions

View File

@ -39,62 +39,62 @@ class Agora(object):
def setup_loop(self): def setup_loop(self):
""" """
Set up the LoopingCall to get all active trades. Set up the LoopingCall to get all active trades and messages.
""" """
self.lc = LoopingCall(self.dashboard) self.lc_dash = LoopingCall(self.dashboard)
self.lc.start(int(settings.Agora.RefreshSec)) self.lc_dash.start(int(settings.Agora.RefreshSec))
def dashboard_id_only(self): def dashboard(self, send_irc=True, formatted=False):
""" """
Returns a list of the IDs of open trades. Returns a dict for the dashboard.
:return: list of open trade IDs Calls hooks to parse dashboard info and get all contact messages.
:rtype: list :return: dict of dashboard info
:rtype: dict
""" """
dash = self.agora.dashboard_seller() dash = self.agora.dashboard_seller()
dash_tmp = [] dash_tmp = {}
if "data" not in dash["response"].keys(): if "data" not in dash["response"].keys():
self.log.error("Data not in dashboard response: {content}", content=dash) self.log.error("Data not in dashboard response: {content}", content=dash)
return False return False
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"]
dash_tmp.append(contact_id) dash_tmp[contact_id] = contact
fmt = self.dashboard_hook(dash_tmp)
self.get_all_messages_hook(dash_tmp)
if formatted:
return fmt
return dash_tmp return dash_tmp
def dashboard(self, send_irc=True): def dashboard_hook(self, dash, 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. Post new trades to IRC and cache trades for the future.
:return: human readable list of strings about our trades or False :return: human readable list of strings about our trades or False
:rtype: list or bool :rtype: list or bool
""" """
dash = self.agora.dashboard_seller()
dash_tmp = [] dash_tmp = []
current_trades = [] current_trades = []
if "data" not in dash["response"]: for contact_id, contact in dash.items():
return False current_trades.append(contact_id)
if dash["response"]["data"]["contact_count"] > 0: buyer = contact["data"]["buyer"]["username"]
for contact in dash["response"]["data"]["contact_list"]: amount = contact["data"]["amount"]
contact_id = contact["data"]["contact_id"] amount_xmr = contact["data"]["amount_xmr"]
current_trades.append(contact_id) currency = contact["data"]["currency"]
buyer = contact["data"]["buyer"]["username"] if not contact["data"]["is_selling"]:
amount = contact["data"]["amount"] continue
amount_xmr = contact["data"]["amount_xmr"] if contact_id not in self.last_dash:
currency = contact["data"]["currency"] self.tx.new_trade(contact_id, buyer, currency, amount, amount_xmr)
if not contact["data"]["is_selling"]: if send_irc:
continue self.irc.client.msg(self.irc.client.channel, f"AUTO {contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR")
if contact_id not in self.last_dash:
self.tx.new_trade(contact_id, buyer, currency, amount, amount_xmr)
if send_irc:
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 # Purge old trades from cache
for trade in list(self.last_dash): # We're removing from the list on the fly for trade in list(self.last_dash): # We're removing from the list on the fly
if trade not in current_trades: if trade not in current_trades:
self.last_dash.remove(trade) self.last_dash.remove(trade)
return dash_tmp return dash_tmp
def dashboard_release_urls(self): def dashboard_release_urls(self):
@ -141,21 +141,31 @@ class Agora(object):
if not len(self.last_messages[contact_id]) == len(messages_tmp): if not len(self.last_messages[contact_id]) == len(messages_tmp):
difference = set(messages_tmp) ^ self.last_messages[contact_id] difference = set(messages_tmp) ^ self.last_messages[contact_id]
for x in difference: for x in difference:
self.irc.client.msg(self.irc.client.channel, x) self.irc.client.msg(self.irc.client.channel, f"AUTO {contact_id}: {x}")
else: else:
self.last_messages[contact_id] = messages_tmp
for x in messages_tmp: for x in messages_tmp:
self.irc.client.msg(self.irc.client.channel, x) self.irc.client.msg(self.irc.client.channel, f"NEW {contact_id}: {x}")
return messages_tmp return messages_tmp
def get_all_messages(self, send_irc=True): def get_all_messages(self, send_irc=True):
""" """
Get all messages for all open trades. Get all messages for all open trades.
Wrapper for passing in dashboard.
:return: dict of lists keyed by trade/contact ID :return: dict of lists keyed by trade/contact ID
:rtype: dict with lists :rtype: dict with lists
""" """
dash = self.dashboard_id_only() dash = self.dashboard()
if dash is None: if dash is None:
return False return False
return self.get_all_messages_hook(dash, send_irc=send_irc)
def get_all_messages_hook(self, dash, send_irc=True):
"""
Get all messages for all open trades.
:return: dict of lists keyed by trade/contact ID
:rtype: dict with lists
"""
messages_tmp = {} messages_tmp = {}
for contact_id in dash: for contact_id in dash:
messages = self.get_messages(contact_id, send_irc=send_irc) messages = self.get_messages(contact_id, send_irc=send_irc)

View File

@ -12,7 +12,7 @@ class IRCCommands(object):
""" """
Get details of open trades and post on IRC. Get details of open trades and post on IRC.
""" """
trades = agora.dashboard(send_irc=False) trades = agora.dashboard(send_irc=False, formatted=True)
if trades is False: if trades is False:
msg("Error getting trades.") msg("Error getting trades.")
return return
@ -109,7 +109,11 @@ class IRCCommands(object):
msg("Error getting accounts.") msg("Error getting accounts.")
for account in accounts: for account in accounts:
if account["balance"] > 0: if account["balance"] > 0:
msg(f"{account['name']} {account['currency']}: {account['balance']}") if "name" in account:
name = account["name"]
else:
name = "not_set"
msg(f"{name} {account['currency']}: {account['balance']}")
accounts_posted += 1 accounts_posted += 1
if accounts_posted == 0: if accounts_posted == 0:
msg("No accounts with balances.") msg("No accounts with balances.")