From 2f274390a8db77c25f53eae052ae7848c26f0b0d Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 28 Dec 2021 11:26:51 +0000 Subject: [PATCH] Implement getting total USD value of Revolut account --- handler/agora.py | 6 ++++-- handler/app.py | 4 +++- handler/irc.py | 19 +++++++++++++++++++ handler/revolut.py | 27 +++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index 4db264b..bf32892 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -59,12 +59,14 @@ class Agora(object): """ 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 - :rtype: list + :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"] diff --git a/handler/app.py b/handler/app.py index 0109909..07a79be 100755 --- a/handler/app.py +++ b/handler/app.py @@ -62,8 +62,10 @@ if __name__ == "__main__": # Define Revolut revolut = Revolut() - # Pass IRC to Revolut + # Pass IRC to Revolut and Revolut to IRC revolut.set_irc(irc) + irc.set_revolut(revolut) + revolut.set_agora(agora) # Define Transactions tx = Transactions() diff --git a/handler/irc.py b/handler/irc.py index 7a3485d..1456958 100644 --- a/handler/irc.py +++ b/handler/irc.py @@ -39,6 +39,9 @@ class IRCBot(irc.IRCClient): def set_agora(self, agora): self.agora = agora + def set_revolut(self, revolut): + self.revolut = revolut + def parse(self, user, host, channel, msg): """ Simple handler for IRC commands. @@ -59,6 +62,8 @@ class IRCBot(irc.IRCClient): if cmd == "trades" and host in self.admins: # Get details of open trades and post on IRC trades = self.agora.dashboard(send_irc=False) + if not trades: + self.msg(channel, "No open trades.") for trade_id in trades: self.msg(channel, trade_id) @@ -104,6 +109,16 @@ class IRCBot(irc.IRCClient): else: return dumps(rtrn) + elif cmd == "accounts" and host in self.admins: + accounts = self.revolut.accounts() + for account in accounts: + if account["balance"] > 0: + self.msg(channel, f"{account['name']} {account['currency']}: {account['balance']}") + + elif cmd == "total" and host in self.admins: + total_usd = self.revolut.get_total_usd() + self.msg(channel, f"Total: {round(total_usd, 2)}USD") + def signedOn(self): """ Called when we have signed on to IRC. @@ -173,6 +188,9 @@ class IRCBotFactory(protocol.ClientFactory): def set_agora(self, agora): self.agora = agora + def set_revolut(self, revolut): + self.revolut = revolut + def buildProtocol(self, addr): """ Custom override for the Twisted buildProtocol so we can access the Protocol instance. @@ -182,6 +200,7 @@ class IRCBotFactory(protocol.ClientFactory): prcol = IRCBot(self.log) self.client = prcol self.client.set_agora(self.agora) + self.client.set_revolut(self.revolut) return prcol def clientConnectionLost(self, connector, reason): diff --git a/handler/revolut.py b/handler/revolut.py index 6a915b3..235c292 100644 --- a/handler/revolut.py +++ b/handler/revolut.py @@ -29,6 +29,9 @@ class Revolut(object): def set_irc(self, irc): self.irc = irc + def set_agora(self, agora): + self.agora = agora + def setup_auth(self): """ Function to create a new Java Web Token and use it to get a refresh/access token. @@ -169,3 +172,27 @@ class Revolut(object): else: self.log.error("Cannot get webhook: {content}", r.content) return False + + def accounts(self): + """ + Get the details and balances of all Revolut accounts. + :return: account details + :rtype: dict + """ + headers = {"Authorization": f"Bearer {self.token}"} + r = requests.get(f"{settings.Revolut.Base}/accounts", headers=headers) + if r.status_code == 200: + return r.json() + else: + return False + + def get_total_usd(self): + rates = self.agora.get_rates_all() + accounts = self.accounts() + total_usd = 0 + for account in accounts: + if account["currency"] == "USD": + total_usd += account["balance"] + else: + total_usd += account["balance"] / rates[account["currency"]] + return total_usd