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,43 +39,43 @@ 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
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) current_trades.append(contact_id)
buyer = contact["data"]["buyer"]["username"] buyer = contact["data"]["buyer"]["username"]
amount = contact["data"]["amount"] amount = contact["data"]["amount"]
@ -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.")