Poll and post trades in the channel

This commit is contained in:
Mark Veidemanis 2021-12-27 19:30:03 +00:00
parent 20b0aef97d
commit 8139845755
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 26 additions and 27 deletions

View File

@ -1,9 +1,9 @@
# Twisted/Klein imports # Twisted/Klein imports
from twisted.logger import Logger from twisted.logger import Logger
from twisted.internet.task import LoopingCall
# Other library imports # Other library imports
from json import loads from json import loads
from simplejson.errors import JSONDecodeError
from forex_python.converter import CurrencyRates from forex_python.converter import CurrencyRates
from agoradesk_py.agoradesk import AgoraDesk from agoradesk_py.agoradesk import AgoraDesk
@ -21,50 +21,49 @@ class Agora(object):
self.agora = AgoraDesk(settings.Agora.Token) self.agora = AgoraDesk(settings.Agora.Token)
self.cr = CurrencyRates() self.cr = CurrencyRates()
self.irc = irc self.irc = irc
self.last_dash = set()
def try_json(self, data): def setup_loop(self):
try: self.lc = LoopingCall(self.dashboard)
parsed = data.json() self.lc.start(int(settings.Agora.RefreshSec))
return parsed
except JSONDecodeError: def dashboard_id_only(self):
self.log.error("Cannot parse {content}", content=data.content) dash = self.agora.dashboard_seller()
return str(data.content) 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): def dashboard(self):
dash = self.agora.dashboard_seller() dash = self.agora.dashboard_seller()
dash_tmp = {} dash_tmp = []
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"]
buyer = contact["data"]["buyer"]["username"] buyer = contact["data"]["buyer"]["username"]
seller = contact["data"]["seller"]["username"]
amount = contact["data"]["amount"] amount = contact["data"]["amount"]
amount_xmr = contact["data"]["amount_xmr"] amount_xmr = contact["data"]["amount_xmr"]
fee_xmr = contact["data"]["fee_xmr"]
currency = contact["data"]["currency"] currency = contact["data"]["currency"]
if not contact["data"]["is_selling"]: if not contact["data"]["is_selling"]:
continue continue
dash_tmp[contact_id] = { if contact_id not in self.last_dash:
"buyer": buyer, self.irc.client.msg(self.irc.client.channel, f"{contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR")
"seller": seller,
"amount": amount,
"amount_xmr": amount_xmr,
"fee_xmr": fee_xmr,
"currency": currency,
}
dash_tmp.append(f"{contact_id}: {buyer} {amount}{currency} {amount_xmr}XMR")
self.last_dash.add(contact_id)
return dash_tmp return dash_tmp
def get_messages(self, contact_id): def get_messages(self, contact_id):
messages = self.agora.contact_messages(contact_id) messages = self.agora.contact_messages(contact_id)
messages_tmp = [] messages_tmp = []
for message in messages["response"]["data"]["message_list"]: for message in messages["response"]["data"]["message_list"]:
print("MESSAGE", message["sender"]["username"], message["msg"])
messages_tmp.append(f"({message['sender']['username']}): {message['msg']}") messages_tmp.append(f"({message['sender']['username']}): {message['msg']}")
return messages_tmp return messages_tmp
def get_all_messages(self): def get_all_messages(self):
dash = self.dashboard() dash = self.dashboard_id_only()
messages_tmp = {} messages_tmp = {}
for contact_id in dash: for contact_id in dash:
messages = self.get_messages(contact_id) messages = self.get_messages(contact_id)

View File

@ -27,6 +27,8 @@ class IRCBot(irc.IRCClient):
self.admins = (settings.IRC.Admins).split("\n") self.admins = (settings.IRC.Admins).split("\n")
self.highlight = (settings.IRC.Highlight).split("\n") self.highlight = (settings.IRC.Highlight).split("\n")
self.channel = settings.IRC.Channel
def set_agora(self, agora): def set_agora(self, agora):
self.agora = agora self.agora = agora
@ -81,11 +83,7 @@ class IRCBot(irc.IRCClient):
elif cmd == "trades" and host in self.admins: elif cmd == "trades" and host in self.admins:
trades = self.agora.dashboard() trades = self.agora.dashboard()
for trade_id in trades: for trade_id in trades:
fmt = ( self.msg(channel, trade_id)
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)
elif cmd == "create" and host in self.admins and len(spl) == 3: elif cmd == "create" and host in self.admins and len(spl) == 3:
posted = self.agora.create_ad(spl[1], spl[2]) posted = self.agora.create_ad(spl[1], spl[2])
@ -96,6 +94,7 @@ class IRCBot(irc.IRCClient):
for message_id in messages: for message_id in messages:
for message in messages[message_id]: for message in messages[message_id]:
self.msg(channel, f"{message_id}: {message}") self.msg(channel, f"{message_id}: {message}")
self.msg(channel, "---")
# self.msg(channel, dumps(messages)) # self.msg(channel, dumps(messages))
def stopcall(self, call): def stopcall(self, call):
@ -103,9 +102,10 @@ class IRCBot(irc.IRCClient):
def signedOn(self): def signedOn(self):
self.log.info("Signed on as %s" % (self.nickname)) self.log.info("Signed on as %s" % (self.nickname))
self.join(settings.IRC.Channel) self.join(self.channel)
def joined(self, channel): def joined(self, channel):
self.agora.setup_loop()
self.log.info("Joined channel %s" % (channel)) self.log.info("Joined channel %s" % (channel))
def privmsg(self, user, channel, msg): def privmsg(self, user, channel, msg):