diff --git a/handler/agora.py b/handler/agora.py index 131776c..f284f3c 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -39,62 +39,62 @@ class Agora(object): 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.start(int(settings.Agora.RefreshSec)) + self.lc_dash = LoopingCall(self.dashboard) + 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. - :return: list of open trade IDs - :rtype: list + Returns a dict for the dashboard. + Calls hooks to parse dashboard info and get all contact messages. + :return: dict of dashboard info + :rtype: dict """ dash = self.agora.dashboard_seller() - dash_tmp = [] + dash_tmp = {} if "data" not in dash["response"].keys(): self.log.error("Data not in dashboard response: {content}", content=dash) return False if dash["response"]["data"]["contact_count"] > 0: for contact in dash["response"]["data"]["contact_list"]: 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 - def dashboard(self, send_irc=True): + def dashboard_hook(self, dash, 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 or False :rtype: list or bool """ - dash = self.agora.dashboard_seller() dash_tmp = [] current_trades = [] - if "data" not in dash["response"]: - 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) - 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.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") + for contact_id, contact in dash.items(): + 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.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") - self.last_dash.add(contact_id) + 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 list(self.last_dash): # We're removing from the list on the fly - if trade not in current_trades: - self.last_dash.remove(trade) + # Purge old trades from cache + for trade in list(self.last_dash): # We're removing from the list on the fly + if trade not in current_trades: + self.last_dash.remove(trade) return dash_tmp def dashboard_release_urls(self): @@ -141,21 +141,31 @@ class Agora(object): 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) + self.irc.client.msg(self.irc.client.channel, f"AUTO {contact_id}: {x}") else: + self.last_messages[contact_id] = 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 def get_all_messages(self, send_irc=True): """ Get all messages for all open trades. + Wrapper for passing in dashboard. :return: dict of lists keyed by trade/contact ID :rtype: dict with lists """ - dash = self.dashboard_id_only() + dash = self.dashboard() if dash is None: 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 = {} for contact_id in dash: messages = self.get_messages(contact_id, send_irc=send_irc) diff --git a/handler/commands.py b/handler/commands.py index 1730bcd..2b4d613 100644 --- a/handler/commands.py +++ b/handler/commands.py @@ -12,7 +12,7 @@ class IRCCommands(object): """ 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: msg("Error getting trades.") return @@ -109,7 +109,11 @@ class IRCCommands(object): msg("Error getting accounts.") for account in accounts: 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 if accounts_posted == 0: msg("No accounts with balances.")