Skip to content

Instantly share code, notes, and snippets.

@vasi
Created June 1, 2024 10:41
Show Gist options
  • Save vasi/7fa5147de7b0b9cb8d0928092b4516bb to your computer and use it in GitHub Desktop.
Save vasi/7fa5147de7b0b9cb8d0928092b4516bb to your computer and use it in GitHub Desktop.
Monarch Money balances
#!/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