test.py 1.1 KB

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