-
Notifications
You must be signed in to change notification settings - Fork 0
/
moneymoney_sum_by_bank.py
35 lines (26 loc) · 1.5 KB
/
moneymoney_sum_by_bank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3
"""
Python script for the great MoneyMoney app to generate a list with total balance by bank account.
"""
import logging
import pandas as pd
from moneymoney_api import fetch_moneymoney_accounts, Account
def sum_by_account(accounts: list[Account]) -> pd.DataFrame:
balance_per_bank_and_currency = []
for account in accounts:
if account['portfolio'] is True or account['group'] is True:
continue
if not account['bankCode']:
account['bankCode'] = account['attributes']['bankIdentifier'] if 'bankIdentifier' in account['attributes'] else 'other'
logging.debug("Account %s has no bank code, using '%s'", account["name"], account['bankCode'])
else:
# we only take the first 4 characters of the bic as that is the bank code, @see https://www.buchhaltung-einfach-sicher.de/finanzen/bic
account['bankCode'] = account['bankCode'][:4]
for balance in account['balance']:
balance_per_bank_and_currency.append([account['bankCode'], balance[0], balance[1]])
df_balance_per_bank_and_currency = pd.DataFrame(balance_per_bank_and_currency, columns=['bank', 'balance', 'currency'])
return df_balance_per_bank_and_currency.groupby(['bank', 'currency']).agg({'balance': 'sum'})
if __name__ == "__main__":
# logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
df = sum_by_account(fetch_moneymoney_accounts())
print(df[df['balance'] > 0].sort_values(by='balance', ascending=False))