From 8139845755780549f31e3dde6500546df64777f3 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 27 Dec 2021 19:30:03 +0000 Subject: [PATCH] Poll and post trades in the channel --- handler/agora.py | 41 ++++++++++++++++++++--------------------- handler/irc.py | 12 ++++++------ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 9b1f6b3..7aa6815 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -1,9 +1,9 @@ # Twisted/Klein imports from twisted.logger import Logger +from twisted.internet.task import LoopingCall # Other library imports from json import loads -from simplejson.errors import JSONDecodeError from forex_python.converter import CurrencyRates from agoradesk_py.agoradesk import AgoraDesk @@ -21,50 +21,49 @@ class Agora(object): self.agora = AgoraDesk(settings.Agora.Token) self.cr = CurrencyRates() self.irc = irc + self.last_dash = set() - def try_json(self, data): - try: - parsed = data.json() - return parsed - except JSONDecodeError: - self.log.error("Cannot parse {content}", content=data.content) - return str(data.content) + def setup_loop(self): + self.lc = LoopingCall(self.dashboard) + self.lc.start(int(settings.Agora.RefreshSec)) + + def dashboard_id_only(self): + dash = self.agora.dashboard_seller() + dash_tmp = [] + 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) + return dash_tmp def dashboard(self): dash = self.agora.dashboard_seller() - dash_tmp = {} + dash_tmp = [] if dash["response"]["data"]["contact_count"] > 0: for contact in dash["response"]["data"]["contact_list"]: contact_id = contact["data"]["contact_id"] buyer = contact["data"]["buyer"]["username"] - seller = contact["data"]["seller"]["username"] amount = contact["data"]["amount"] amount_xmr = contact["data"]["amount_xmr"] - fee_xmr = contact["data"]["fee_xmr"] currency = contact["data"]["currency"] if not contact["data"]["is_selling"]: continue - dash_tmp[contact_id] = { - "buyer": buyer, - "seller": seller, - "amount": amount, - "amount_xmr": amount_xmr, - "fee_xmr": fee_xmr, - "currency": currency, - } + if contact_id not in self.last_dash: + self.irc.client.msg(self.irc.client.channel, 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) return dash_tmp def get_messages(self, contact_id): messages = self.agora.contact_messages(contact_id) messages_tmp = [] for message in messages["response"]["data"]["message_list"]: - print("MESSAGE", message["sender"]["username"], message["msg"]) messages_tmp.append(f"({message['sender']['username']}): {message['msg']}") return messages_tmp def get_all_messages(self): - dash = self.dashboard() + dash = self.dashboard_id_only() messages_tmp = {} for contact_id in dash: messages = self.get_messages(contact_id) diff --git a/handler/irc.py b/handler/irc.py index f446dd8..d9d9c5a 100644 --- a/handler/irc.py +++ b/handler/irc.py @@ -27,6 +27,8 @@ class IRCBot(irc.IRCClient): self.admins = (settings.IRC.Admins).split("\n") self.highlight = (settings.IRC.Highlight).split("\n") + self.channel = settings.IRC.Channel + def set_agora(self, agora): self.agora = agora @@ -81,11 +83,7 @@ class IRCBot(irc.IRCClient): elif cmd == "trades" and host in self.admins: trades = self.agora.dashboard() for trade_id in trades: - fmt = ( - f"{trade_id}: {trades[trade_id]['buyer']} " - f"{trades[trade_id]['amount']}{trades[trade_id]['currency']} {trades[trade_id]['amount_xmr']}XMR" - ) - self.msg(channel, fmt) + self.msg(channel, trade_id) elif cmd == "create" and host in self.admins and len(spl) == 3: posted = self.agora.create_ad(spl[1], spl[2]) @@ -96,6 +94,7 @@ class IRCBot(irc.IRCClient): for message_id in messages: for message in messages[message_id]: self.msg(channel, f"{message_id}: {message}") + self.msg(channel, "---") # self.msg(channel, dumps(messages)) def stopcall(self, call): @@ -103,9 +102,10 @@ class IRCBot(irc.IRCClient): def signedOn(self): self.log.info("Signed on as %s" % (self.nickname)) - self.join(settings.IRC.Channel) + self.join(self.channel) def joined(self, channel): + self.agora.setup_loop() self.log.info("Joined channel %s" % (channel)) def privmsg(self, user, channel, msg):