pluto/handler/sinks/__init__.py

124 lines
4.4 KiB
Python
Raw Normal View History

# Other library imports
# import requests
# from json import dumps
import sinks.nordigen
import sinks.truelayer
import util
from db import r
2023-02-09 07:20:00 +00:00
2022-07-15 10:09:54 +00:00
# Project imports
from settings import settings
class Sinks(util.Base):
"""
Class to manage calls to various sinks.
"""
def __init__(self):
super().__init__()
self.account_info = {}
2022-05-02 08:19:55 +00:00
self.currencies = []
def __irc_started__(self):
2022-05-02 15:32:19 +00:00
self.log.debug("IRC hook called.")
self.startup()
2022-05-02 15:32:19 +00:00
self.log.debug("Finished initialising subclasses.")
2022-05-05 08:18:35 +00:00
def all_sinks_authenticated(self): # TODO: fix
2022-05-05 17:16:56 +00:00
self.money.setup_loops()
2022-05-05 08:18:35 +00:00
def startup(self):
"""
We NEED the other libraries, and we initialise fast, so don't make
any race conditions by relying on something that might not be there.
"""
if settings.Nordigen.enabled == "1":
self.nordigen = sinks.nordigen.Nordigen(self)
if settings.TrueLayer.enabled == "1":
self.truelayer = sinks.truelayer.TrueLayer(self)
# setattr(self.truelayer, "sinks", self)
def got_transactions(self, subclass, account_id, transactions):
if not transactions:
return False
transaction_ids = [x["transaction_id"] for x in transactions]
new_key_name = f"new.transactions.{subclass}.{account_id}"
old_key_name = f"transactions.{subclass}.{account_id}"
# for transaction_id in transaction_ids:
if not transaction_ids:
return
r.sadd(new_key_name, *transaction_ids)
difference = list(r.sdiff(new_key_name, old_key_name))
difference = util.convert(difference)
2022-07-15 10:09:54 +00:00
new_transactions = [
x for x in transactions if x["transaction_id"] in difference
]
# Rename the new key to the old key so we can run the diff again
r.rename(new_key_name, old_key_name)
for transaction in new_transactions:
transaction["subclass"] = subclass
self.tx.transaction(transaction)
def got_account_info(self, subclass, account_infos):
"""
Called when we get account information from an API provider.
:param subclass: class name that called it, truelayer, fidor, etc
:param account_infos: dict of dicts of account information
:param account_infos: dict
"""
2022-04-19 20:03:11 +00:00
if not account_infos:
self.log.error(f"No accounts provided for {subclass}") #
return
for bank, accounts in account_infos.items():
for index, account in enumerate(list(accounts)):
if "account_number" not in account:
account_infos[bank][index]["account_number"] = {}
fields = ["sort_code", "number", "iban"]
for field in fields:
if field in account:
2022-07-15 10:09:54 +00:00
account_infos[bank][index]["account_number"][
field
] = account[field]
del account_infos[bank][index][field]
if len(account["account_number"]) == 1:
account_infos[bank].remove(account)
self.log.warning(f"Potentially useless bank account: {account}")
2022-07-15 10:09:54 +00:00
currencies = [
account["currency"]
for bank, accounts in account_infos.items()
for account in accounts
]
for bank, accounts in account_infos.items():
self.account_info[bank] = []
for account in accounts:
self.account_info[bank].append(account)
# self.account_info = account_infos
self.currencies = currencies
# parsed_details =
# {"EUR": {"IBAN": "xxx", "BIC": "xxx"},
# "GBP": {"SORT": "04-04-04", "ACCOUNT": "1922-2993"}}
# self.markets.distribute_account_details(currencies, account_infos)
2022-04-09 18:59:22 +00:00
def get_total_usd(self):
"""
Get the total balance of our accounts in USD.
"""
2022-04-12 21:06:28 +00:00
total = 0
if settings.Nordigen.enabled == "1":
total_nordigen = self.nordigen.get_total_map()
total_nordigen_usd = self.money.multiple_to_usd(total_nordigen)
total += total_nordigen_usd
if settings.TrueLayer.enabled == "1":
total_truelayer = self.truelayer.get_total_map()
total_truelayer_usd = self.money.multiple_to_usd(total_truelayer)
total += total_truelayer_usd
2022-04-09 18:59:22 +00:00
2022-04-12 21:06:28 +00:00
return total