12345678910111213141516171819202122232425262728293031323334 |
- #!/usr/bin/env python3
- import os
- import datetime
- import collections
- import hammock
- from logzero import logger
- if __name__ == '__main__':
- # Init the API.
- ynab = hammock.Hammock(
- 'https://api.youneedabudget.com/v1',
- headers={'Authorization': 'Bearer {}'.format(os.getenv('YNAB_API_KEY'))},
- )
- # Get the budget.
- budgets = ynab.budgets.GET().json()['data']
- # This code supports accounts with single budget. Or it will just take the first one.
- budget = budgets['budgets'][0]
- logger.info('Working with budget "%s"', budget['name'])
- # Figure out the date 7 days ago.
- week_ago = (datetime.datetime.today() - datetime.timedelta(days=7)).strftime('%Y-%m-%d')
- # Get all transactions.
- transactions = collections.defaultdict(list)
- for t in ynab.budgets(budget['id']).transactions.GET('?since_date={}'.format(week_ago)).json()['data']['transactions']:
- transactions[t['date']].append(t['amount'])
- # Output spending per day.
- for date, ts in transactions.items():
- total = u'%4.2f €' % abs(sum(ts) / 1000)
- logger.info('%s: %s', date, total)
|