Created
June 1, 2024 10:41
-
-
Save vasi/7fa5147de7b0b9cb8d0928092b4516bb to your computer and use it in GitHub Desktop.
Monarch Money balances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from monarchmoney import MonarchMoney, LoginFailedException | |
import asyncio | |
import csv | |
import getpass | |
import json | |
import os | |
from pathlib import Path | |
import sys | |
async def main(argv): | |
_, account, outdir = sys.argv | |
outdir = Path(outdir) | |
outdir.mkdir(parents=True, exist_ok=True) | |
mm = MonarchMoney() | |
try: | |
print("Logging in...") | |
await mm.login() | |
except LoginFailedException: | |
password = getpass.getpass("Password: ") | |
await mm.login(account, password) | |
print("Getting accounts...") | |
account_data = await mm.get_accounts() | |
accounts = account_data['accounts'] | |
accounts.sort(key=lambda acct: acct['displayName']) | |
print(f"Found {len(accounts)} accounts") | |
with (outdir / 'accounts.json').open('w') as f: | |
json.dump(account_data, f, indent=2) | |
with (outdir / 'accounts.csv').open('w') as f: | |
w = csv.writer(f) | |
w.writerow(['id', 'name']) | |
for acct in accounts: | |
w.writerow([acct['id'], acct['displayName']]) | |
for idx, acct in enumerate(accounts): | |
print(f"Getting balances for #{idx+1}: {acct['displayName']}") | |
id = acct['id'] | |
balances = await mm.get_account_history(id) | |
with (outdir / f'{id}.csv').open('w') as f: | |
w = csv.writer(f) | |
w.writerow(['date', 'balance']) | |
for b in balances: | |
w.writerow([b['date'], b['signedBalance']]) | |
print("Done!") | |
asyncio.run(main(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment