Browse Source

Inital commit

Nikola Kotur 5 years ago
commit
ddf934da20
3 changed files with 37 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 2 0
      requirements.txt
  3. 34 0
      test.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+.env

+ 2 - 0
requirements.txt

@@ -0,0 +1,2 @@
+hammock
+logzero

+ 34 - 0
test.py

@@ -0,0 +1,34 @@
+#!/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 speding per day.
+    for date, ts in transactions.items():
+        total = u'%4.2f €' % abs(sum(ts) / 1000)
+        logger.info('%s: %s', date, total)